GCC在32位机器上以不同的方式编译程序
GCC compiles program differently in 32-bit machine
我遇到了这样一种情况,在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。coeff*(max-min)的结果可能是0.9999999…并且分配给int导致0。两个朋友在64位机器上尝试了相同的代码,结果是1。为什么它在32位上给出结果0?它可能与虚拟机有关吗?我的机器上的测试环境:
- 32位Linux Mint 17.2在VMWare 6.0.7上运行
- gcc 4.8.4
- 用于构建的命令:g++main.cpp-o main
我可以用gcc 4.8.4:在64位Ubuntu 14.04上重现这个问题
$ g++ -m32 main.c -o main && ./main
0
$ g++ -m64 main.c -o main && ./main
1
我相信@void_ptr是对的。这是由于x87内部使用80位数学,而SSE2使用64位。
如果使用-ffloat-store
编译32位可执行文件以强制64位浮点精度,就会发生这种情况:
$ 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.
在任何情况下,永远不要依赖浮点在数学上是准确的。
相关文章:
- 如何在c++中为模板函数实例创建快捷方式
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 创建引用向量的优雅方式
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 不同/较旧的处理器运行c++代码的方式是否不同
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在reactor中存储eventHandlers的最佳方式是什么
- 如何以优化的方式同时迭代两个间距不相等的数组
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 为什么C++有不同的变量初始化方式?
- 在AVX通道中混洗的最佳方式
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- 如何以静态代码分析友好的方式使用 #define 防护?
- GCC在32位机器上以不同的方式编译程序