Bug #66011 [Com]: Wrong serialize of rounded value
| From: | mail at johanntanzer dot at | Date: | Thu, 31 Oct 2013 16:09:37 +0000 |
| Subject: | Bug #66011 [Com]: Wrong serialize of rounded value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182523@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66011&edit=1
ID: 66011
Comment by: mail at johanntanzer dot at
Reported by: proh dot alexey at gmail dot com
Summary: Wrong serialize of rounded value
Status: Feedback
Type: Bug
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
well that was obvious as you are using strval(),
if i do
$a = round(1 / 3, 2);
var_dump($a, serialize($a));
I get
double(0.33)
string(22) "d:0.33000000000000002;"
on PHP 5.4.6-1ubuntu1.1 (cli)
The problem is, i guess, that there is no finite number in the base 2 system that is equal to 0.33,
and when you serialize that php takes all the available bits to make it as accurate as possible, so
it is serializing the approximation of 0.33 in the base 2 system, which is not equal to 0.33.
There is nothing php can do about this since this is just how computers work,
but if you know this you could just serialize the strval() version.
Previous Comments:
------------------------------------------------------------------------
[2013-10-31 16:08:51] nikic@php.net
Can't reproduce this either. Don't see how that result could possible happen with the
strval() call in there.
------------------------------------------------------------------------
[2013-10-31 15:55:24] mail at johanntanzer dot at
Could not reproduce
I get
string(4) "0.33"
string(11) "s:4:"0.33";"
as expected
------------------------------------------------------------------------
[2013-10-31 13:19:34] proh dot alexey at gmail dot com
Description:
------------
Wrong serialize of rounded value
Test script:
---------------
$a = strval(round(1 / 3, 2));
var_dump($a, serialize($a));
Expected result:
----------------
float(0.33)
string(7) "d:0.33;"
Actual result:
--------------
float(0.33)
string(59) "d:0.330000000000000015543122344752191565930843353271484375;"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66011&edit=1