sklose / NCalc2

expression evaluator for .NET with built-in compiler
MIT License
170 stars 58 forks source link

Wrong calculation #2

Closed AlexByte closed 7 years ago

AlexByte commented 7 years ago
var expr = new Expression("2747474 * 63663");       
Console.WriteLine(expr.Evaluate());

It prints -1 181 221 874, when the right answer is 174 912 437 262.

sklose commented 7 years ago

Integers are evaluated as 32 bit in NCalc. You might be able to get the correct result by using decimals instead ("2747474.0 * 63663.0") ... see the original NCalc's documentation: https://ncalc.codeplex.com/wikipage?title=values&referringTitle=Home

AlexByte commented 7 years ago

I believe that the computation should be performed in the checked statement to be able to catch exception. It would be convenient to have the option to always use decimal.

sklose commented 7 years ago

Since this is a breaking change it should probably be a setting on the EvaluationOptions. Unfortunately I won't have time to work on this, but I happily accept pull requests if you want to jump in.

MalukuSeito commented 7 years ago

I put in an option to enable checked math at least. Always use decimal seems a bit to weird. Also decimal can overflow as well, but will always throw an exception, even in unchecked, this just pushes the error further and not actually fixes it.