0

I have a formula to be evaluated "6*6*6*6*6*6*5*6*6*6*5*6*5" Ncalc returns = -1031662592 when the answer should be (I expect to be) = 7558272000

the code is fairly straight forward

 Function StraightEval(Formula As String)
    Try
        Dim X As New Expression(Formula)
        Dim result = X.Evaluate
        Return result
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return "Error " & ex.Message
    End Try

End Function

Any ideas as to the discrepancy and how to fix it ? I know I could code my own little function that splits the string and does the math in a loop but I just though I ask the question first.

user1500403
  • 551
  • 8
  • 32

1 Answers1

1

My idea is that setting result = 6*6*6*6*6*6*5*6*6*6*5*6*5 causes an overflow of its limit 2,147,483,647 since it seems to default to integer, with the result being interpreted as a negative number.

I believe that

Function StraightEval(Formula As String)
    Try
        Dim X As New Expression(Formula)
        Dim result as Long = X.Evaluate
        Return result
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return "Error " & ex.Message
    End Try

End Function

will fix it.

Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23