Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: undef == 0?

by swampyankee (Parson)
on Oct 25, 2007 at 00:13 UTC ( [id://647043]=note: print w/replies, xml ) Need Help??


in reply to undef == 0?

It actually makes perfect sense to me, in that undef and 0 both evaluate to "false" and "false" equals "false." I've programmed Fortran for many years (my first editor was an IBM-029 card punch 8=)); Fortran actually has a dedicated logical operators, .EQV. and .NEQV. to compare Boolean values. Part of the reason for this that some older Fortrans would do something like "x == y" (actually, X .EQ. Y) without any kind of cast: the compiler would just generate the instructions to compare the actual bit sequences. And Fortran's concept of "true" vs "false" can be a trifle confusing. Of course, one could now digress into why -0 != 0, but one shouldn't.


emc

Information about American English usage here and here.

Any Northeastern US area jobs? I'm currently unemployed.

Replies are listed 'Best First'.
Re^2: undef == 0?
by stu42j (Sexton) on Oct 25, 2007 at 05:09 UTC
    1 and 2 are both true in boolean context so does that mean that 1 == 2?
      == forces scalar context. Both 1 and 2 are treated as decimal integers. Obviously they are not the same.

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
        Uhm, yeah. That was a rhetorical question type thing in response to swampyankee's suggestion that since undef and 0 are both false then it logically follows that undef == 0 should also be true. I was just giving an example where boolean equality is not equivalent to numeric equality.
      No, what it means is that is_true(1) == is_true(2), where is_true is something like sub { $_[0] ? 1 : 0 }

      It does mean that 1 && 2 evaluates to true. And, were Perl to have Fortran-style logical comparison operators (.EQV. and .NEQV.; this gives me an idea for an Acme module...), 1 eqv 2 would evaluate to true.

      Every language has its little corners which may not be that well thought out; undef& == 0 evaluating to true may be one of them (00 evaluating to 1 is probably another). It is consistent, and arguably reasonable, behavior, although it's just as sensible, and arguably more reasonable, to have undef behave like NaN and not be comparable to anything, even itself (NaN == NaN and NaN != NaN are both false in most languages).


      emc

      Information about American English usage here and here.

      Any Northeastern US area jobs? I'm currently unemployed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://647043]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-23 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found