Re: Perl WTF
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 19 Jun 2009 16:32:34 GMT
Brandon Metcalf <brandon@xxxxxxxxxxxxxxxxxxxxxx> wrote in
news:slrnh3ndvd.6il.brandon@xxxxxxxxxxxxxxxxxxxxxxxx:
On 2009-06-19, A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> wrote:@x6g2000vbg.googlegroups.com:
sisyphus <sisyphus359@xxxxxxxxx> wrote in
news:da9c3cc3-f551-41b7-a666-8e79331dc0c2
...
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/
.
- Follow-Ups:
- Re: Perl WTF
- From: A. Sinan Unur
- Re: Perl WTF
- References:
- Perl WTF
- From: Lech
- Re: Perl WTF
- From: Randal L. Schwartz
- Re: Perl WTF
- From: Lech
- Re: Perl WTF
- From: sisyphus
- Re: Perl WTF
- From: C.DeRykus
- Re: Perl WTF
- From: sisyphus
- Re: Perl WTF
- From: A. Sinan Unur
- Re: Perl WTF
- From: Brandon Metcalf
- Perl WTF
- Prev by Date: Re: FAQ 2.11 Perl Books
- Next by Date: Re: Perl WTF
- Previous by thread: Re: Perl WTF
- Next by thread: Re: Perl WTF
- Index(es):
Relevant Pages
|