kitnos Posted April 16, 2007 Share Posted April 16, 2007 info: PHP Version 5.1.6 Why do i get this value in a simple operation? <?php echo (1.2 - 0.1) - 1.1; ?> result = -2.2204460492503E-016 Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/ Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 That the result of converting floating point numbers in and out of binary. It's not exact and never will be. For all practical purposes, the result you got is zero. If you round it to one decimal place you will see that. Ken Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230511 Share on other sites More sharing options...
neel_basu Posted April 16, 2007 Share Posted April 16, 2007 Ya Its Strange <?php header('Content-Type: text/plain'); $res1 = (1.2-0.1)-1.1;// = 1.1-1.1 -> Evaluates to Zero $res2 = (2.2-0.1)-2.1;// = 2.1-2.1 -> Also Evaluates to Zero echo $res1."\t".gettype($res1); echo "\n"; echo $res2."\t".gettype($res2); ?> -2.22044604925E-016 double 0 double Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230579 Share on other sites More sharing options...
kitnos Posted April 16, 2007 Author Share Posted April 16, 2007 I need to have precision on this function and when this happens the result goes crazy :-\ Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230593 Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 How many digits of precision do you need? Ken Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230597 Share on other sites More sharing options...
paul2463 Posted April 16, 2007 Share Posted April 16, 2007 echo number_format(((1.2 - 0.1) - 1.1), 0); //outputs -0 echo number_format(((1.2 - 0.1) - 1.1), 1); //outputs -0.0 echo number_format(((1.2 - 0.1) - 1.1), 2); //outputs -0.00 Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230600 Share on other sites More sharing options...
per1os Posted April 16, 2007 Share Posted April 16, 2007 A work around is to do maybe try this: <?php header('Content-Type: text/plain'); $res1 = (1.2-0.1)-1.1;// = 1.1-1.1 -> Evaluates to Zero $res2 = (2.2-0.1)-2.1;// = 2.1-2.1 -> Also Evaluates to Zero echo convertNumber($res1)."\t".gettype($res1); echo "\n"; echo convertNumber($res2)."\t".gettype($res2); function convertNumber($num) { if (number_format($num,1) == -0.0) { return 0; } return $num; } I would create my own function, like above. And just make sure you use it on all number values being calculated. This will return the correct results you want. Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230601 Share on other sites More sharing options...
kitnos Posted April 16, 2007 Author Share Posted April 16, 2007 Thanks All . Problem solved This forum is very quick and with good people Im making some gadgets to a physics software and this was bugging me Link to comment https://forums.phpfreaks.com/topic/47262-solved-simple-operation-get-wrong-result/#findComment-230607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.