EvalMethod

Applies to:Fast Math Parser
Class:uCalc
Parses and evaluates an expression in one step
Syntax
Eval(Expression)
Parameters
Expression
String
Expression to be evaluated
Returns
Type: double
Parses and evaluates an expression in one step
Remarks
This takes a math expression and returns a numeric result.
Example 1: Evaluating expressions

        Console.WriteLine(uc.Eval("1+1")) ' Returns 2
        Console.WriteLine(uc.Eval("5*(3+9)^2")) ' Returns 720     
        Console.WriteLine(uc.Eval("Length('This is a test')")) ' Returns 14

          

            Console.WriteLine(uc.Eval("1+1")); // Returns 2;
            Console.WriteLine(uc.Eval("5*(3+9)^2")); // Returns 720;     
            Console.WriteLine(uc.Eval("Length('This is a test')")); // Returns 14;

          

      WriteLn(uc.Eval('1+1')); // Returns 2;
      WriteLn(uc.Eval('5*(3+9)^2')); // Returns 720;     
      WriteLn(uc.Eval('Length("This is a test")')); // Returns 14;

          

    cout << uc.Eval("1+1") << endl; // Returns 2;
    cout << uc.Eval("5*(3+9)^2") << endl; // Returns 720;     
    cout << uc.Eval("Length('This is a test')") << endl; // Returns 14;

          

        Console::WriteLine(uc.Eval("1+1")); // Returns 2;
        Console::WriteLine(uc.Eval("5*(3+9)^2")); // Returns 720;     
        Console::WriteLine(uc.Eval("Length('This is a test')")); // Returns 14;

          
Example 2: Performing a summation; a return value is needed only at the very end

        Dim VariableX = uc.DefineVariable("x")
        Dim Total = uc.DefineVariable("Total")
        Dim Expression As string
        Console.Write("Enter an expression (Ex: Total = Total + x^2 + 5): ")
        Expression = Console.ReadLine()
        Dim ParsedExpr = uc.Parse(Expression)

        Dim x As double
        for x = 1 To 10
            VariableX.SetVariableValue(x)
            ParsedExpr.Execute()
            Console.WriteLine(uc.Eval("x") + "   Sub total = " + uc.Eval("Total"))
        Next
        ' Output
        ' 1   Sub total = 6
        ' 2   Sub total = 15
        ' 3   Sub total = 29
        ' 4   Sub total = 50
        ' 5   Sub total = 80
        ' 6   Sub total = 121
        ' 7   Sub total = 175
        ' 8   Sub total = 244
        ' 9   Sub total = 330
        ' 10   Sub total = 435

          

            var VariableX = uc.DefineVariable("x");
            var Total = uc.DefineVariable("Total");
            string Expression;
            Console.Write("Enter an expression (Ex: Total = Total + x^2 + 5): ");
            Expression = Console.ReadLine();
            var ParsedExpr = uc.Parse(Expression);

            for (double x = 1; x <= 10; x = x + 1)
            {
                VariableX.SetVariableValue(x);
                ParsedExpr.Execute();
                Console.WriteLine(uc.Eval("x") + "   Sub total = " + uc.Eval("Total"));
            }
            // Output
            // 1   Sub total = 6
            // 2   Sub total = 15
            // 3   Sub total = 29
            // 4   Sub total = 50
            // 5   Sub total = 80
            // 6   Sub total = 121
            // 7   Sub total = 175
            // 8   Sub total = 244
            // 9   Sub total = 330
            // 10   Sub total = 435

          

//var VariableX = uc.DefineVariable('x');
//var Total = uc.DefineVariable('Total');
      Write('Enter an expression (Ex: Total = Total + x^2 + 5): ')
Expression = ReadLn();
//var ParsedExpr = uc.Parse(Expression);

      for x := 1 to 10 do
begin
   VariableX.SetVariableValue(x);
         ParsedExpr.Execute();
         WriteLn(uc.Eval('x') + '   Sub total = ' + uc.Eval('Total'));
End;
      // Output
