AntonovAnton / math.evaluation

This .NET library allows you to evaluate and compile any mathematical expression from a string dynamically at runtime. It supports a wide range of operations and allows for the use of custom variables, operators, and functions. The evaluator can be configured for different contexts, such as scientific, programming, boolean math expressions.
Other
37 stars 2 forks source link

Error of evaluating the expression. 'Infinity' is not recognizable. Invalid token at position 0 #47

Closed Bykiev closed 2 months ago

Bykiev commented 2 months ago

Describe the bug

Error of evaluating the expression. 'Infinity' is not recognizable. Invalid token at position 0

To Reproduce

Steps to reproduce the issue:

string expr = (double.MaxValue + 2e+300).ToString(CultureInfo.InvariantCulture);
Console.WriteLine(expr.Evaluate(CultureInfo.InvariantCulture));

Expected behavior

The result of expression evaluation should be Double.PositiveInfinity

Actual Behavior

Exception is thrown

AntonovAnton commented 2 months ago

It will work if you are evaluating C# math expression as a string: Console.WriteLine("double.MaxValue + 2e+300".Evaluate(new DotNetStandartMathContext()));

AntonovAnton commented 2 months ago

If you want the evaluator understands "Invinity" string as a PositiveInfinity you can specify context:

            var context = new MathContext();
            context.BindVariable(double.PositiveInfinity, "Infinity");
            string expr = (double.MaxValue + 2e+300).ToString(CultureInfo.InvariantCulture);
            Console.WriteLine(expr.Evaluate(context));
AntonovAnton commented 2 months ago

Describe the bug

Error of evaluating the expression. 'Infinity' is not recognizable. Invalid token at position 0

To Reproduce

Steps to reproduce the issue:

string expr = (double.MaxValue + 2e+300).ToString(CultureInfo.InvariantCulture);
Console.WriteLine(expr.Evaluate(CultureInfo.InvariantCulture));

Expected behavior

The result of expression evaluation should be Double.PositiveInfinity

Actual Behavior

Exception is thrown

Hi! The evaluator returns double not a string, ToString() of the double type represents double.PositiveInfinity as "Infinity" or "∞" so I can add support of these notations to the DotNetStandartMathContext if you use this context the expression will evaluate "Infinity" or "∞" as double.PositiveInfinity. Do you agree to use the DotNetStandartMathContext? Without specified math context the evaluator supports only +-*/(). Also ScientificMathContext supports "∞" notation at the moment.