#46537 [Opn->Bgs]: Incorrect addition of float and string
| From: | jani@php.net | Date: | Mon, 10 Nov 2008 20:13:50 +0000 |
| Subject: | #46537 [Opn->Bgs]: Incorrect addition of float and string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-130849@lists.php.net to get a copy of this message | ||
ID: 46537
Updated by: jani@php.net
Reported By: syphon dot and dot the dot trick at gmai dot com
-Status: Open
+Status: Bogus
Bug Type: Strings related
Operating System: *
PHP Version: 5.2.6
New Comment:
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2008-11-10 18:43:41] syphon dot and dot the dot trick at gmai dot com
Description:
------------
I've been setting up our system to receive payments via Paypal. Paypal
talks via IPN to a URL that I specify. In my code I have logic to verify
the transaction.
On one line I ran into the problem of addition when verifying
transaction amounts.
I have tried this test on Win32 running PHP 5.2.3 (Basic AppServ
install) and on our live linux environment PHP 5.2.6 with both showing
the same results.
Reproduce code:
---------------
$SubTotal = "67.43";
$Tax = "3.37";
$Amount = $SubTotal + $Tax;
"<div>".var_dump($Amount)."</div>";
$Array = array("mc_gross" => "70.80");
echo ($Amount == $Array['mc_gross']) ? $Array['mc_gross'].' is equal to
'.$Amount : 'These values are not equal. '.$Array['mc_gross']." is not
equal to ".$Amount;
echo '<br /><br />Second Test<br /><br />';
$Amount = 70.8;
"<div>".var_dump($Amount)."</div>";
echo ($Amount == $Array['mc_gross']) ? $Array['mc_gross'].' is equal to
'.$Amount : 'These values are not equal. '.$Array['mc_gross']." is not
equal to ".$Amount;
Expected result:
----------------
The variable mc_gross, a string with a string value "70.80" should
equal the variable $Amount with the float value of 70.8
Actual result:
--------------
The first test adds the two variables ($Subtotal and $Tax) together
into the variable $Amount. Using var_dump php tells us that $Amount is a
float with the value 70.8
When comparing that variable to the one supplied by Paypal (mc_gross)
php says that the values are not equal to each other.
If you redeclare the variable $Amount and with the value 70.8 and try
again, php says the values are equal.
var_dump confirms the variable types and value.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46537&edit=1