ID: 41907 User updated by: milman at gmx dot de Reported By: milman at gmx dot de Status: Bogus Bug Type: *Math Functions Operating System: windows xp PHP Version: 5.2.3 New Comment:
ok, but that is the same for other numbers too. function tround ($r0) { $rr0 = round ($r0,2) ; $str1 = sprintf ("%015.20f", $r0) ; echo "$r0 : $rr0 $str1<br>\n" ; } tround (8.055,2) ; tround (8.155,2) ; tround (8.255,2) ; 8.055 : 8.06 8.05499999999999971578 8.155 : 8.15 8.15499999999999936051 8.255 : 8.26 8.25500000000000078160 so i don't understand it. for me it is a bug. Previous Comments: ------------------------------------------------------------------------ [2007-07-05 18:50:48] [EMAIL PROTECTED] php -r 'echo sprintf("%15.20f", 8.155);' 8.15499999999999936051 ------------------------------------------------------------------------ [2007-07-05 18:48:25] milman at gmx dot de sorry but i can't accept this answer. for me it seem like, round-problem than bogus and standard answer. php use common round (Symmetric Arithmetic Rounding) Decide which is the last digit to keep. Increase it by 1 if the next digit is 5 or more (this is called rounding up) Leave it the same if the next digit is 4 or less (this is called rounding down) and then round (8.155, 2) is 8.16 and not 8.15 sprintf ("%15.8f", 8.155) is 000008.15500000 so it is not a problem from intern representation. the rounding problem occurs at number_format and sprintf too. round ------------------------------------------------------------------------ [2007-07-05 16:30:05] [EMAIL PROTECTED] Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. . ------------------------------------------------------------------------ [2007-07-05 16:27:24] milman at gmx dot de Description: ------------ wrong result from round(64.365,2) should be 64.37 not 64.36 Reproduce code: --------------- <? echo "right: round (63.365,2) = " . round (63.365,2) . "<br>\n" ; echo "wrong: round (64.365,2) = " . round (64.365,2) . "<br>\n" ; echo "wrong: round (81.365,2) = " . round (81.365,2) . "<br>\n" ; echo "right: round (82.365,2) = " . round (82.365,2) . "<br>\n" ; ?> Expected result: ---------------- allway .37 not somtimes .36 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41907&edit=1