PHP expression output -


this question has answer here:

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

Popular posts from this blog

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -

php - Prepared Statement Inner Join Cannot Pass Parameter by Reference -

c# - Value cannot be null or empty.\r\nParameter name: name -