Signed null linux vs windows

I am running a program in C ++ for windows and linux. The output must be identical. I try to ensure that the only differences are real differences opposed to working changes. so far I have taken care of all the differences that may be caused by the differences \ r \ n but there is one thing that I cannot understand.

there is 0.000 in windows out, and -0.000 in linux

Does anyone know what that means, what makes the difference?

thanks

+6
source share
3 answers

Since in IEEE floating point format, the sign bit is separate from the value, you have two different values โ€‹โ€‹of 0, positive and negative. In most cases, this does not matter; both zeros are compared equal, and they really describe the same mathematical value (mathematically, 0 and -0 are the same). Where the difference can be significant when you have a flaw, and you need to know if the overflow has occurred with a positive or negative value. Also, if you divide by 0, the infinity sign you get depends on the sign 0 (i.e. 1 / + 0.0 gives + Inf, but 1 / -0.0 gives -Inf). In other words, most likely, it will not affect you.

Please note, however, that a different conclusion does not necessarily mean that the number itself is different. It is possible that the value in Windows is also -0.0, but the output procedure in Windows does not distinguish between +0.0 and -0.0 (they are compared anyway).

+1
source

This is probably due to differences in how the optimizer optimizes some FP calculations (which can be customized - see, for example, here ); in one case, you get a value slightly less than 0, and in the other, a little more. Both pins are rounded to 0.000 , but they retain their "real" sign.

+7
source

If (insecure) flags are used, such as -ffast-math , the compiler is limited in the assumptions it can make when it "optimizes" the IEEE-754 arithmetic. First check that both platforms use the same rounding.

Also, if possible, check that they are using the same floating point block. i.e. SSE vs FPU on x86. The latter may be a problem with the implementation of the functions of the mathematical library - especially trigonometric / transcendental functions.

+1
source

Source: https://habr.com/ru/post/903044/


All Articles