From: matt at equaliser dot net Operating system: Linux PHP version: 4.4.1 PHP Bug Type: *Math Functions Bug description: Round() not doing what it says on the tin
Description: ------------ I know *why* this is happening - so please don't point me to http://www.php.net/manual/en/language.types.float.php - I'm aware of this. This does not change the fact that a pure call to the round() function using a float value stored in memory can return the wrong result. The fact that this is easily fixed by rounding to one decimal place further along the float before performing the actual rounding, simply makes the continued existence of this bug more baffling. Why doesn't PHP round() do this internally? Do all PHP developers need to roll their own round() function if they are going to guarantee mathematical accuracy? Reproduce code: --------------- <?php $input_value = 45; $vat = $input_value * .175; echo $vat . '<br />'; echo round($vat , 2) . '<br />'; echo round(strval($vat) , 2) . '<br />'; echo round(round($vat , 3) , 2) . '<br />'; $input_value2 = 7.875; echo round($input_value2 , 2) . '<br />'; echo round(7.875 , 2) . '<br />'; echo $vat - $input_value2; ?> Expected result: ---------------- 7.875 7.87 7.88 7.88 7.88 7.88 -8.8817841970013E-16 Actual result: -------------- 7.875 7.88 7.88 7.88 7.88 7.88 -8.8817841970013E-16 -- Edit bug report at http://bugs.php.net/?id=35896&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=35896&r=trysnapshot44 Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=35896&r=trysnapshot51 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=35896&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=35896&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=35896&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=35896&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=35896&r=needscript Try newer version: http://bugs.php.net/fix.php?id=35896&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=35896&r=support Expected behavior: http://bugs.php.net/fix.php?id=35896&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=35896&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=35896&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=35896&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35896&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=35896&r=dst IIS Stability: http://bugs.php.net/fix.php?id=35896&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=35896&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=35896&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=35896&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=35896&r=mysqlcfg