#46727 [NEW]: For loop doesn't work as expected with decimals
| From: | grant at tattomedia dot com | Date: | Mon, 01 Dec 2008 21:11:29 +0000 |
| Subject: | #46727 [NEW]: For loop doesn't work as expected with decimals | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-131528@lists.php.net to get a copy of this message | ||
From: grant at tattomedia dot com
Operating system: Linux
PHP version: 5.3CVS-2008-12-01 (snap)
PHP Bug Type: Unknown/Other Function
Bug description: For loop doesn't work as expected with decimals
Description:
------------
My PHP version is PHP 5.2.5 (cli): This wasn't listed in the select.
It seems that a for loop that deals with floats has bad logic when
evaluating equivalence against another number (in my situation an int).
Reproduce code:
---------------
$values = array();
for ($val = 0; $val <= 2; $val = $val + .1) {
$values[] = $val;
}
print_r($values);
Expected result:
----------------
Array ( [0] => 0 [1] => 0.1 [2] => 0.2 [3] => 0.3 [4] => 0.4 [5] => 0.5
[6] => 0.6 [7] => 0.7 [8] => 0.8 [9] => 0.9 [10] => 1 [11] => 1.1 [12] =>
1.2 [13] => 1.3 [14] => 1.4 [15] => 1.5 [16] => 1.6 [17] => 1.7 [18] => 1.8
[19] => 1.9 [20] => 2 )
Actual result:
--------------
Array ( [0] => 0 [1] => 0.1 [2] => 0.2 [3] => 0.3 [4] => 0.4 [5] => 0.5
[6] => 0.6 [7] => 0.7 [8] => 0.8 [9] => 0.9 [10] => 1 [11] => 1.1 [12] =>
1.2 [13] => 1.3 [14] => 1.4 [15] => 1.5 [16] => 1.6 [17] => 1.7 [18] => 1.8
[19] => 1.9 )
--
Edit bug report at http://bugs.php.net/?id=46727&edit=1
--
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=46727&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=46727&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=46727&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=46727&r=fixedcvs
Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=46727&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=46727&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=46727&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=46727&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=46727&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=46727&r=support
Expected behavior: http://bugs.php.net/fix.php?id=46727&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=46727&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=46727&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=46727&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=46727&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=46727&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=46727&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=46727&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=46727&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=46727&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=46727&r=mysqlcfg