浮点比较 - 不同运行之间的结果

发布于 2024-09-25 07:06:53 字数 125 浏览 0 评论 0原文

我知道我无法在 C++/C 上比较两个浮点数或双精度数的绝对相等。如果出于某种原因,我编写了一个使用绝对相等的 if 条件,是否可以保证 if 条件在程序的不同运行中对相同数据返回相同的结果?或者它纯粹是不确定的并且结果可能会有所不同?

I know that I can not compare two floating point or double numbers for absolute equality on C++/C. If for some reason, I write a if condition which uses the absolute equality, is it guaranteed that the if condition will return the same result on different runs of the program for same data? Or it is purely non-deterministic and the result can vary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

£冰雨忧蓝° 2024-10-02 07:06:53

对于同一台 PC 上的相同编译二进制文件,结果应该是相同的。如果您使用其他编译器或其他 PC,结果可能会有所不同。

For the same compiled binary and on the same PC, results should be the same. If you use another compiler or another PC, results may vary.

浸婚纱 2024-10-02 07:06:53

我曾经进行过一个单元测试,在具有 Intel CPU 的机器上失败,但在 AMD 上运行正常。四舍五入可能存在一些差异,并且测试或多或少符合通过/失败标准。

但我不会因此而在你的代码中到处乱扔过于复杂的测试。

I once had a unit test that failed on a machine with an Intel CPU but worked OK on an AMD. There was probably some difference in rounding off, and the test moreorless hit the pass/fail criterion head on.

But I would not litter your code with overcomplex tests everywhere just because of that.

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