|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44659 Adding decimals to math calc where it should not
Submitted: 2008-04-07 13:04 UTC Modified: 2012-05-11 21:25 UTC
From: spartacus4+php at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.5 OS: Mac OSX
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
28 + 12 = ?
Subscribe to this entry?

 [2008-04-07 13:04 UTC] spartacus4+php at gmail dot com
It appears PHP is adding incorrect decimal points. 
The included test script shows this happening at value: 31243.41.
When I add the two values: 31243.41 + 49.83 it produces the correct sum. But if I use it in an array that += the amount, it adds an incorrect decimal value after several hundred add ons. It appears to be a possible memory flaw when summing array values.
FYI: I'm actually using PHP version 5.2.3 - it was not an option in the drop down.

Reproduce code:
$amount = '49.83';
for($i=0; $i<1000; $i++){
	if(!isset($out[$test][$result])) $out[$test][$result]=0;
	$out[$test][$result] += $amount;
	echo $out[$test][$result].' + '.$amount.'<br>';
echo '<pre>';
echo '</pre>';

Expected result:
Either an explanation of why the decimal points are added only when using an array value OR potentially a bug fix within new version fixing this flaw.

Actual result:
Actual results are output within included code.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-07 13:13 UTC] spartacus4+php at gmail dot com
Sorry, I through together that sample code really quick. 
Here's a simplified sample code snippet that produces the same results:

$amount = 49.83;
for($i=0; $i<1000; $i++){
	if(!isset($out['test'])) $out['test']=0;
	$out['test'] += $amount;
	echo $out['test'].' + '.$amount.'<br>';
echo '<pre>';
echo '</pre>';
// Messes up at value: 31243.41
 [2008-04-07 13:45 UTC]
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:
Thank you for your interest in PHP.

 [2012-05-11 21:05 UTC] junkmail at expresscourier dot info
echo  $balance.'<br>';
echo  $trbalance.'<br>';
echo $openbalance = $balance - $trbalance.'<br>';	?>

So 80343.05 - 38390.74 = 41952.26 (!!!!  should be 41952.31) 

lost 0.05 in simple math.
 [2012-05-11 21:25 UTC]

$balance = 80343.05;
$trbalance = 38390.74;
echo  $balance.'<br>';
echo  $trbalance.'<br>';
echo $openbalance = $balance - $trbalance.'<br>';


PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 28 11:01:32 2024 UTC