// 1   Sub total = 6
// 2   Sub total = 15
// 3   Sub total = 29
// 4   Sub total = 50
// 5   Sub total = 80
// 6   Sub total = 121
// 7   Sub total = 175
// 8   Sub total = 244
// 9   Sub total = 330
// 10   Sub total = 435

          

    auto VariableX = uc.DefineVariable("x");
    auto Total = uc.DefineVariable("Total");
    string Expression;      
    cout << "Enter an expression (Ex: Total = Total + x^2 + 5): " << endl;
    cin >> Expression; // Ex: Total = Total + x^2 + 5
    auto ParsedExpr = uc.Parse(Expression);

    for (double x = 1; x <= 10; x = x + 1) {
        VariableX.SetVariableValue(x);
        ParsedExpr.Execute();
        cout << uc.Eval("x") << "   Sub total = " << uc.Eval("Total") << endl;
    }
    // Output
    // 1   Sub total = 6
    // 2   Sub total = 15
    // 3   Sub total = 29
    // 4   Sub total = 50
    // 5   Sub total = 80
    // 6   Sub total = 121
    // 7   Sub total = 175
    // 8   Sub total = 244
    // 9   Sub total = 330
    // 10   Sub total = 435

          

        auto VariableX = uc.DefineVariable("x");
        auto Total = uc.DefineVariable("Total");
        string ^ Expression;
        Console::Write("Enter an expression (Ex: Total = Total + x^2 + 5): ");
        Expression = Console::ReadLine();
        auto ParsedExpr = uc.Parse(Expression);

        for (double x = 1; x <= 10; x = x + 1) {
            VariableX.SetVariableValue(x);
            ParsedExpr.Execute();
            Console::WriteLine(uc.Eval("x") + "   Sub total = " + uc.Eval("Total"));
        }
        // Output
        // 1   Sub total = 6
        // 2   Sub total = 15
        // 3   Sub total = 29
        // 4   Sub total = 50
        // 5   Sub total = 80
        // 6   Sub total = 121
        // 7   Sub total = 175
        // 8   Sub total = 244
        // 9   Sub total = 330
        // 10   Sub total = 435

          
Example 3: Defining various items

        uc.Define("Function: f(x, y) = x + y")
        uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b")
        uc.Define("Variable: MyVar = 123")
        uc.Define("Lock ~~ Variable: Pi = 3.14") ' like uc.DefineConstant
        uc.Define("Syntax: aa ::= bb")

        Console.WriteLine(uc.Eval("f(10, 20) + MyVar")) ' Returns 153
        Console.WriteLine(uc.Eval("Pi")) ' Returns 3.14
        Console.WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")) ' Returns "Hello World!"
        Console.WriteLine(uc.Expand("aa, bb, cc")) ' Returns "bb, bb, cc"

          

            uc.Define("Function: f(x, y) = x + y");
            uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
            uc.Define("Variable: MyVar = 123");
            uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
            uc.Define("Syntax: aa ::= bb");

            Console.WriteLine(uc.Eval("f(10, 20) + MyVar")); // Returns 153;
            Console.WriteLine(uc.Eval("Pi")); // Returns 3.14;
            Console.WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")); // Returns "Hello World!";
            Console.WriteLine(uc.Expand("aa, bb, cc")); // Returns "bb, bb, cc";

          

      uc.Define('Function: f(x, y) = x + y');
      uc.Define('Operator: {a As String} CONCAT {b As String} As String = a + b');
      uc.Define('Variable: MyVar = 123');
      uc.Define('Lock ~~ Variable: Pi = 3.14'); // like uc.DefineConstant
      uc.Define('Syntax: aa ::= bb');
      
      WriteLn(uc.Eval('f(10, 20) + MyVar')); // Returns 153;
      WriteLn(uc.Eval('Pi')); // Returns 3.14;
      WriteLn(uc.EvalStr('"Hello " CONCAT "World!"')); // Returns 'Hello World!';
      WriteLn(uc.Expand('aa, bb, cc')); // Returns 'bb, bb, cc';

          

    uc.Define("Function: f(x, y) = x + y");
    uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
    uc.Define("Variable: MyVar = 123");
    uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
    uc.Define("Syntax: aa ::= bb");

    cout << uc.Eval("f(10, 20) + MyVar") << endl; // Returns 153;
    cout << uc.Eval("Pi") << endl; // Returns 3.14;
    cout << uc.EvalStr("'Hello ' CONCAT 'World!'") << endl; // Returns "Hello World!";
    cout << uc.Expand("aa, bb, cc") << endl; // Returns "bb, bb, cc";

          

        uc.Define("Function: f(x, y) = x + y");
        uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
        uc.Define("Variable: MyVar = 123");
        uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
        uc.Define("Syntax: aa ::= bb");

        Console::WriteLine(uc.Eval("f(10, 20) + MyVar")); // Returns 153;
        Console::WriteLine(uc.Eval("Pi")); // Returns 3.14;
        Console::WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")); // Returns "Hello World!";
        Console::WriteLine(uc.Expand("aa, bb, cc")); // Returns "bb, bb, cc";

          
