ucEval
ucEvalStr
See Also: ucParse, ucEvaluate
Evaluates an expression in one simple step.
ucEval(Expression [, tHandle])
-or-
ucEvalStr(Expression [, tHandle])
ucEval is a convenient one-step procedure for evaluating an expression. It is adequate for general situations where speed is not of utmost importance, such as when the expression is not recalculated many times in a loop. It is similar to calling ucParse, ucEvaluate, returning a value, and immediately releasing the expression from the definition space.
Parameters
Expression
Required. Expression is an argument you pass as a string, such as "5 ^ 2 + 3", the result of which you are interested in. An expression may also consist of multiple statements separated by a semi-colon (;). In the case of multiple statements, the value of the sub-expression following the last semi-colon is the one that is returned.
tHandle
Optional. This represents the handle of a thread. By restricting an expression to a given thread, the expression will be evaluated based only on the definitions of that thread (and its parent thread(s)). So you might have a variable x in one thread with one value, and variable x in another thread. Each expression will evaluate differently based on the value assigned to x in the thread you are evaluating in. See Thread Handling.
Remarks
ucEval returns a numeric value in extended precision (or double precision for compilers that do not support extended precision).
If your expression evaluates to a string result, or another non-numeric type, then use ucEvalStr instead.
Expressions of any data type can be returned with ucEvalStr. It can be useful even for numeric expressions, where it might save you an extra step of converting the numeric result to a string yourself, such is if you wanted to store the result in a text box for instance.
ucEvalStr can also return special numeric values such as Nan, +Inf, and -Inf. And for compilers where ucEval returns a double precision number instead of extended precision, you can use ucEvalStr to return values beyond the scope of double precision.
If an error occurs, such as a syntax error in the expression itself, or a division by 0 during evaluation, ucEvalStr can return a string containing the appropriate error message.
Example 1:
UserExpression$ = InputBox("Enter an
expression", "Example", "5+4*10")
MsgBox ucEval(UserExpression$)
Example 2:
UserExpression$ = InputBox("Enter an
expression", "Example", "'Hello ' + 'World'")
MsgBox ucEvalStr(UserExpression$)
' Other expressions you can try:
' 1 + 2 /
' Returns a syntax error message
' 1500!
' Returns a large number beyond the scope of double precision
' 5 / 0
' Returns either Inf, or a division by 0 error message,
depending on your FPU setting
Example 3:
tHandleA = ucNewThread()
ucDefineVariable("n = 50", tHandleA)
ucDefineFunction("f(x) = x ^ 2", tHandleA)
tHandleB = ucNewThread()
ucDefineVariable("n = 1000", tHandleB)
ucDefineFunction("f(x) = x + 1", tHandleB)
Print ucEval("f(5)
+ n", tHandleA) ' Returns 75
Print ucEval("f(5)
+ n", tHandleB) ' Returns 1006
New or Enhanced