php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27733 inaccurate arithmetic calculations
Submitted: 2004-03-27 11:12 UTC Modified: 2004-03-27 12:57 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: admin at rubas dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.3.4 OS: Linux 2.4.xx and 2.6.xx
Private report: No CVE-ID: None
 [2004-03-27 11:12 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-27 11:19 UTC] 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 12:57 UTC] edink@php.net
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.


 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Jul 01 22:05:45 2022 UTC