Bug #8464 Precision math: AVG returns incorrect result
Submitted: 12 Feb 2005 0:45 Modified: 7 Mar 2005 15:35
Reporter: Trudy Pelzer Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.3-alpha-debug OS:Linux (SuSE 9.1)
Assigned to: Alexey Botchkov CPU Architecture:Any

[12 Feb 2005 0:45] Trudy Pelzer
Description:
The AVG of a set of decimals numbers is not being
correctly calculated.

How to repeat:
mysql> create table t1 (col1 decimal(16,12));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(-5.00000000001),(-5.00000000002),(-5.00000000003),(-5.00000000000),(-5.00000000001),(-5.00000000002);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> insert into t1 select * from t1;
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select col1,count(col1),sum(col1),avg(col1) from t1 group by col1\G
*************************** 1. row ***************************
       col1: -5.000000000030
count(col1): 2
  sum(col1): -10.000000000060
  avg(col1): 3741823499999999499999999499999999499999999499999994.4999999994695000
*************************** 2. row ***************************
       col1: -5.000000000020
count(col1): 4
  sum(col1): -20.000000000080
  avg(col1): 2224127749999999749999999749999999749999999749999994.7499999997297500
*************************** 3. row ***************************
       col1: -5.000000000010
count(col1): 4
  sum(col1): -20.000000000040
  avg(col1): 2224127749999999749999999749999999749999999749999994.7499999997397500
*************************** 4. row ***************************
       col1: -5.000000000000
count(col1): 2
  sum(col1): -10.000000000000
  avg(col1): -5.0000000000000000
4 rows in set, 7 warnings (0.01 sec)
-- In all cases, the AVG should be the same as the originally inserted
value. All other functions shown provide the correct result.
[7 Mar 2005 11:09] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/22716
[7 Mar 2005 15:35] Alexey Botchkov
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html