php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6034 Simple Addition not correct
Submitted: 2000-08-09 00:01 UTC Modified: 2000-08-09 02:34 UTC
From: markn at alverstone dot com dot au Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Linux, Windows + ?
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: markn at alverstone dot com dot au
New email:
PHP Version: OS:

 

 [2000-08-09 00:01 UTC] markn at alverstone dot com dot au
When adding several numbers it appears that they should add to zero. Unfortunatly they dont. For example:

<?

// This is a very simple representation of the problem

$ary[] = 54493.08;
$ary[] = 18900.58;
$ary[] = 45819.39;
$ary[] = 22901.79;
$ary[] = 13342.42;
$ary[] = -127799;
$ary[] = 1755;
$ary[] = -29413.01;
$ary[] = -0.25;
$ary[] = 0;

while(list($key,$val) = each($ary)){
  $total = $val + $total;
  echo "Value: $val Total: $total<br>\n";
}


?>

The running total returns;
Value: 54493.08 Total: 54493.08
Value: 18900.58 Total: 73393.66
Value: 45819.39 Total: 119213.05
Value: 22901.79 Total: 142114.84
Value: 13342.42 Total: 155457.26
Value: -127799 Total: 27658.26
Value: 1755 Total: 29413.26
Value: -29413.01 Total: 0.25000000001091
Value: -0.25 Total: 1.0913936421275E-011
Value: 0 Total: 1.0913936421275E-011


So you can see that 29413.26 + -29413.01 doesnt equal 0.25 as it should.

mn


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-09 02:34 UTC] hholzgra@php.net
_never_ rely on decimal floating point precision when using 
a dual/binary based data type (this isn't even language dependant)

converting from decimal fractions to dual fractions and
back almost always results in theese artefacts you got here

if you need absolute precision without rounding then use the
bcmath extension insted
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC