#45970 [Opn->Bgs]: Weird rounding behaviour

From: Date: Tue, 02 Sep 2008 09:28:08 +0000
Subject: #45970 [Opn->Bgs]: Weird rounding behaviour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-128603@lists.php.net to get a copy of this message
ID: 45970 Updated by: scottmac@php.net Reported By: jan dot vernieuwe at webline dot be -Status: Open +Status: Bogus Bug Type: Math related Operating System: FreeBSD PHP Version: 5.2.6 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2008-09-02 09:08:36] jan dot vernieuwe at webline dot be Description: ------------ Rounding a calculated float gives bad results, rounding it as a string works as intended. Reproduce code: --------------- <?php echo "<pre>"; $total = 332.145; $substract = 274.5; $myVal = $total-$substract; var_dump($myVal); var_dump(round(57.645,2)); var_dump(round($myVal,2)); var_dump(round("$myVal",2)); echo "</pre>"; ?> Expected result: ---------------- float(57.645) float(57.65) float(57.65) float(57.65) Actual result: -------------- float(57.645) float(57.65) float(57.64) float(57.65) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45970&edit=1

« previous php.bugs (#128603) next »