Re: Perl WTF



Brandon Metcalf <brandon@xxxxxxxxxxxxxxxxxxxxxx> wrote in
news:slrnh3ndvd.6il.brandon@xxxxxxxxxxxxxxxxxxxxxxxx:

On 2009-06-19, A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> wrote:
sisyphus <sisyphus359@xxxxxxxxx> wrote in
news:da9c3cc3-f551-41b7-a666-8e79331dc0c2
@x6g2000vbg.googlegroups.com:

...

Another way of looking at it - all of the following are true (on
many systems):
"infertility" == "infertility"
"infertility" == "informal"
"fanaticists" == "fanaticists"
"fanaticists" == "most strings"
but
"infertility" != "most strings"
"nanoseconds" != "nanoseconds"

What *were* they thinking of ;-)

Excellent examples. It is clear what is going on if you know what is
going on but to understand what is going on one needs to connect way
too many dots. ;-)


I'll bite. What is going on?

Strings that start with 'inf' are being interpreted as infinity (because
the comparison is numeric). By the rules of IEEE floating point
arithmetic, infinity == infinity is true.

Strings that start with 'nan' are being interpreted as nan (because the
comparison is numeric). By the rules of IEEE floating point arithmetic,
a nan cannot compare equal to any other number, even another nan.

"fanaticists" and "most strings" strings are interpreted as zero.
As 0 == 0 is always true, so is "fanaticists" == "most strings".
However, because "infertility" is interpreted as infinity, it does not
compare numerically equal to "most strings".

As was mentioned upthread, you will be warned of such things if you use
warnings.

Argument "most strings" isn't numeric in numeric ne (!=) at -e line 1.
Argument "infertility" isn't numeric in numeric ne (!=) at -e line 1.

So, you have to be careful when using tests for numeric equality.

If you want string comparison, use eq.

On the other hand, I just tried is on my computer with AS Perl
5.10.0.1004, and I get the following:

C:\Temp> perl -e "print 'infertility' == 'most strings'"
1

C:\Temp> perl -e "print 0 + 'infertility'"
0

C:Temp> perl -e "print 'nanoseconds' == 'nanoseconds'
1

etc. so it seems like something was fixed at some point.

Sinan

--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
.



Relevant Pages

  • Re: Perl WTF
    ... Strings that start with 'inf' are being interpreted as infinity (because ... Strings that start with 'nan' are being interpreted as nan (because the ... By the rules of IEEE floating point arithmetic, ...
    (comp.lang.perl.misc)
  • Re: Perl WTF
    ... infinity == infinity is true. ... Strings that start with 'nan' are being interpreted as nan (because the ... By the rules of IEEE floating point arithmetic, ... As already wrote it is the same for all string to number conversions: ...
    (comp.lang.perl.misc)
  • Re: Calculus XOR Probability
    ... have a number, so it's NaN. ... What kind of math can you do on a Java ... similarly any operation involving an infinity operand results in either ... You described counting the size of a finite set. ...
    (sci.math)
  • Re: Perl WTF
    ... them using strings 'nan' and 'inf' (or any other strings, ... #Alternative creation of an inf: ... print $nan, "\n"; ... - whereas, on linux, the inf/nan strings retain their inf/nan values ...
    (comp.lang.perl.misc)
  • Re: Calculus XOR Probability
    ... have a number, so it's NaN. ... What kind of math can you do on a Java ... similarly any operation involving an infinity operand results in either ... You described counting the size of a finite set. ...
    (sci.math)