3

I am trying to use nCalc in my application but am running into issues with it wanting to convert to UInt16 for whatever reason and causing an error.

string evalstring = "-.503937 ^ 2";
Expression e = new Expression(evalstring);

this.Value = Convert.ToDouble(e.Evaluate());

This throws;

System.OverflowException occurred
  HResult=-2146233066
  Message=Value was either too large or too small for a UInt16.
  Source=mscorlib
  StackTrace:
       at System.Convert.ToUInt16(Int32 value)
  InnerException: 
Wobbles
  • 3,033
  • 1
  • 25
  • 51

1 Answers1

3

In an NCalc expression, ^ is the bitwise XOR operator, which accepts two unsigned 16-bit integer operands. In your expression, NCalc attempts to convert -.503937 to a UInt16 value, and OverflowException is thrown because the number is less than zero.

If you want exponentiation, use the Pow function instead:

string evalstring = "Pow(-.503937, 2)";
Michael Liu
  • 52,147
  • 13
  • 117
  • 150