PHP expression output -
this question has answer here:
- is floating point math broken? 20 answers
i found somewhere in book
echo (int) ((0.1 + 0.7) * 10);
output : 7
echo ((0.1 + 0.7) * 10);
output : 8
why both out different ? think answer should 8
when write
echo ((0.1 + 0.7) * 10);
the result of simple arithmetic expression stored internally 7.999999 instead of 8.
now when value converted int,
echo (int) ((0.1 + 0.7) * 10); // 7.999999 when typecasted int becomes 7
php truncates away fractional part, resulting in rather significant error (12.5%, exact).
Comments
Post a Comment