Example 4: Setting variables of any data type

        Dim Int32Var = uc.DefineVariable("Int32Var As Int32")
        Dim ByteVar = uc.DefineVariable("ByteVar As Byte")
        Dim StrVar = uc.DefineVariable("StrVar As String")
        Dim SngVar = uc.DefineVariable("SngVar As Single")

        Int32Var.SetVariableValueAny("4.25") ' Will be converted to integer
        ByteVar.SetVariableValueAny("-1") ' Will be converted to unsigned byte
        StrVar.SetVariableValueAny("'Test'")
        SngVar.SetVariableValueAny("1.25")

        Console.WriteLine(uc.Eval("Int32Var")) ' Returns 4
        Console.WriteLine(uc.Eval("ByteVar")) ' Returns 255
        Console.WriteLine(uc.EvalStr("StrVar")) ' Returns "Test"
        Console.WriteLine(uc.EvalStr("SngVar")) ' Returns 1.25

          

            var Int32Var = uc.DefineVariable("Int32Var As Int32");
            var ByteVar = uc.DefineVariable("ByteVar As Byte");
            var StrVar = uc.DefineVariable("StrVar As String");
            var SngVar = uc.DefineVariable("SngVar As Single");

            Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
            ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
            StrVar.SetVariableValueAny("'Test'");
            SngVar.SetVariableValueAny("1.25");

            Console.WriteLine(uc.Eval("Int32Var")); // Returns 4;
            Console.WriteLine(uc.Eval("ByteVar")); // Returns 255;
            Console.WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
            Console.WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

          

//var Int32Var = uc.DefineVariable('Int32Var As Int32');
//var ByteVar = uc.DefineVariable('ByteVar As Byte');
//var StrVar = uc.DefineVariable('StrVar As String');
//var SngVar = uc.DefineVariable('SngVar As Single');

      Int32Var.SetVariableValueAny('4.25'); // Will be converted to integer
      ByteVar.SetVariableValueAny('-1');    // Will be converted to unsigned byte
      StrVar.SetVariableValueAny('"Test"');
      SngVar.SetVariableValueAny('1.25');

      WriteLn(uc.Eval('Int32Var')); // Returns 4;
      WriteLn(uc.Eval('ByteVar')); // Returns 255;
      WriteLn(uc.EvalStr('StrVar')); // Returns 'Test';
      WriteLn(uc.EvalStr('SngVar')); // Returns 1.25;

          

    auto Int32Var = uc.DefineVariable("Int32Var As Int32");
    auto ByteVar = uc.DefineVariable("ByteVar As Byte");
    auto StrVar = uc.DefineVariable("StrVar As String");
    auto SngVar = uc.DefineVariable("SngVar As Single");

    Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
    ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
    StrVar.SetVariableValueAny("'Test'");
    SngVar.SetVariableValueAny("1.25");

    cout << uc.Eval("Int32Var") << endl; // Returns 4;
    cout << uc.Eval("ByteVar") << endl; // Returns 255;
    cout << uc.EvalStr("StrVar") << endl; // Returns "Test";
    cout << uc.EvalStr("SngVar") << endl; // Returns 1.25;

          

        auto Int32Var = uc.DefineVariable("Int32Var As Int32");
        auto ByteVar = uc.DefineVariable("ByteVar As Byte");
        auto StrVar = uc.DefineVariable("StrVar As String");
        auto SngVar = uc.DefineVariable("SngVar As Single");

        Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
        ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
        StrVar.SetVariableValueAny("'Test'");
        SngVar.SetVariableValueAny("1.25");

        Console::WriteLine(uc.Eval("Int32Var")); // Returns 4;
        Console::WriteLine(uc.Eval("ByteVar")); // Returns 255;
        Console::WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
        Console::WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

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

Private Function Eval__(ByVal uCalcHandle As IntPtr, ByVal Expression As String) As Double
End Function
            
[DllImport(uCalcDLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Eval")]

protected static extern double Eval_(IntPtr uCalcHandle, string Expression);
            
{DLLImport}function Eval__(uCalcHandle: System.Pointer;Expression: AnsiString):  Double; cdecl; external uCalcDLL name 'Eval';

            
typedef  double (* __Eval)(void *uCalcHandle, CONSTCHAR Expression ); 

            
[DllImport(uCalcDLL, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "Eval")]

static double Eval_(void *  uCalcHandle, MARSHALSTR Expression);
            
See also
Prev | Next