Problems Incrementing by 0.1
Posted: Sun Apr 16, 2006 10:50 am
This could be a really simple problem but maths is not my strong point. I have the following code that loops from 0 to 100 in steps of 0.1
Which works fine until the number after 54.1 - it comes out as 54.200000000001
Can anyone shed some light on this? I know that I could simply round the number but would like to understand what's going on first.
Cheers in advance.
Code: Select all
<?php
for ($i=0; $i<100; $i+= 0.1){
echo $i . "<br />\n";
}
?>Can anyone shed some light on this? I know that I could simply round the number but would like to understand what's going on first.
Cheers in advance.