EvaluateMethod

Applies to:Fast Math Parser
Class:uCalc.ParsedExpr
Rapidly evaluates a floating point expression that was parsed with Parse or passed By Expression
Syntax
Evaluate()
Remarks
Evaluate can be used instead of Eval for faster results in time-critical loops where the same expression is repeatedly re-evaluated, with the only change in each iteration typically being the value(s) of one or more variables. Evaluate works in pair with Parse, the relatively slower part, which is called first before going into the time-critical loop. The ParsedExpr object returned by Parse is what calls the Evaluate function.

Evaluate can also be called by a ParsedExpr object that was passed to a callback By Expression.

The expression must be one who's evaluation returns a floating point numeric value. If the expression evaluates to a non-numeric value (such as a string) or non-scalar numeric value (such as a complex number) , or if a numeric type other than the default was explicitely specified with Parse, the value returned will be invalid. However, an error will not be raised.

Example 1: Displaying a different result for each value of x based on the expression parsed with Parse

Dim VariableX = uc.DefineVariable("x")
Dim Expression As string
Console.Write("Enter an expression (Ex: x^2 * 10): ")
Expression = Console.ReadLine()

Dim ParsedExpr = uc.Parse(Expression)

Dim x As double
for x = 1 To 10
   VariableX.SetVariableValue(x)
   Console.WriteLine("x = " + x + "  Result = " + ParsedExpr.Evaluate())
Next

' Output
' x = 1  Result = 10
' x = 2  Result = 40
' x = 3  Result = 90
' x = 4  Result = 160
' x = 5  Result = 250
' x = 6  Result = 360
' x = 7  Result = 490
' x = 8  Result = 640
' x = 9  Result = 810
' x = 10  Result = 1000

ParsedExpr.Release()
VariableX.Release()

          

var VariableX = uc.DefineVariable("x");
string Expression;
Console.Write("Enter an expression (Ex: x^2 * 10): ");
Expression = Console.ReadLine();

var ParsedExpr = uc.Parse(Expression);

for (double x = 1; x <= 10; x = x + 1) {
   VariableX.SetVariableValue(x);
   Console.WriteLine("x = " + x + "  Result = " + ParsedExpr.Evaluate());
}

// Output
// x = 1  Result = 10
// x = 2  Result = 40
// x = 3  Result = 90
// x = 4  Result = 160
// x = 5  Result = 250
// x = 6  Result = 360
// x = 7  Result = 490
// x = 8  Result = 640
// x = 9  Result = 810
// x = 10  Result = 1000

ParsedExpr.Release();
VariableX.Release();

          

//var VariableX = uc.DefineVariable('x');
      Write('Enter an expression (Ex: x^2 * 10): ')
Expression = ReadLn();

//var ParsedExpr = uc.Parse(Expression);

      for x := 1 to 10 do
begin
   VariableX.SetVariableValue(x);
         WriteLn('x = ' + x + '  Result = ' + ParsedExpr.Evaluate());
End;

      // Output
// x = 1  Result = 10
// x = 2  Result = 40
// x = 3  Result = 90
// x = 4  Result = 160
// x = 5  Result = 250
// x = 6  Result = 360
// x = 7  Result = 490
// x = 8  Result = 640
// x = 9  Result = 810
// x = 10  Result = 1000

      ParsedExpr.Release();
      VariableX.Release();

          

auto VariableX = uc.DefineVariable("x");
string Expression;      
cout << "Enter an expression (Ex: x^2 * 10): " << endl;
cin >> Expression; // Ex: x^2 * 10

auto ParsedExpr = uc.Parse(Expression);

for (double x = 1; x <= 10; x = x + 1) {
   VariableX.SetVariableValue(x);
   cout << "x = " << x << "  Result = " << ParsedExpr.Evaluate() << endl;
}

// Output
// x = 1  Result = 10
// x = 2  Result = 40
// x = 3  Result = 90
// x = 4  Result = 160
// x = 5  Result = 250
// x = 6  Result = 360
// x = 7  Result = 490
// x = 8  Result = 640
// x = 9  Result = 810
// x = 10  Result = 1000

ParsedExpr.Release();
VariableX.Release();

          

auto VariableX = uc.DefineVariable("x");
string ^ Expression;
Console::Write("Enter an expression (Ex: x^2 * 10): ");
Expression = Console::ReadLine();

auto ParsedExpr = uc.Parse(Expression);

for (double x = 1; x <= 10; x = x + 1) {
   VariableX.SetVariableValue(x);
   Console::WriteLine("x = " + x + "  Result = " + ParsedExpr.Evaluate());
}

// Output
// x = 1  Result = 10
// x = 2  Result = 40
// x = 3  Result = 90
// x = 4  Result = 160
// x = 5  Result = 250
// x = 6  Result = 360
// x = 7  Result = 490
// x = 8  Result = 640
// x = 9  Result = 810
// x = 10  Result = 1000

ParsedExpr.Release();
VariableX.Release();

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="Evaluate")> _

Private Function Evaluate__(ByVal ExprHandle As IntPtr) As  Double
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="Evaluate")]

protected static extern  double Evaluate_(IntPtr ExprHandle);
            
{DLLImport}function Evaluate__(ExprHandle: System.Pointer):  Double; cdecl; external uCalcDLL name 'Evaluate';

            
typedef  double (* __Evaluate)(void *ExprHandle); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "Evaluate")]

static double Evaluate_(void *  ExprHandle);
            
See also
Prev | Next