|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-02-16 17:18 UTC] qphoria at gmail dot com
Description:
------------
Tried this on the latest 5.3.10 and also on 5.2.17
It is a bit difficult to put into words, but math with varying string-number
sizes calculates wrong.
Simply put:
THIS IS CORRECT:
-----------------
$x = 20.22
: double = 20.22
$y = ("10.10" + "10.12");
: double = 20.22
$x == $y
: bool = TRUE
THIS IS BUGGED:
-------------------
$x = 20.22
: double = 20.22
$y = ("19.10" + "1.12"); //20.22
: double = 20.22
$x == $y
: bool = FALSE
For some reason, if you have a wide number spread in the string math, the
boolean fails, even though they are both shown as float/double numbers
The simple fix is to wrap round() around the string math. Can't really explain
it.
Test script:
---------------
<?php
$x = 23.36;
$y = ("21.42" + "1.94");
if ($x < $y) {
echo "math fail<br/>";
} else {
echo "math win<br/>";
}
var_dump($x, $y);
echo "<br/>";
$x = 23.36;
$y = ("10.36" + "13.00");
if ($x < $y) {
echo "math fail<br/>";
} else {
echo "math win<br/>";
}
var_dump($x, $y);
?>
Expected result:
----------------
I expect to see:
math win
float(23.36) float(23.36)
math win
float(23.36) float(23.36)
Actual result:
--------------
What I really see is:
math fail
float(23.36) float(23.36)
math win
float(23.36) float(23.36)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
Perhaps, but then why wouldn't the additional decimals show in the variable? The initial string math should have returned: 20.22000003 if that was the case. I would expect to see: $x = 20.22 : double = 20.22 $y = ("10.10" + "10.12"); : double = 20.22000030 or 20.22e08 or something