2016-07-23 31 views
1

我想在我的應用程序中使用nCalc,但遇到了問題,它想要轉換爲UInt16出於任何原因並導致錯誤。使用ToUInt16()停止nCalc?

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: 

回答

1

在NCalc表達,^是按位XOR運算符,它接受兩個16位無符號整數的操作數。在您的表達式中,NCalc嘗試將-.503937轉換爲UInt16值,並且由於該數字小於零而引發了OverflowException

如果你想冪,使用Pow函數:

string evalstring = "Pow(-.503937, 2)";