How to test if float is NaN?

Don Clugston dac at nospam.com.au
Fri May 26 13:41:55 PDT 2006


Walter Bright wrote:
> Don Clugston wrote:
>>> Since by definition a NaN is not equal to anything, including itself, 
>>> I suppose it's a compiler bug?
>>
>> Definitely. I've seen that sort of behaviour before in VC++.
>> <cynic> Does anyone at Microsoft understand floating point arithmetic? 
>> </cynic>
> 
> At least up until VC6, it did not check the parity bit for floating 
> point comparisons (the P bit is set if one of the operands is NaN). In 
> fact, I know of no C++ compiler other than DMC++ that does this correctly.


I've been bitten by that before. Incorrect NaN support coupled with 
uninitialized variables is a deadly combination. Because negative 
numbers fill the stack with 0xFF bytes, it's relatively common for 
uninitialized floating-point stack variables to intermittently be NaN in 
release mode. The most-difficult-to-track-down bug I've ever had was a 
consequence of this.

> 
> VC dropping support for 80 bit long doubles is not an encouraging sign.
> 
> This is typical for C++ compiler vendors. Handling of NaN arguments to 
> the math.h functions is routinely erratic and buggy. This can be traced 
> back to the Standards committee, who failed to specify NaN behavior.




More information about the Digitalmars-d-learn mailing list