Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道我无法在 C++/C 上比较两个浮点数或双精度数的绝对相等性。如果出于某种原因,我编写了一个使用绝对相等的 if 条件,是否可以保证 if 条件在程序的不同运行中为相同的数据返回相同的结果?或者它纯粹是不确定的,结果可能会有所不同?
对于相同的编译二进制文件和相同的 PC,结果应该是相同的。如果您使用其他编译器或其他 PC,结果可能会有所不同。
我曾经有一个单元测试在配备 Intel CPU 的机器上失败,但在 AMD 上运行良好。四舍五入可能存在一些差异,并且测试更直接地达到了通过/失败标准。
但是我不会因为这个原因而到处乱扔你的代码。