php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46727 For loop doesn't work as expected with decimals
Submitted: 2008-12-01 21:11 UTC Modified: 2008-12-02 05:47 UTC
From: grant at tattomedia dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3CVS-2008-12-01 (snap) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: grant at tattomedia dot com
New email:
PHP Version: OS:

 

 [2008-12-01 21:11 UTC] grant at tattomedia dot com
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 ) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-02 05:44 UTC] magicaltux@php.net
This is because at the last iteration, the value for $val is (at least on my 5.2.6 test system; php 5.3.0 returns me the same value):

2.000000000000000444089209850062616169452667236328125

This is > 2, which means "<= 2" is false.

If I dump the values of $val for each iteration:

0
0.1000000000000000055511151231257827021181583404541015625
0.200000000000000011102230246251565404236316680908203125
0.3000000000000000444089209850062616169452667236328125
0.40000000000000002220446049250313080847263336181640625
0.5
0.59999999999999997779553950749686919152736663818359375
0.6999999999999999555910790149937383830547332763671875
0.79999999999999993338661852249060757458209991455078125
0.899999999999999911182158029987476766109466552734375
0.99999999999999988897769753748434595763683319091796875
1.0999999999999998667732370449812151491641998291015625
1.1999999999999999555910790149937383830547332763671875
1.3000000000000000444089209850062616169452667236328125
1.4000000000000001332267629550187848508358001708984375
1.5000000000000002220446049250313080847263336181640625
1.6000000000000003108624468950438313186168670654296875
1.7000000000000003996802888650563545525074005126953125
1.8000000000000004884981308350688777863979339599609375
1.9000000000000005773159728050814010202884674072265625

Your "best" option would be to round($val, 1) before checking, but it might prove to be expensive. This is why it's always tricky to work with float values.
 [2008-12-02 05:47 UTC] magicaltux@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/en/language.types.float.php and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 21 00:00:03 2025 UTC