Bug #64928 [NEW]: Summing floats yields different result when order of summands is changed
| From: | thz at plista dot com | Date: | Mon, 27 May 2013 10:22:09 +0000 |
| Subject: | Bug #64928 [NEW]: Summing floats yields different result when order of summands is changed | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-179150@lists.php.net to get a copy of this message | ||
From: thz at plista dot com
Operating system: Ubuntu 12.04
PHP version: 5.5Git-2013-05-27 (Git)
Package: Math related
Bug Type: Bug
Bug description:Summing floats yields different result when order of summands is changed
Description:
------------
First of all: I am aware of how floats work and that they are only
approximations of their ideal values.
I found an inconsistency in how they are handled when added, please see the
code below. Variable $a and $b are created by summing up three floats <1.0.
The only difference is the order of the summands. Both variables should
have exactly the same value, yet $a evaluates to 1.0 and $b to <1.0.
Test script:
---------------
<?php
$a = array_sum(array(0.6, 0.3, 0.1));
$b = array_sum(array(0.1, 0.3, 0.6));
var_dump($a); // float(1)
var_dump($b); // float(1)
var_dump($a == $b); // bool(false)
var_dump(intval($a)); // int(0)
var_dump(intval($b)); // int(1)
$c = $a - 1;
var_dump($c); // float(-1.1102230246252E-16)
Expected result:
----------------
float(1)
float(1)
bool(true)
int(1)
int(1)
float(0)
Actual result:
--------------
float(1)
float(1)
bool(false)
int(0)
int(1)
float(-1.1102230246252E-16)
--
Edit bug report at https://bugs.php.net/bug.php?id=64928&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64928&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64928&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64928&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64928&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64928&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64928&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64928&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64928&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64928&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64928&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=64928&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=64928&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64928&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64928&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64928&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64928&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64928&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64928&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64928&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64928&r=mysqlcfg