somepalli Posted March 17, 2009 Share Posted March 17, 2009 (1) <?phpecho (int) ((0.2+0.7)*10);?> The Output Of This code is : 9 (2) <?phpecho (int) ((0.1+0.7)*10);?> The Output Of This code is : 7 In the (2) Program as per my calculation it has to come '8' but i am getting '7' why? [sorry for poor english] [/] Quote Link to comment Share on other sites More sharing options...
mrdamien Posted March 17, 2009 Share Posted March 17, 2009 Because float's are not actually precise numbers to the computer. http://ca2.php.net/int#language.types.integer.casting.from-float Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 The reason is due to floating point math and it's documented inaccuracies. Multiply --- 0.2+0.7 --- each of those numbers by 10, add together, then divide by ten, and it'll work as expected. The idea is convert the 0.2 and 0.7 to an integer first (2 and 7) add them, then convert back to floating point. Quote Link to comment 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.