gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:23:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com