Hi folks. I have a strange thing happening here. I have a loop where counter gets incrememnted by 0.1 starting from 0 and up to 1. In each iterration I use the counter in an expression with modulo operator. I get incorrect result when the counter has values of 0.8 and 1. This is the code:
<?php
echo '<pre>';
for($i = 0; $i <= 1; $i += 0.1) {
// result of an expression
$result = ($i * 10) % 10;
// display $i, the expression and the result of the expression
// when $i equals 0.8 and 1 the result is incorrect!!!
echo "i: $i =>> ($i * 10) % 10 = $result <br />";
}
echo '<pre>';
?>
and here is the output I get:
i: 0 =>> (0 * 10) % 10 = 0
i: 0.1 =>> (0.1 * 10) % 10 = 1
i: 0.2 =>> (0.2 * 10) % 10 = 2
i: 0.3 =>> (0.3 * 10) % 10 = 3
i: 0.4 =>> (0.4 * 10) % 10 = 4
i: 0.5 =>> (0.5 * 10) % 10 = 5
i: 0.6 =>> (0.6 * 10) % 10 = 6
i: 0.7 =>> (0.7 * 10) % 10 = 7
i: 0.8 =>> (0.8 * 10) % 10 = 7 <-- this is incorrect
i: 0.9 =>> (0.9 * 10) % 10 = 9
i: 1 =>> (1 * 10) % 10 = 9 <-- this is incorrect
Can anybody test this code and see if the result is the same. Any ideas why this?