Hello,
I'm sure you guys out there are familiar with the limitations of floats. For example, the following code, will show how a float might lead you to loose a whole number.
<?php
$floaty = (0.1 + 0.7) * 10; //you would expect this to be a solid 8, right?
echo $floaty . "\n"; // output without conversion - outputs 8 as expected
echo (int)$floaty . "\n"; //output with int conversion - outputs 7!
?>
From what I have read, the reason this outputs 7 when converted to integer is because internally the number is stored as 7.99999999 but my question is why? Why is it stored like that?
If you write the code so that $floaty is 0.8 * 10, you will have no problem. So that means that 0.1 + 0.7 is not the same as 0.8
but why?
Also, why when I write 0.3 * 10 and I echo it to output, I get 3 and not 3.3333333333 as I would have expected?