Double 除以零返回除以零错误
我遇到了意想不到的行为,希望有人能够提供一些指导,指导重点调查哪些领域。
我有两种方法,一种本质上是对双精度数执行除以零测试,第二种方法为非托管 dll 调用 extern
方法。
注意: 在 .Net 运行时中,将 Double 除以零应该返回一个 Infinity 值(有趣的是正值或负值)。
我正在做的伪代码看起来像这样:
InfinityTest(); // Returns an Infinity value as expected
DllCall();
InfinityTest(); // Divide by zero error on second call.
第一次调用 InfinityTest() 按预期返回值 Infinity。第二次调用 InfinityTest() 会引发我没有预料到的除以零异常。
更新
下面的有效InfinityTest()
代码。为了简洁起见,我删除了 try/catch 元素等。我无权详细了解 DllCall()
伪代码元素,抱歉。
private double InfinityTest()
{
double a = 1.0;
int b = 0;
return a / b;
}
I am experiencing an unexpected behaviour and was hoping someone could help with some guidance as to what areas to focus an investigation on.
I have two methods, one essentially performs a divide by zero test on a double, the second calls an extern
method for an unmanaged dll.
Note: In the .Net runtime, dividing a Double by Zero should return an Infinity value (amusingly of either positive or negative flavours).
Pseudocode for what I am doing looks something like this:
InfinityTest(); // Returns an Infinity value as expected
DllCall();
InfinityTest(); // Divide by zero error on second call.
The first call to InfinityTest()
returns the value Infinity as expected. The second call to InfinityTest()
throws a Divide by Zero exception that I didn't expect.
Update
The effective InfinityTest()
code below. For brevity I've removed try/catch elements, etc. I do not have permission to go into details about the DllCall()
pseudocode element, apologies.
private double InfinityTest()
{
double a = 1.0;
int b = 0;
return a / b;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于听起来您的 DLL 正在更改您的 FP 状态字,因此您唯一的选择可能就是将其更改回来。我建议 P/Invoke 到
_clearfp
或_fpreset
。以下是他们的 P/Invoke 签名:这可能不会将事情重置回原来的样子,但希望它足够接近。
Since it sounds like your DLL is changing the FP status word on you, your only choice may be to change it back. I would suggest P/Invoke to
_clearfp
or_fpreset
. Here are their P/Invoke signatures:This may not reset things back to exactly the way they were, but hopefully it will be close enough.