|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-07 13:04 UTC] spartacus4+php at gmail dot com
Description:
------------
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';
$out=array();
$type='test';;
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>';
print_r($out);
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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
Sorry, I through together that sample code really quick. Here's a simplified sample code snippet that produces the same results: $amount = 49.83; $out=array(); for($i=0; $i<1000; $i++){ if(!isset($out['test'])) $out['test']=0; $out['test'] += $amount; echo $out['test'].' + '.$amount.'<br>'; } echo '<pre>'; print_r($out); echo '</pre>'; // Messes up at value: 31243.41