Summary: Parameter use before its use in an AndAnd expression
                    with reals treats NaN as false
           Product: D
           Version: 2.041
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Aldo Nunez <> 2010-03-09 16:41:26 
PST ---
NaN is supposed to be treated as true when implicitly converted to bool,
because it's not the same as zero. But, in the following case, the NaN is
treated as false.

- optimized build
- a variable of type real with value NaN
- the variable is used in an AndAnd (&&) or OrOr (||) expression
- the variable is used before the AndAnd (&&) or OrOr (||) expression

I assume this happens anywhere a real is turned into a bool, but I haven't

Here's a sample program:

import std.stdio;

void Do( float t, real u )
    writeln( u );
    writeln( t && u );

void Do2( real t, float u )
    writeln( t );
    writeln( t && u );

void main( string[] args )
    Do( float.nan, real.nan );
    Do2( real.nan, float.nan );

In the function calls to both Do and Do2, the result is "false". It should be
"true". Commenting out "writeln( u )" and "writeln( t )" also make the results

Looking at the assembly code, I noticed that the cause seems to be an
instruction that's left out when the program is optimized.

Unoptimized (right):

00403C0D  fld         tbyte ptr [esp+8] 
00403C11  fldz             
00403C13  fucompp          
00403C15  fnstsw      ax   
00403C17  sahf             
00403C18  jne         00403C20 
00403C1A  jp          00403C20 

Optimized (wrong):

00403C1E  fld         tbyte ptr [esp+8] 
00403C22  fldz             
00403C24  fucompp          
00403C26  fnstsw      ax   
00403C28  sahf             
00403C29  jne         00403C2F 

The JP instruction is missing. As a result, we'll fall thru to where EAX is
cleared, and the result turns into false.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to