Oracle 5.0 Reference Manual page 2992

Table of Contents

Advertisement

The following example (for versions of MySQL older than 5.0.3) demonstrates the problem. It shows
that even for older
DECIMAL
are subject to floating-point error. (Were you to replace the
problems would occur for all versions of MySQL.)
mysql>
CREATE TABLE t1 (i INT, d1 DECIMAL(9,2), d2 DECIMAL(9,2));
mysql>
INSERT INTO t1 VALUES (1, 101.40, 21.40), (1, -80.00, 0.00),
->
(2, 0.00, 0.00), (2, -13.20, 0.00), (2, 59.60, 46.40),
->
(2, 30.40, 30.40), (3, 37.00, 7.40), (3, -29.60, 0.00),
->
(4, 60.00, 15.40), (4, -10.60, 0.00), (4, -34.00, 0.00),
->
(5, 33.00, 0.00), (5, -25.80, 0.00), (5, 0.00, 7.20),
->
(6, 0.00, 0.00), (6, -51.40, 0.00);
mysql>
SELECT i, SUM(d1) AS a, SUM(d2) AS b
->
FROM t1 GROUP BY i HAVING a <> b;
+------+--------+-------+
| i
| a
| b
+------+--------+-------+
|
1 |
21.40 | 21.40 |
|
2 |
76.80 | 76.80 |
|
3 |
7.40 |
7.40 |
|
4 |
15.40 | 15.40 |
|
5 |
7.20 |
7.20 |
|
6 | -51.40 |
0.00 |
+------+--------+-------+
The result is correct. Although the first five records look like they should not satisfy the comparison
(the values of
and
a
b
the numbers shows up around the tenth decimal or so, depending on factors such as computer
architecture or the compiler version or optimization level. For example, different CPUs may evaluate
floating-point numbers differently.
As of MySQL 5.0.3, you will get only the last row in the above result.
The problem cannot be solved by using
floating-point number:
mysql>
SELECT i, ROUND(SUM(d1), 2) AS a, ROUND(SUM(d2), 2) AS b
->
FROM t1 GROUP BY i HAVING a <> b;
+------+--------+-------+
| i
| a
| b
+------+--------+-------+
|
1 |
21.40 | 21.40 |
|
2 |
76.80 | 76.80 |
|
3 |
7.40 |
7.40 |
|
4 |
15.40 | 15.40 |
|
5 |
7.20 |
7.20 |
|
6 | -51.40 |
0.00 |
+------+--------+-------+
This is what the numbers in column
mysql>
SELECT i, ROUND(SUM(d1), 2)*1.0000000000000000 AS a,
->
ROUND(SUM(d2), 2) AS b FROM t1 GROUP BY i HAVING a <> b;
+------+----------------------+-------+
| i
| a
+------+----------------------+-------+
|
1 |
21.3999999999999986 | 21.40 |
|
2 |
76.7999999999999972 | 76.80 |
|
3 |
7.4000000000000004 |
|
4 |
15.4000000000000004 | 15.40 |
|
5 |
7.2000000000000002 |
|
6 | -51.3999999999999986 |
+------+----------------------+-------+
Depending on your computer architecture, you may or may not see similar results. For example, on
some machines you may get the "correct" results by multiplying both arguments by 1, as the following
example shows.
Query-Related Issues
columns, calculations that are done using floating-point operations
|
do not appear to be different), they may do so because the difference between
ROUND()
|
look like when displayed with more decimal places:
a
| b
|
7.40 |
7.20 |
0.00 |
2972
columns with FLOAT, similar
DECIMAL
[913]
or similar functions, because the result is still a

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents