#23298 [NEW]: Floating point data loss in serialize() (and sessions)

From: Date: Mon, 21 Apr 2003 20:22:45 +0000
Subject: #23298 [NEW]: Floating point data loss in serialize() (and sessions)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-38019@lists.php.net to get a copy of this message
From: swbrown at ucsd dot edu Operating system: any PHP version: 4.3.1 PHP Bug Type: Variables related Bug description: Floating point data loss in serialize() (and sessions) 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: <?php $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). -- Edit bug report at http://bugs.php.net/?id=23298&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=23298&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=23298&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=23298&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=23298&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=23298&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=23298&r=support Expected behavior: http://bugs.php.net/fix.php?id=23298&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=23298&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=23298&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=23298&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23298&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=23298&r=dst IIS Stability: http://bugs.php.net/fix.php?id=23298&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=23298&r=gnused

« previous php.bugs (#38019) next »