Math.EvaluateExpression

EvaluateExpression function is used to evaluate a mathematical expression from a string. This is especially useful when a user is required to enter an equation.

Parameters:
  • expression: String, containing the mathematical expression
  • constants: Hashtable, contains the values of the variables (keys are Strings and values are Numbers)

Math.EvaluateExpression returns a Number.
An expression can contain numbers, operators, constants, and functions (case insensitive)
  • Supported operators: +, -, *, /, ^, % (note than the operator % have the same precedence as * and /)
  • Supported constants: PI, E
  • Supported functions: sqrt, log, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh (note that log is base ten)

Examples:
Screen.Write(Math.EvaluateExpression("5 + 1", Hashtable.New())); // output: "6"
var foo = Hashtable.New();
foo["x"] = 2;
var bar = Math.EvaluateExpression("2x - 1", foo); // output: "3"

Screen.Write(bar);
// uniform acceleration displacement calculator
var foo = Hashtable.New();
foo["xi"] = 50; // initial displacement
foo["vi"] = 2;  // initial velocity
foo["a"] = 1.6; // constant acceleration
foo["t"] = 30;  // time
var displacement = Math.EvaluateExpression("xi + vi*t + (1/2)a(t^2)", foo);

Screen.Write(displacement); // prints: "830"
// a formula that means nothing but makes use of both PI and a function
Screen.Write(Math.EvaluateExpression("2pi - sqrt(4)", Hashtable.New())); // prints: lengthy number
updated 21 months and one week ago by guest