php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30199 array_sum gives an incorrect total
Submitted: 2004-09-22 20:31 UTC Modified: 2004-10-23 13:57 UTC
From: sdibb at myway dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.0.1 OS: Linux
Private report: No CVE-ID: None
 [2004-09-22 20:31 UTC] sdibb at myway dot com
Description:
------------
array_sum AND adding numbers seems to be returning the wrong total, where all the values are floats.

It seems to be in every case that it's the last value that throws off the grand total.

Tested with both v4.3.8 and v5.0.1 on different linux machines.

Looks like a dupe of http://bugs.php.net/bug.php?id=6034 but not sure, since this doesn't use very large values for the numbers.

Reproduce code:
---------------
long example: http://wonkabar.org/array_sum.phps

short example: php -r "print array_sum(array(149.96, 49.99, -20.00, -149.96, 0, -20.00, -9.99));"



Expected result:
----------------
the correct calculated totals.

Actual result:
--------------
short example: 8.881784197E-15

# php -m
[PHP Modules]
bz2
ctype
gmp
mysql
overload
pcre
pgsql
posix
session
sqlite
standard
tokenizer
xml
zip
zlib

[Zend Modules]


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-23 13:57 UTC] hholzgra@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-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC