Why is it to stop the cycle before reaching the set value?
3 answers
This does not work due to floating point precision. The numbers are stored in base 2 format and are not always accurate due to rounding. When you add .1 to 1.9, you do not get 2.0. You end up with something like 1.99999. At the next iteration, you will get something like 2.099998 depending on what it is rounded up with to base 2 format.
For more information, see Floating-Point Numbers and Double-Precision Floating-Point Format
- ,
for ($i = 0; $i < 2.1; $i += .1){
echo $i . '<br />';
}
0