|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23298 Floating point data loss in serialize() (and sessions)
Submitted: 2003-04-21 15:22 UTC Modified: 2003-05-04 13:42 UTC
From: swbrown at ucsd dot edu Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.2-RC OS: any
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: swbrown at ucsd dot edu
New email:
PHP Version: OS:


 [2003-04-21 15:22 UTC] swbrown at ucsd dot edu
I ran into a bug in my code where the floating point number I put in a session variable changed slightly when it was loaded from the session the next time around, which in this case caused some math to differ by 1 depending on if the object had been through a session before.  This was definately not expected behavior, as I would expect data to be invariant under serialization.  I poked around at this for a bit, and I'm guessing that PHP uses serialize() to serialize session variables, and serialize() has data loss issues with float/double as it tries to convert them into truncated base 10 numbers.

Test case:

$var = 1.0 / 7.0;
$x = unserialize(serialize($var));
if($x != $var) print("FAIL\n");
else print("SUCCESS\n");

One way I think this could be fixed is to add a new serialize field type other than 'd' for serialize()ing floats/doubles so that future PHP releases can serialize the entire float in base 16 or something so it won't alter data and won't break BC.  Maybe 'e' for IEEE or something.  Serialization should never alter the data being serialized, or it creates all sorts of funky problems (like mine where the object acts slightly different depending on if it's gone through a session).


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-23 11:47 UTC]
php.ini option 'precision', when set high enough will let
this work as expected. (precision=18)

Here's a bit better example script that shows the problem:


ini_set("precision", 32);

$var = 1.4285714285714286476;
$foo = serialize($var);
echo '    ', $foo, "\n";
$x = unserialize($foo);

var_dump($x === $var);

ini_set("precision", 12);

$var = 1.4285714285714286476;
$foo = serialize($var);
echo '    ', $foo, "\n";
$x = unserialize($foo);

var_dump($x === $var);


 [2003-04-23 11:48 UTC]
Forgot the output:

# php test.php

(notice the difference in the value in first part..)

 [2003-04-25 10:52 UTC]
Here's another test script:


ini_set("precision", 32);

$var = 1.0 / 0.7;
echo "float($var)\n";



This all is most likely related to the serialize()
bug too.

 [2003-05-04 13:42 UTC]
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at
In case this was a documentation problem, the fix will show up soon at

In case this was a website problem, the change will show
up on the site and on the mirror sites in short time.
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Apr 02 03:03:39 2023 UTC