2

我正在尝试在我的应用程序中使用 nCalc,但遇到了问题,无论出于何种原因它想要转换为 UInt16 并导致错误。

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

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

这抛出;

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: 
4

1 回答 1

2

在 NCalc 表达式中,^是按位 XOR 运算符,它接受两个无符号 16 位整数操作数。在您的表达式中,NCalc 尝试转换-.503937为 UInt16 值,并OverflowException因为该数字小于零而被抛出。

如果要取幂,请改用该Pow函数:

string evalstring = "Pow(-.503937, 2)";
于 2016-07-23T21:00:50.020 回答