Re: Why do these values not match?
| From: | Tony | Date: | Wed, 26 Jun 2002 01:00:08 +0000 |
| Subject: | Re: Why do these values not match? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-20116@lists.php.net to get a copy of this message | ||
I wouldn't say it's a bug.
It happens in all programming languages, not just PHP.
I'd rather call it "loss of precision".
A decimal number isn't just what you see.
332.06 doesn't mean it only contains 2 digits.
I don't know much about it, but cast a float number into a string certainly
will get your job done.
Or find a function that does that for you (I don't know which one).
Tony S. Wu
szii@sziisoft.com at szii@sziisoft.com wrote:
> Looks like a bug in PHP since the types are both
> set correctly and appear equal. Doing a cast/recast
> should never change the "value" and have it suddenly
> compare okay via the last "!=" operator.
>
> Filed report Bug #17984 along with attached script.
>
> <?php
>
> $a = 332.06;
> $b = 45.11 + 134.38 + 85.35 + 67.22;
>
> echo "output\n";
>
> var_dump($a);
> var_dump($b);
>
> print("Total = $a\n");
> print("Production = $b\n");
>
>
> if($a != $b)
> {
> print("Totals do not Match!\n");
> }
> else {
> print("Totals are in Balance\n");
> }
>
> if($a !== $b)
> {
> print("Totals do not Match!\n");
> }
> else {
> print("Totals are in Balance\n");
> }
>
> if($a <> $b)
> {
> print("Totals do not Match!\n");
> }
> else {
> print("Totals are in Balance\n");
> }
>
> if($a > $b)
> {
> print("a is greater!\n");
> }
> else {
> print("b is greater!\n");
> }
>
> settype($a,"string");
> settype($b,"string");
>
> if ($a != $b)
> echo "String compare failed!\n\n";
>
>
> settype($a,"float");
> settype($b,"float");
>
> if ($a != $b)
> echo "recast to float compare failed!\n\n";
>
> echo "end output\n";
> ?>
>
> -----------------------------
> output
> float(332.06)
> float(332.06)
> Total = 332.06
> Production = 332.06
> Totals do not Match!
> Totals do not Match!
> Totals do not Match!
> b is greater!
> end output
> ------------------------------
>
> Summary:
> float compare fails (!=)
> float compare fails (<>)
> float compare fails (!==)
> "b" is somehow greater than "a"
> cast to string, compare okay.
> recast to float, compare okay.
>
> -Mike