Mark W Posted December 25, 2008 Share Posted December 25, 2008 Why does this evaluate to 7? echo (int) ((0.1 + 0.7) * 10); I was told it is because (0.1 + 0.7) * 10) is being stored as 7.99999999, and everything after the decimal is cut off by the conversion to int, but why is it being stored as that? Surely ((0.1 + 0.7) * 10) should be being stored as 8 even before the data type conversion? Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/ Share on other sites More sharing options...
.josh Posted December 25, 2008 Share Posted December 25, 2008 some kind of bug? echo (int) ((0.1 + 0.7) * 10); // output: 7 echo (int) ((.1 + .7) * 10); // output: 7 echo (int) ((1.1 + 0.7) * 10); // output: 18 echo (int) ((0.1 + 1.7) * 10); // output: 18 echo (int) ((1.1 + .7) * 10); // output: 18 echo (int) ((.1 + 1.7) * 10); // output: 18 echo (int) (10 * (0.1 + 0.7)); // output: 7 echo (int) 10 * (0.1 + 0.7); // output: 8 seems to be casting the first number as int and then operating. Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/#findComment-723668 Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 we are here again? read http://www.phpfreaks.com/forums/index.php/topic,231460.msg1073336.html#msg1073336 Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/#findComment-723669 Share on other sites More sharing options...
PFMaBiSmAd Posted December 25, 2008 Share Posted December 25, 2008 Must be a class assignment to see if they can find out how computers do (or don't do) floating point math. Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/#findComment-723694 Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 Must be a class assignment to see if they can find out how computers do (or don't do) floating point math. good call. cus the equation is STRAIGHT off the PHP website Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/#findComment-723723 Share on other sites More sharing options...
.josh Posted December 25, 2008 Share Posted December 25, 2008 haha yeah I noticed that, too, when I followed the link trail. I even learned a thing or two. Quote Link to comment https://forums.phpfreaks.com/topic/138404-why/#findComment-723726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.