Double 除以零返回除以零错误

发布于 2024-11-02 04:52:16 字数 711 浏览 0 评论 0原文

我遇到了意想不到的行为,希望有人能够提供一些指导,指导重点调查哪些领域。

我有两种方法,一种本质上是对双精度数执行除以零测试,第二种方法为非托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-11-09 04:52:16

由于听起来您的 DLL 正在更改您的 FP 状态字,因此您唯一的选择可能就是将其更改回来。我建议 P/Invoke 到 _clearfp_fpreset。以下是他们的 P/Invoke 签名:

    [DllImport("msvcrt.dll")]
    static extern UInt32 _clearfp();
    [DllImport("msvcrt.dll")]
    static extern void _fpreset();

这可能不会将事情重置回原来的样子,但希望它足够接近。

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:

    [DllImport("msvcrt.dll")]
    static extern UInt32 _clearfp();
    [DllImport("msvcrt.dll")]
    static extern void _fpreset();

This may not reset things back to exactly the way they were, but hopefully it will be close enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文