ID: 46727
Updated by: [EMAIL PROTECTED]
Reported By: grant at tattomedia dot com
Status: Open
Bug Type: Unknown/Other Function
Operating System: Linux
PHP Version: 5.3CVS-2008-12-01 (snap)
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2008-12-01 21:11:29] 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 )
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46727&edit=1