Is there any way to make sure the floating point arithmetic result the same in both linux and windows

前端 未结 2 391
梦如初夏
梦如初夏 2021-02-06 12:42

My programe runs both in linux and windows, I have to make sure the floating point arithmetic get the same result in different OS.

Here is the code:

for          


        
2条回答
  •  时光取名叫无心
    2021-02-06 13:34

    The precise results of your code are not fully defined by the IEEE and C/C++ standards. That is the source of the problem.

    The main problem is that while all of your inputs are floats that does not mean that the calculation must be done at float precision. The compiler can decide to use double-precision for all intermediate values if it wants to. This tends to happen automatically when compiling for x87 FPUs, but the compiler (VC++ 2010, for instance) can do this expansion explicitly if it wants to even when compiling SSE code.

    This is not well understood. I shared my understanding of this a few years ago here:

    http://randomascii.wordpress.com/2012/03/21/intermediate-floating-point-precision/

    Some compilers let you specify the intermediate precision. If you can force all compilers to use the same intermediate precision then your results should be consistent.

提交回复
热议问题