Evaluates an expression at runtime.
# = Eval(exp)
# = Eval(exp, hash[])
# = Eval(exp, [ hash[]] , function)
exp: sexp
hash: Hash
function:Function
Evaluates a formula or expression that is in the form of text and returns the result as a Double.
Print Eval("1+2*3")
Print Eval("1.045 ^ 20")
Eval knows the basic mathematical rules. The function Eval is performed by a small internal compiler. Eval accepts the following numbers, operators, and functions:
numbers | like with Val, also #date# or Hex with $x or binary with %x. |
brackets | ( ) |
addition | + |
subtraction | - |
multiplication | * |
division | / |
Modulo | Mod |
Integer division | Div |
leading sign | - |
leading sign | + |
raise to a higher power | ^ |
raise to a higher power | ** |
Pi | 3.1415.. |
goniometric functions | Sin(x), Cos(x), Tan(x) |
logarithm | Log(x), Exp(x) |
binary And | And |
binary Or | Or |
binary exclusive Or | Xor |
Extreme values | Min(x, y), Max(x, y) |
Diverse | Int(x), Trunc(x), Frac(x), Fix(x) Floor(x), Ceil(x), Abs(x), Sgn(x) Rnd, Rnd(x), Random(x), Rnd(x) is exact like Random(x), Sqr(x), Not, Fact(n), Combin(n, k), Permut(n, k), Exp2(x), Exp10(x), Log2(x) == Lb(x), Log10(x) == Lg(x) Log(x) == Ln(x) |
Comparison* | < <= =< != <> >< = == >= => > |
Not* | ! |
* The return value of a comparison, and of the ! - operator, are the floating point numbers -1.0 or 0.0.
Strings passed to the Eval function must be correctly formatted, otherwise they will throw an error (see Known Issue).
# = Eval(exp, hash[])
The second form accepts a Hash Double which holds variable values. The key of a Hash element is the variable name and the Double a value. The Hash will be evaluated before anything else, this excludes the usage of h["pi"], h["Sin"] or h["Mod"] keys.
# = Eval(exp, [hash[]], function)
The third form accepts, optionally, a Hash Double holding variable values and a function name. The function is executed for the string expression exp and the entire string expression exp will be passed as the first argument, followed by the number of parameters in exp, followed by the parameters itself in a one dimensional array of type double.
Function func(x$, n%, f#()) As Double
For example, suppose
Print Eval("new(1.3, 3*9)", , EvalFunc)
The Eval function executes EvalFunc passing "new" in x$, 2 in n%, and the arguments of new() in f(1) = 1.3, f(2) = 27. The EvalFunc could look like this:
Function EvalFunc(x$, n%, f#()) As Double
Select Lower(x)
Case "new"
If n% = 2 Then Return Mul(f(1), f(1))
Err.Raise 1001, "EvalFunc", "New(x, y) expects 2 parameters, not " & n
EndSelect
Err.Raise 1000, "EvalFunc", " extension " & x$ & " unknown."
EndFunc
The maximum number of parameters for a user-defined function is 5. The parameter array is a 'global' array with a dimension of (1..10).
The use of a Hash
OpenW 1
Local h As Hash Double, x%
h["a"] = 123
h["rent"] = 1.075
Print Eval("a / 7", h[])
Print Eval("10000*rent^30", h[])
The performance of the Eval function depends on the parsing of the expression string and can hardly be compared by normal calculations. The following example shows the difference.
OpenW 1 : Win_1.FontName = "Courier New"
Local t#(3), d#, a#, b#, c#, i%
t(1) = Timer
For i = 1 To 100000
d = Eval("1*2+3")
Next
t(1) = Timer - t(1)
t(2) = Timer
a = 1 : b = 2 : c = 3
For i = 1 To 100000
d = a * b + c
Next
t(2) = Timer - t(2)
t(3) = Timer
For i = 1 To 100000
d = Val("1") * Val("2") + Val("3")
Next
t(3) = Timer - t(3)
Print "Time for Eval: "; t(1); Tab(45); " ~"; Int((t(1) / t(2)) + 0.5); "times slower than variables"
Print "Time for Variables: "; t(2)
Print "Time for Val(): "; t(3); Tab(45); " ~"; Int((t(3) / t(2)) + 0.5); "times slower than variables & ~"; Int((t(1) / t(3)) + 0.5); "times faster than Eval"
Do
Sleep
Loop Until Me Is Nothing
The version with Eval is three times slower as the one with Val, and 200 times slower as the version with the variables.
If a string passed to Eval contains an invalid symbol (such as starting with an equals sign (=)), this will cause the program to stop BUT an error will not necessarily be thrown (sometimes an Invalid Parameter error appears, sometimes not).
{Created by Sjouke Hamstra; Last updated: 17/10/2017 by James Gaite}