Jump to content

Math Errors and Problems


~mr_wizard~

Recommended Posts

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?

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.