Search code examples
vb.netncalc

Ncalc formula evaluates to unexpected result


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.


Solution

  • 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.