mathexpressionncalc

NCalc expression.evaluate output different from when I compute the expression manually


Here is my code:

Dim ex As String = "(((105 * 4000) * 0.20) - ((0 + 10000) * (105 * 4000)) / ((105 * 4000) + Round((52 * 18192.31),0))) / 24"
Dim x As New Expression(ex)
Dim result As Decimal = x.Evaluate
Console.WriteLine(result.ToString("N"))

it returns: 3,502.90, but when i compute it manually and thru excel the output I get is: 3371.888737

Here is the image of my manual computation...

Manual Computation


Solution

  • 3,502.90 * 24 = 84089.6

    It is bigger than the first addend. And the second should be positive.

    But you have overflow of 32-bit integer arithmetics in this expression

    (0 + 10000) * (105 * 4000) = 4 200 000 000 > 2^31-1
    

    That is why the second addend becomes negative (and a1-(a2) > a1)

    Probably it is enough to use doubles like this:

      (0.0 + 10000) * (105.0 * 4000)
    

    P.S. You don't execute rounding in manual writing