Large negative number returned instead of correct result - code changed to fix.

Topics: General, Help
Feb 7, 2013 at 11:51 PM
Hello, First this is a great tool and i hope to use it frequently, and as such have made a small change to the source on our end that may help others.

While I'm a long time .NET/C# programmer it's not my "day job" and i'm mostly self-taught. So i cause more damage often than i solve... that being said..

the following expression: 9999999*23563333+3/3 would fail and return a large negative number. with version 1.3.8.0

Sample code for testing:
Expression eg = new Expression(tbFormula.Text,EvaluateOptions.IgnoreCase | EvaluateOptions.NoCache);
var result = eg.Evaluate();
rtbResult.Text = result.ToString();

By updating NCalcParser.cs line 2576 the result returns the correct positive number
instead of int.Parse in the try{} having Int64.Parse solved the issue.

It seems it's not throwing the overflow exception that you would expect. i didn't trace or debug much so I am missing the WHY but with a deadline looming, solving the "what" will have to do tonight. Would love for someone to explain this in a comment below.

NCalcParser.cs Line 2576 now reads
try { retval.value = new ValueExpression(Int64.Parse((INTEGER35!=null?INTEGER35.Text:null))); } catch(System.OverflowException) { retval.value = new ValueExpression(long.Parse((INTEGER35!=null?INTEGER35.Text:null))); }

in our implementation. Hope this helps someone else out there! Thanks again.
Apr 15, 2013 at 8:46 AM
Hi,

I can't help you to resolve the overflow problem but I can say why you don't get the overflow exception.

You must compile the project with the checked (http://msdn.microsoft.com/en-us/library/khy08726(v=vs.100).aspx) option (globally set it in the project/properties/Build/Advanced check the "Check for arithmetic overflow/underflow" option.

Now you get the overflow exceptions in EvaluationVisitor.cs but I don't know how to elevate the expression to UInt64 :-(