我遇到的情况是,在32位机器上编译的相同程序给出的结果不同于在64位机器上编译它的结果。 一块代码是这样的
#include <iostream> int main(int argc, char *argv[]) { int index = 1; int total = 21; int min = 79; int max = 100; double coef = index / (double)total; int ret1 = coef * (max - min); std::cout << ret1 << "\n"; return 0; }
我期望结果为1,但在32位Linux上,我得到的结果为0.可能coef *(max – min)的结果是0.9999999 …并分配给int导致为0.两个朋友尝试相同的代码在64位机器上,他们得到的结果为1.为什么它给结果0在32位? 可能与虚拟机有关吗? 在我的机器上testing环境:
我可以重现与64位Ubuntu 14.04与gcc 4.8.4的问题:
$ g++ -m32 main.c -o main && ./main 0 $ g++ -m64 main.c -o main && ./main 1
我相信@void_ptr是正确的。 这是由x87内部使用80bit数学引起的,而SSE2使用64bit引起的。
如果使用-ffloat-store
编译32位可执行文件以强制执行64位浮点精度, -ffloat-store
发生这种情况:
$ g++ -m32 -ffloat-store main.c -o main && ./main 1
以下是man gcc
对此问题所说的话:
-ffloat-store Do not store floating-point variables in registers, and inhibit other options that might change whether a floating-point value is taken from a register or memory. This option prevents undesirable excess precision on machines such as the 68000 where the floating registers (of the 68881) keep more precision than a "double" is supposed to have. Similarly for the x86 architecture. For most programs, the excess precision does only good, but a few programs rely on the precise definition of IEEE floating point. Use -ffloat-store for such programs, after modifying them to store all pertinent intermediate computations into variables.
无论如何, 永远不要依赖浮点数学上的精确 。