MySQL Forums
Forum List  »  General

Sum of doubles giving incorrect value
Posted by: Matthew Jenkins
Date: January 04, 2011 12:18PM

I have a table (of accounts) with a balance field that is of type 'double'. If the accounts balance, the sum of the balance field of all records should equal 0:

mysql> select balance from accounts;
+---------+
| balance |
+---------+
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| -847 |
| 0 |
| 805.01 |
| 41.99 |
+---------+
12 rows in set (0.01 sec)

So, "SELECT SUM(balance) FROM accounts" should return 0, yes?

mysql> select sum(balance) from accounts;
+---------------------+
| sum(balance) |
+---------------------+
| -7.105427357601e-15 |
+---------------------+
1 row in set (0.00 sec)

I know it's almost 0, but that's gotta be a bug, right?

# /usr/local/libexec/mysqld --version
/usr/local/libexec/mysqld Ver 5.1.49 for portbld-freebsd8.1 on i386 (FreeBSD port: mysql-server-5.1.49)

Options: ReplyQuote


Subject
Written By
Posted
Sum of doubles giving incorrect value
January 04, 2011 12:18PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.