develooper Front page | perl.perl6.language | Postings from October 2001

Re: EX3: $a == $b != NaN

Thread Previous | Thread Next
From:
Damian Conway
Date:
October 6, 2001 19:27
Subject:
Re: EX3: $a == $b != NaN
Message ID:
200110070227.MAA50468@indy05.csse.monash.edu.au
 
   >     $a == $b != NaN
   > 
   > really means this:
   > 
   >     $a == $b && $b != NaN
   > 
   > But "$a == $b != NaN" is supposed to "[solve] the problem of numerical
   > comparisons between non-numeric strings."  Well, what if:
   > 
   >     $a = 'hello';
   >     $b = 0;
   > 
   > Doesn't that mean:
   > 
   >     "hello" == 0 && 0 != NaN
   > 
   > will evaluate to true?
   
No. The step you're missing is that the non-numeric string "hello",
when evaluated in a numeric context, produces NaN. So:

         "hello" == 0 && 0 != NaN

is:

         Nan == 0 && 0 != NaN

which is false.

And in the reverse case:

         $a = 0;
         $b = 'hello';
     
then:

         $a == $b != NaN
    
is:
    
         $a == $b && $b != NaN
     
is:

         0 == "hello" && "hello" != NaN

is:

         0 == NaN && NaN != NaN

which is false too.

The C<!= NaN> is really only needed to cover the third case:

         $a = 'goodbye';
         $b = 'hello';
     
when:

         $a == $b != NaN
    
is:
    
         $a == $b && $b != NaN
     
is:

         "goodbye" == "hello" && "hello" != NaN

is:

         NaN == NaN && NaN != NaN

which is false as well.

Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About