php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55518 for loop incorrect arithmetic iteration
Submitted: 2011-08-27 06:53 UTC Modified: 2011-08-27 08:32 UTC
From: forez84 at gmail dot com Assigned:
Status: Closed Package: Math related
PHP Version: PHP Version 5.3.5-1ubuntu7.2 OS: Ubuntu
Private report: No CVE-ID: None
 [2011-08-27 06:53 UTC] forez84 at gmail dot com
Description:
------------
<?php
for ($i=0; $i<100; $i=$i+0.1) {
  echo $i.'|';
}
?>
this produces something like:

0|0.1|0.2|0.3|0.4|0.5|0.6|0.7|0.8|0.9...
...53.6|53.7|53.8|53.9|54|54.1|54.200000000001|54.300000000001|54.400000000001|54.500000000001|...
...66.200000000001|66.300000000001|66.400000000001|66.5|66.6|66.7|66.8|66.9|67|67.1|67.2|...
...83.7|83.8|83.9|84|84.099999999999|84.199999999999|84.299999999999...
...99.799999999999|99.899999999999|99.999999999999|
I believe it makes one more iteration then it should.

Test script:
---------------
<?php
for ($i=0; $i<100; $i=$i+0.1) {
  echo $i.'|';
}
?>


Expected result:
----------------
0|0.1|0.2|0.3|...53.9|54|54.1|54.2|54.3|54.4|...99.9|


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-27 06:57 UTC] forez84 at gmail dot com
-PHP Version: 5.3SVN-2011-08-27 (SVN) +PHP Version: PHP Version 5.3.5-1ubuntu7.2
 [2011-08-27 06:57 UTC] forez84 at gmail dot com
<?php
for ($i=0; $i<100; $i=$i+0.1) {
  echo $i.'|';
}
?>
this produces something like:

0|0.1|0.2|0.3|0.4|0.5|0.6|0.7|0.8|0.9...
...53.6|53.7|53.8|53.9|54|54.1|54.200000000001|54.300000000001|54.400000000001|54.500000000001|...
...66.200000000001|66.300000000001|66.400000000001|66.5|66.6|66.7|66.8|66.9|67|67.1|67.2|...
...83.7|83.8|83.9|84|84.099999999999|84.199999999999|84.299999999999...
...99.799999999999|99.899999999999|99.999999999999|
I believe it makes one more iteration than it should.
 [2011-08-27 08:32 UTC] forez84 at gmail dot com
-Status: Open +Status: Closed
 [2011-08-27 08:32 UTC] forez84 at gmail dot com
apparently it was my pc rather than php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC