How to test if float is NaN?
Don Clugston
dac at nospam.com.au
Fri May 19 06:23:11 PDT 2006
Lionello Lunesu wrote:
> xs0 wrote:
>> I think this should work as well:
>>
>> if (num == num) {
>> // not NaN
>> } else {
>> // NaN
>> }
>
> Indeed! This works just fine in VC on x86, but fails miserably on x64...
>
> In a release build the VC8 x64 compiler optimized that "if" to "if(1)" :D
>
> Here's the assembly from a debug build:
>
> inline bool _is_nan( double d ) { return !(d==d); }
> 0000000000404000 movsd mmword ptr [rsp+8],xmm0
> 0000000000404006 push rdi
> 0000000000404007 sub rsp,10h
> 000000000040400B mov rdi,rsp
> 000000000040400E mov rcx,4
> 0000000000404018 mov eax,0CCCCCCCCh
> 000000000040401D rep stos dword ptr [rdi]
> 000000000040401F movsd xmm0,mmword ptr [d]
> 0000000000404025 ucomisd xmm0,mmword ptr [d]
> 000000000040402B je citkTypes::_is_nan+36h (404036h)
> 000000000040402D mov dword ptr [rsp],1
> 0000000000404034 jmp citkTypes::_is_nan+3Dh (40403Dh)
> 0000000000404036 mov dword ptr [rsp],0
> 000000000040403D mov al,byte ptr [rsp]
> 0000000000404040 add rsp,10h
> 0000000000404044 pop rdi
> 0000000000404045 ret
>
> According to
> http://www.ews.uiuc.edu/~cjiang/reference/vc314.htm
> they should not have been checking for ZF,PF,CF==1,0,0, but the code
> only checks ZF?
>
> 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>
More information about the Digitalmars-d-learn
mailing list