php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66011 Wrong serialize of rounded value
Submitted: 2013-10-31 13:19 UTC Modified: 2013-11-01 08:53 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: proh dot alexey at gmail dot com Assigned: maarten (profile)
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: proh dot alexey at gmail dot com
New email:
PHP Version: OS:

 

 [2013-10-31 13:19 UTC] 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;"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-31 15:55 UTC] 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 16:08 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2013-10-31 16:08 UTC] 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 16:09 UTC] mail at johanntanzer dot at
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.
 [2013-11-01 08:35 UTC] maarten@php.net
-Status: Feedback +Status: Closed -Assigned To: +Assigned To: maarten
 [2013-11-01 08:35 UTC] maarten@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See http://3v4l.org/nCNWi for results of your test script and the code by johanntanzer.

Behaviour as expected, see the warning at http://php.net/manual/en/language.types.float.php

(above link shows different results for 4.3.0 and 4.3.1, which was a major bug and those versions are really ancient)
 [2013-11-01 08:53 UTC] nikic@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC