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] [/] Link to comment https://forums.phpfreaks.com/topic/149776-output-vary-problem/ 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 Link to comment https://forums.phpfreaks.com/topic/149776-output-vary-problem/#findComment-786490 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. Link to comment https://forums.phpfreaks.com/topic/149776-output-vary-problem/#findComment-786491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.