Why result is that? -2.22044604925E-16
When you use the range function, with a range between zero and 1,
incrementing by 0.05 each step, the loop produces an incorrect result when
you attempt to minus the current value in the loop from 1.
As you can see below, the last element in the array is the integer 1. The
script should echo the result of 1 minus 1. However, for some reason PHP
treats 1-1 as -2.22044604925E-16 instead of zero.
Reproduce code:
foreach (range(0,1,0.05) as $t)
{
echo (1-$t).'<br />';
}
Expected result:
----------------
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
Actual result:
--------------
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
-2.22044604925E-16
No comments:
Post a Comment