#27733 [Opn->Bgs]: inaccurate arithmetic calculations

From: Date: Sat, 27 Mar 2004 17:57:33 +0000
Subject: #27733 [Opn->Bgs]: inaccurate arithmetic calculations
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-56953@lists.php.net to get a copy of this message
ID: 27733 Updated by: edink@php.net Reported By: admin at rubas dot net -Status: Open +Status: Bogus Bug Type: Math related Operating System: Linux 2.4.xx and 2.6.xx PHP Version: 4.3.4 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. Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2004-03-27 11:19:30] j at bitron dot ch Other programming languages as for example C don't have such inaccuracies in results of that simple calculations. According to the IEEE standard for floating point arithmetic, the different programming languages must not return different results on the same machine. ------------------------------------------------------------------------ [2004-03-27 11:12:58] admin at rubas dot net Description: ------------ Sample Code: <?php $resultA = ((6 * 5.95) - (5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95)); $resultB = ((7 * 5.95) - (5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95)); echo "Result A: ".$resultA."\n"; echo "Result B: ".$resultB."\n"; ?> Output: Result A: 0 Result B: -7.105427357601E-15 Reproduced with: PHP 4.3.4 PHP 4.3.3 PHP 4.3.2 Reproduce code: --------------- Sample Code: <?php $resultA = ((6 * 5.95) - (5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95)); $resultB = ((7 * 5.95) - (5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95 + 5.95)); echo "Result A: ".$resultA."\n"; echo "Result B: ".$resultB."\n"; ?> Expected result: ---------------- Result A: 0 Result B: 0 Actual result: -------------- Result A: 0 Result B: -7.105427357601E-15 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27733&edit=1

« previous php.bugs (#56953) next »