I discovered something new, that is interesting:
When I compile:
volatile const double result = 0.0/0.0;
This will result in “0x7FF8000000000000”
But:
volatile const double bla = 0.0;
volatile const double result = 0.0/bla;
Will result in “0xFFF8000000000000”
Both compiled with Clang of course!
In the first case (0.0/0.0) clang’s constant folder gets rid of the division and replaces it with clang’s default representation of NaN. In the second case (0.0/bla) because you’ve declared ‘bla’ as volatile we actually perform the division, so the NaN in that case comes from the processor.
-Andy
So MSVC’s setting the sign bit when it shouldn’t, report the bug to them.
Actually, it looks like MSVC always does the division. The sign bit doesn’t matter.
FWIW, gcc also does the division in the non-volatile 0.0/0.0 case because it is preserving the floating point status flag that gets set by this operation. If you pass -ffast-math or -fno-trapping-math to gcc it will constant fold (to the same value clang uses). I can’t get MSVC to do the constant folding, but I might just not know the right option.
It’s probably worth mentioning that clang doesn’t guarantee that floating point exception/status flag semantics will be preserved. That’s currently under development.
-Andy
Hello Andy,
Thank you for the comparison and the explanation of the effect. I’m still a beginner and I’m always happy to learn something new!
I understand now how the different values came to be and why they shouldn’t matter. Thank you all!
Kind greetings
Björn