小编hdb*_*ean的帖子

有没有办法确保浮点运算结果在linux和windows中都相同

我的程序在linux和windows中运行,我必须确保浮点运算在不同的OS中得到相同的结果.

这是代码:

for (int i = 0; i < 100000; ++i)
{
    float d_value = 10.0f / float(i);
    float p_value = 0.01f * float(i) + 100.0f;
}
Run Code Online (Sandbox Code Playgroud)

我使用" g ++ -m32 -c -static -g -O0 -ffloat-store "在linux中构建代码.我使用"/ fp:precise/O2"在vs2005的windows中构建代码.

当我打印"d_value"和"p_value"时,"d_value"在linux和windows中都是相同的.但有时候"p_value"会有所不同.例如,使用十六进制格式打印"p_value":

windows:  42d5d1eb
linux:    42d5d1ec
Run Code Online (Sandbox Code Playgroud)

为什么要发生这种情况

我的g ++版本是

Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc …
Run Code Online (Sandbox Code Playgroud)

linux windows math floating-point g++

5
推荐指数
1
解决办法
3107
查看次数

有没有办法确保不同操作系统中浮点的输出相同?

这是我的代码:

int a = 0x451998a0;
float b = *((float *)&a);
printf("coverto float: %f, %.10lf\n", b, b);
Run Code Online (Sandbox Code Playgroud)

在Windows中输出是:

coverto float: 2457.539063, 2457.5390625000
Run Code Online (Sandbox Code Playgroud)

在linux中,输出是:

coverto float: 2457.539062, 2457.5390625000
Run Code Online (Sandbox Code Playgroud)

有没有办法确保输出相同?

c++ linux windows floating-point io

4
推荐指数
1
解决办法
244
查看次数

标签 统计

floating-point ×2

linux ×2

windows ×2

c++ ×1

g++ ×1

io ×1

math ×1