~mr_wizard~ Posted January 21, 2012 Share Posted January 21, 2012 I am doing some simple math and it is spitting out very odd results. //First Loop to get the values $get_values = mysql_query("SELECT * FROM table"); while($values = mysql_fetch_array)){ $value_one += $values[value_one]; $value_two += $values[value_two]; } $difference = $value_one - $value_two; echo"$value_one - $value_two = $difference; The database field types are DECIMAL(10,2). The output of the individual values is echoing the correct 0.00 format but after it loops for awhile the difference seems to come out differently. and have close to 5 or 6 places after the decimal. if i echo the values as it is looping and adding them it seems to not be adding correctly either. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/255451-math-errors-and-problems/ Share on other sites More sharing options...
awjudd Posted January 21, 2012 Share Posted January 21, 2012 If that is all you are doing ... why don't you just do the math in the database? SELECT (SUM(value_one) - SUM(value_two)) AS totalDifference FROM table Quote Link to comment https://forums.phpfreaks.com/topic/255451-math-errors-and-problems/#findComment-1309713 Share on other sites More sharing options...
SergeiSS Posted January 21, 2012 Share Posted January 21, 2012 The database field types are DECIMAL(10,2). The output of the individual values is echoing the correct 0.00 format but after it loops for awhile the difference seems to come out differently. and have close to 5 or 6 places after the decimal. if i echo the values as it is looping and adding them it seems to not be adding correctly either. What am I doing wrong? I think that you are doing correct all things. You just don't understand it If you read about a way how float values are stored and processed, it could be more clear. The point is that there is an accuracy (or better say precision?) of float values. It could be that you save 3.0 but it coule be read next time as 2.999999999999. And what happen if you read 2 values and substract (or add) them? You'd get exactly what you described. Read here http://en.wikipedia.org/wiki/Floating_point#Minimizing_the_effect_of_accuracy_problems Look for words "Minimizing the effect of accuracy problems". Quote Link to comment https://forums.phpfreaks.com/topic/255451-math-errors-and-problems/#findComment-1309738 Share on other sites More sharing options...
~mr_wizard~ Posted January 21, 2012 Author Share Posted January 21, 2012 Thank you for the reply and yes I discovered and read alot about the floating point. After talking with my brother who is a Software / Robotics Engineer for NASA, he basically explained that a computer will run out the decimal places. that is truly never a zero. Makes sense. But as a way to combat this issue, they use a double accuracy check in a matter of speaking. Basically, in my case, I want to display 2 decimal places. So if I store the integer in the database as 4 decimals, ( DECIMAL(10,4) ) and process them troughout the script that way, simply use round($foo, 4); and then at the last minute before display take it to 2 decimals then it will stay pretty accurate. When looping thru just a few records and adding them together, not much will change. But if, like in my case, your looping thru 10's of thousands of records then by the time it hits the end of the loop, you will end up with extra digits that you never placed. Anyway, most of you probably already know that but I thought I would reply so that someone else who didn't would maybe have a better understanding. Quote Link to comment https://forums.phpfreaks.com/topic/255451-math-errors-and-problems/#findComment-1309862 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.