praba karan wrote:
The Code which laughed at me :
*<?php
echo (int)((0.1+0.7)*10);
?>*
As per the logic *ANSWER should be "8" but i'm getting only "7"*.. why it
is so ???
(From: http://www.phpbuilder.com/manual/en/language.types.integer.php)
*
Warning*
Never cast an unknown fraction to integer
<http://phpbuilder.com/manual/en/language.types.integer.php>, as this
can sometimes lead to unexpected results.
| <?php
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
?>|
if the code is written as
*echo ((0.1+0.7)*10); *
//or
*echo (float) ((0.1+0.7)*10);*
then no issues in getting the answer "8"..
(From: http://phpbuilder.com/manual/en/language.types.float.php)
It is typical that simple decimal fractions like /0.1/ or /0.7/ cannot
be converted into their internal binary counterparts without a small
loss of precision. This can lead to confusing results: for example,
/floor((0.1+0.7)*10)/ will usually return /7/ instead of the expected
/8/, since the internal representation will be something like /7.9/.
--
With Regards,
Parthan SR "technofreak"
GPG Key 2FF01026
Fingerprint 5707 ECBD 8D8D 8E6E 28F8 DFA5 938B D861 2FF0 1026
Weblog http://blog.technofreak.in
_______________________________________________
To unsubscribe, email ilugc-requ...@ae.iitm.ac.in with
"unsubscribe <password> <address>"
in the subject or body of the message.
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc