MySQL SUM() Doesn’t Play Well With Floats
I had to write some reports for some legacy software today and I was unpleasantly surprised with the results of my SQL queries. I was selecting dollar values and summing them to for the monthly spending of certain individuals. Easy enough right? I wrote a query something like this :
SELECT SUM(t.money_spent) as sum_of_spent,
c.customer_name
from transactions t
join customers c on t.customer_id=c.customer_id
group by customer_name order by c.customer_name asc
I ended up getting numerical values that were 10 decimal places long with seemingly random numbers. After checking to make sure the database didn’t have any odd entries I stumbled on this bug report.
The ‘money_spent’ column had a data type of float, which is a waste, but I still don’t think that it should sum up incorrectly. When I select individual values I get proper two decimal results.
Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn’t really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258. This behavior is a little silly but easily fixed by using the ROUND() function :
SELECT SUM(ROUND(t.money_spent)) as sum_of_money_spent,
c.customer_name from transactions t
join customers c on t.customer_id=c.customer_id
group by customer_name order by c.customer_name asc
October 27th, 2009 at
[…] MySQL SUM() Doesn't Play Well With Floats http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html […]
October 28th, 2009 at
I dont’t think it is actually a bug. FLOAT just store floating numbers using a binary representation so there are some subtle aspects to keep in account in the binary-decimal conversion.
Indeed float is not the right type for representing money.
http://docs.sun.com/source/806-3568/ncg_goldberg.html
October 28th, 2009 at
Thanks for the comment. And I agree that it’s not really a bug. That is exactly what that bug report that I linked to said. It is odd behavior though and I had to google a bit before I understood what was going on.
October 29th, 2009 at
use double instead of float
see http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
October 29th, 2009 at
No one expects the Floating Point!