Edit report at http://bugs.php.net/bug.php?id=53949&edit=1
ID: 53949 Comment by: delphists at apollo dot lv Reported by: pleszczynski at zubi dot pl Summary: Rounding error Status: Open Type: Bug Package: Unknown/Other Function Operating System: Linux PHP Version: 5.2.17 Block user comment: N Private report: N New Comment: Getting invalid results for values between 1048576.505 and 1342176.505: <?php for ( $n = 1048574.505; $n <= 1342178.505; ++$n ) { $rounded = (string)round($n, 2); if ( preg_match('#.51$#', $rounded) ) { echo "Got correct result with n = {$n}\n"; } } ?> Result: Got correct result with n = 1048574.505 Got correct result with n = 1048575.505 Got correct result with n = 1342177.505 Got correct result with n = 1342178.505 Previous Comments: ------------------------------------------------------------------------ [2011-02-07 14:13:17] pleszczynski at zubi dot pl Description: ------------ PHP 5.2.13 & 5.2.17 both have this weird errors in rounding function. This happens quite often, I can supply other numbers as well. Test script: --------------- <?php echo " 1.505: " . round(1.505, 2) . "\n"; echo " 11.505: " . round(11.505, 2) . "\n"; echo " 111.505: " . round(111.505, 2) . "\n"; echo " 111.505: " . round(1111.505, 2) . "\n"; echo " 1111.505: " . round(11111.505, 2) . "\n"; echo " 11111.505: " . round(111111.505, 2) . "\n"; echo " 111111.505: " . round(1111111.505, 2) . "\n"; echo " 1111111.505: " . round(11111111.505, 2) . "\n"; echo "11111111.505: " . round(111111111.505, 2) . "\n"; Expected result: ---------------- 1.505: 1.51 11.505: 11.51 111.505: 111.51 111.505: 1111.51 1111.505: 11111.51 11111.505: 111111.51 111111.505: 1111111.51 1111111.505: 11111111.51 11111111.505: 111111111.51 Actual result: -------------- 1.505: 1.51 11.505: 11.51 111.505: 111.51 111.505: 1111.51 1111.505: 11111.51 11111.505: 111111.51 111111.505: 1111111.5 1111111.505: 11111111.51 11111111.505: 111111111.51 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53949&edit=1