#40892 [NEW]: floating point rounding issue

From: Date: Thu, 22 Mar 2007 14:13:24 +0000
Subject: #40892 [NEW]: floating point rounding issue
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-110680@lists.php.net to get a copy of this message
From: fizz at beyond dot hjsoft dot com Operating system: linux, solaris PHP version: 5.2.1 PHP Bug Type: Math related Bug description: floating point rounding issue Description: ------------ When printing out a floating point number using printf (or storing it with sprintf, or casting it as an int) it gives an incorrect number. This appears to be due to floating point math being inaccurate. Reproduce code: --------------- <?php $val = 309; for ($x = 0; $x < 950; $x++) $val += 0.001; var_dump($val); echo $val . "\n"; $val = $val * 100; var_dump($val); echo $val . "\n"; printf ("%d\n", $val); echo (int)$val . "\n"; ?> Expected result: ---------------- float(309.95) 309.95 float(30995) 30995 30995 30995 Actual result: -------------- float(309.95) 309.95 float(30995) 30995 30994 30994 -- Edit bug report at http://bugs.php.net/?id=40892&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40892&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40892&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40892&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=40892&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=40892&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=40892&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=40892&r=needscript Try newer version: http://bugs.php.net/fix.php?id=40892&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=40892&r=support Expected behavior: http://bugs.php.net/fix.php?id=40892&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=40892&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=40892&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=40892&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40892&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=40892&r=dst IIS Stability: http://bugs.php.net/fix.php?id=40892&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=40892&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=40892&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=40892&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=40892&r=mysqlcfg

« previous php.bugs (#110680) next »