项目作者: miroiu

项目描述 :
Evaluates a math expression from a string. Supports variables and custom operators.
高级语言: C#
项目地址: git://github.com/miroiu/string-math.git
创建时间: 2020-01-06T13:22:37Z
项目社区:https://github.com/miroiu/string-math

开源协议:MIT License

下载


V3 README can be found here: https://github.com/miroiu/string-math/tree/release-3.0.0

NEW! Boolean math example: https://github.com/miroiu/string-math/pull/6/files

String Math NuGet Downloads .NET

Calculates the value of a math expression from a string returning a double.
Supports variables, user defined operators and expression compilation.

  1. double result = "1 * (2 - 3) ^ 2".Eval(); // 1

Variables

  1. double result = "{a} + 2 * {b}".Substitute("a", 2).Substitute("b", 3).Result; // 8

Global variables

These variables are inherited and cannot be substituted.

  1. MathExpr.AddVariable("PI", 3.1415926535897931);
  2. double result = "1 + {PI}".Eval(); // 4.1415926535897931

Custom operators

Global operators

These operators are inherited and can be overidden.

  1. MathExpr.AddOperator("abs", a => a > 0 ? a : -a);
  2. double result = "abs -5".Eval(); // 5
  3. // Operator precedence (you can specify an int for precedence)
  4. MathExpr.AddOperator("max", (a, b) => a > b ? a : b, Precedence.Power);
  5. double result = new MathExpr("2 * 3 max 4").Result; // 8

Local operators

These are applied only to the target expression.

  1. MathExpr expr = "{PI} + 1";
  2. expr.SetOperator("+", (a, b) => Math.Pow(a, b));
  3. double result = expr; // 3.1415926535897931
  4. double result2 = "{PI} + 1".Eval(); // 4.1415926535897931

Advanced

Extract variables

  1. var expr = "{a} + {b} + {PI}".ToMathExpr();
  2. var variables = expr.Variables; // { "a", "b", "PI" }
  3. var localVariables = expr.LocalVariables; // { "a", "b" }

Compilation

  1. Func<double, double> fn = "{a} + 2".ToMathExpr().Compile("a");
  2. double result = fn(5); // 7

Conditional substitution

  1. MathExpr expr = "1 / {a}".Substitute("a", 1);
  2. double temp = expr.Result; // 1
  3. if (someCondition) // true
  4. expr.Substitute("a", 2);
  5. double final = expr.Result; // 0.5

Sharing math context

  1. MathExpr expr = "{PI} + 1";
  2. expr.SetOperator("+", (a, b) => Math.Pow(a, b));
  3. MathExpr expr2 = "3 + 2".ToMathExpr(expr.Context);
  4. double result = "1 + 2 + 3".Eval(expr.Context);

Custom math context

  1. var context = new MathContext(); // new MathContext(MathContext.Default); // to inherit from global
  2. context.RegisterBinary("+", (a, b) => Math.Pow(a, b));
  3. MathExpr expr = new MathExpr("{PI} + 1", context);
  4. MathExpr expr2 = "3 + 2".ToMathExpr(context);
  5. double result = "1 + 2 + 3".Eval(context);

Default operators

Binary

  1. + (addition)
  2. - (subtraction)
  3. * (multiplication)
  4. / (division)
  5. % (remainder)
  6. ^ (power)
  7. log (logarithm)
  8. max (maximum)
  9. min (minimum)

Unary

  1. - (negation)
  2. ! (factorial)
  3. sqrt (square root)
  4. sin (sine)
  5. asin (arcsine)
  6. cos (cosine)
  7. acos (arccosine)
  8. tan (tangent)
  9. atan (arctangent)
  10. rad (convert degrees to radians)
  11. deg (convert radians to degrees)
  12. ceil (ceiling)
  13. floor (floor)
  14. round (rounding)
  15. exp (e raised to power)
  16. abs (absolute)