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

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

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