#44898 [Opn->Bgs]: Operators not acting correctly to floatval results
| From: | derick@php.net | Date: | Fri, 02 May 2008 20:58:29 +0000 |
| Subject: | #44898 [Opn->Bgs]: Operators not acting correctly to floatval results | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-124679@lists.php.net to get a copy of this message | ||
ID: 44898
Updated by: derick@php.net
Reported By: zero at infernotechnologies dot net
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: CentOS Enterprise 4.5 x86_64
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-05-02 20:49:24] zero at infernotechnologies dot net
It seems adding a single line fixes the issue:
Line to add:
---------------------
$result = floatval($result . ' ');
Working version:
---------------------
$value1 = '1';
$value2 = '0.9';
$value3 = 0.1;
$result = floatval(floatval($value1) - floatval($value2));
$result = floatval($result . ' ');
if ($result >= $value3)
{
echo 'Working as intended';
}
else
{
echo "$result IS NOT >= $value3<br />";
var_dump($result, $value3);
}
------------------------------------------------------------------------
[2008-05-02 20:43:47] zero at infernotechnologies dot net
Description:
------------
floatval() two seperate strings to return a result, try result against
exact same number (via >=) and condition returns false.
Reproduce code:
---------------
$value1 = '1';
$value2 = '0.9';
$value3 = 0.1;
$result = floatval(floatval($value1) - floatval($value2));
if ($result >= $value3)
{
echo 'Working as intended';
}
else
{
echo "$result IS NOT >= $value3<br />";
var_dump($result, $value3);
}
Expected result:
----------------
Working as intended
Actual result:
--------------
0.1 IS NOT >= 0.1
float(0.1) float(0.1)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44898&edit=1