This is probably a very easy question for someone experienced but it's driving me nuts.
I am writing a program that needs to draw a specific amount of smilies.
The account is calculated via:
$cer = userinput 1 / userinput 2
$eer = userinput 3 / userinput 4
$arr = $cer-$eer
all userinputs are integers but $cer, $eer and $arr are floats most of the time although they can be integers depending on the inputs. I am not using a static cast here.
I want to represent this as a 10x10 grid of smilies with happy, sad and difference smilies as options.
my code:
$experiment_happy = (int)(100-($eer*100+$arr*100));
$experiment_difference = (int)($arr*100);
$experiment_sad = (int)($eer*100);
echo $experiment_difference."<br>";
echo "<table><tr>";
for ($i = 0; $i < $experiment_happy ; $i++) {
if ($i % 10 != 0){
echo "<td><img src='images/happy.gif' width='20' height='20'></td>";
}else{
echo "</tr><tr>";
echo "<td><img src='images/happy.gif' width='20' height='20'></td>";
}
}
echo "</tr><tr>";
for ($i = 0; $i < $experiment_difference ; $i++) {
if ($i % 10 != 0){
echo "<td><img src='images/difference.gif' width='20' height='20'> </td>";
}else{
echo "</tr><tr>";
echo "<td><img src='images/difference.gif' width='20' height='20'></td>";
}
}
echo "</tr><tr>";
for ($i = 0; $i < $experiment_sad ; $i++) {
if ($i % 10 != 0){
echo "<td><img src='images/sad.gif' width='20' height='20'></td>";
}
else{
echo "</tr><tr>";
echo "<td><img src='images/sad.gif' width='20' height='20'></td>";
}
}
echo"
</tr></table>
";
I am getting problems with the following numbers: userinput 1: 100 userinput 2: 30 userinput 3: 100 userinput 4: 17
The calculations run fine but an incorrect number of smilies is drawn because of some conversion error. arr is calculated to be 0.13 which is multiplied with 100 -> 13 This is then converted to an integer (int)($arr*100) which somehow returns 12.
Where am I going wrong. I suspect it has something to do with floating point comparison or conversion to integer but I just can not seem to fix this..
Any help and explanation would be most appreciated.
A live version can be seen here. http://niederrad.no-ip.org/alex_test/nnt.php. The input boxes represent the userinputs 1-4 from top to bottom and my problem is occuring with the second smilie grid.