c# - 阻止 nCalc 使用 ToUInt16()?

标签 c# ncalc

我正在尝试在我的应用程序中使用 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: 

最佳答案

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

如果你想求幂,请改用 Pow 函数:

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

关于c# - 阻止 nCalc 使用 ToUInt16()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38545490/

相关文章:

c# - 如何以编程方式将几个 JPEG 组合成一个更大的无损 wav JPEG (.net)

c# - 搜索单个结果时与 Entity Framework 一起使用的集合

c# - 链接 : warning AL1073: Referenced assembly 'mscorlib.dll' targets a different processor

c# - 使用 swashbuckle 将摘要或供应商扩展添加到 swagger 中的路径

c# - 如何向 Ncalc 添加新函数

c# - 我可以有一个不可变的 IEnumerable<> 吗?

c# - NCalc:有其他选择吗?

c# - NCalc 指定类型

.net - 在 NCalc 中为什么 'if (12 > 8)' 解析为 false?