Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

B.4.4.8 Problems with Floating-Point Values

Floating-point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an SQL statement may not be the same as the value represented internally. Attempts to treat floating-point values as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. The FLOAT and DOUBLE data types are subject to these issues. For DECIMAL columns, MySQL performs operations with a precision of 65 decimal digits, which should solve most common inaccuracy problems.

The following example uses DOUBLE to demonstrate how calculations that are done using floating-point operations are subject to floating-point error.

  1. mysql> CREATE TABLE t1 (i INT, d1 DOUBLE, d2 DOUBLE);
  2. mysql> INSERT INTO t1 VALUES (1, 101.40, 21.40), (1, -80.00, 0.00),
  3.     -> (2, 0.00, 0.00), (2, -13.20, 0.00), (2, 59.60, 46.40),
  4.     -> (2, 30.40, 30.40), (3, 37.00, 7.40), (3, -29.60, 0.00),
  5.     -> (4, 60.00, 15.40), (4, -10.60, 0.00), (4, -34.00, 0.00),
  6.     -> (5, 33.00, 0.00), (5, -25.80, 0.00), (5, 0.00, 7.20),
  7.     -> (6, 0.00, 0.00), (6, -51.40, 0.00);
  8.  
  9. mysql> SELECT i, SUM(d1) AS a, SUM(d2) AS b
  10.     -> FROM t1 GROUP BY i HAVING a <> b;
  11.  
  12. +------+-------+------+
  13. | i    | a     | b    |
  14. +------+-------+------+
  15. |    1 |  21.4 | 21.4 |
  16. |    2 |  76.8 | 76.8 |
  17. |    3 |   7.4 |  7.4 |
  18. |    4 |  15.4 | 15.4 |
  19. |    5 |   7.2 |  7.2 |
  20. |    6 | -51.4 |    0 |
  21. +------+-------+------+

The result is correct. Although the first five records look like they should not satisfy the comparison (the values of a and b do not appear to be different), they may do so because the difference between 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.

If columns d1 and d2 had been defined as DECIMAL rather than DOUBLE, the result of the SELECT query would have contained only one row—the last one shown above.

The correct way to do floating-point number comparison is to first decide on an acceptable tolerance for differences between the numbers and then do the comparison against the tolerance value. For example, if we agree that floating-point numbers should be regarded the same if they are same within a precision of one in ten thousand (0.0001), the comparison should be written to find differences larger than the tolerance value:

  1. mysql> SELECT i, SUM(d1) AS a, SUM(d2) AS b FROM t1
  2.     -> GROUP BY i HAVING ABS(a - b) > 0.0001;
  3. +------+-------+------+
  4. | i    | a     | b    |
  5. +------+-------+------+
  6. |    6 | -51.4 |    0 |
  7. +------+-------+------+
  8. 1 row in set (0.00 sec)

Conversely, to get rows where the numbers are the same, the test should find differences within the tolerance value:

  1. mysql> SELECT i, SUM(d1) AS a, SUM(d2) AS b FROM t1
  2.     -> GROUP BY i HAVING ABS(a - b) <= 0.0001;
  3. +------+------+------+
  4. | i    | a    | b    |
  5. +------+------+------+
  6. |    1 | 21.4 | 21.4 |
  7. |    2 | 76.8 | 76.8 |
  8. |    3 |  7.4 |  7.4 |
  9. |    4 | 15.4 | 15.4 |
  10. |    5 |  7.2 |  7.2 |
  11. +------+------+------+
  12. 5 rows in set (0.03 sec)

Floating-point values are subject to platform or implementation dependencies. Suppose that you execute the following statements:

  1. CREATE TABLE t1(c1 FLOAT(53,0), c2 FLOAT(53,0));
  2. INSERT INTO t1 VALUES('1e+52','-1e+52');
  3. SELECT * FROM t1;

On some platforms, the SELECT statement returns inf and -inf. On others, it returns 0 and -0.

An implication of the preceding issues is that if you attempt to create a replication slave by dumping table contents with mysqldump on the master and reloading the dump file into the slave, tables containing floating-point columns might differ between the two hosts.


Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-problems-with-float.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut