Re: '1.8' == 1.8 can return false
| From: | Todd Ruth | Date: | Sat, 12 Apr 2008 05:59:49 +0000 |
| Subject: | Re: '1.8' == 1.8 can return false | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-36982@lists.php.net to get a copy of this message | ||
I'd take that as proof of a design flaw in having php
doing string to float comparison by casting the string to
float instead of the float to a string, but you've got
me - the documentation does say the string will be converted
to a float and not vice-versa. If it wouldn't be a bad BC
break, I think there'd be a lower WTF by doing the cast
the other way. "==" means are they the same and "==="
means are they the SAME. With "==", '1.8' should match
any float that would be displayed as 1.8. (Of course,
with "===" they aren't even the same type, so you're
false from the start.) I say "should" from an anti-WTF
perspective. Again, I must bow to the spec and you have
me there.
Thanks for saving me the hours of discovering all this
via debugging. Please consider the switching of casts
for string==float for the suggestion box.
- Todd
On Fri, 2008-04-11 at 20:29 -0700, Rasmus Lerdorf wrote:
> There is no bug here. Please read:
>
> http://docs.sun.com/source/806-3568/ncg_goldberg.html
>
> -Rasmus
>
> Todd Ruth wrote:
> > I'm thinking there must be a bug in the heart of php
> > causing this. I'll debug it, but I haven't looked at
> > php source code in a few years and would like a tip as
> > to which files are involved in evaluating "==". Here
> > is a fragment from my code and the output:
> >
> > ...
> > print "<pre>\n";
> > var_dump($max);
> > var_dump($nms);
> > var_dump($nms == $max);
> > $max = (float)$max;
> > $nms = (string)$nms;
> > var_dump($max);
> > var_dump($nms);
> > var_dump($nms == $max);
> > $max = (string)$max;
> > $nms = (float)$nms;
> > var_dump($max);
> > var_dump($nms);
> > var_dump($nms == $max);
> > print "</pre>\n";
> > ...
> >
> > string(3) "1.8"
> > float(1.8)
> > bool(false)
> > float(1.8)
> > string(3) "1.8"
> > bool(true)
> > string(3) "1.8"
> > float(1.8)
> > bool(true)
> >
> > I found this using php 5.2.3. I just upgraded to php 5.2.5
> > and am receiving the exact same result. Obviously, I can't
> > reproduce it with a simple var_dump("1.8"==1.8). :(
> > I guess something is getting corrupted, and I will add debug
> > output until I find out what (or Murphy removes the problem
> > by adding debug output; then I'm not sure what I'll do).
> >
> > Thanks for any tips on which files I should look at it.
>