Rechercher dans le manuel MySQL

12.25.5 Precision Math Examples

This section provides some examples that show precision math query results in MySQL. These examples demonstrate the principles described in Section 12.25.3, “Expression Handling”, and Section 12.25.4, “Rounding Behavior”.

Example 1. Numbers are used with their exact value as given when possible:

  1. mysql> SELECT (.1 + .2) = .3;
  2. +----------------+
  3. | (.1 + .2) = .3 |
  4. +----------------+
  5. |              1 |
  6. +----------------+

For floating-point values, results are inexact:

  1. mysql> SELECT (.1E0 + .2E0) = .3E0;
  2. +----------------------+
  3. | (.1E0 + .2E0) = .3E0 |
  4. +----------------------+
  5. |                    0 |
  6. +----------------------+

Another way to see the difference in exact and approximate value handling is to add a small number to a sum many times. Consider the following stored procedure, which adds .0001 to a variable 1,000 times.

  1.   DECLARE d DECIMAL(10,4) DEFAULT 0;
  2.   WHILE i < 10000 DO
  3.     SET d = d + .0001;
  4.     SET f = f + .0001E0;
  5.     SET i = i + 1;
  6.   END WHILE;
  7.   SELECT d, f;

The sum for both d and f logically should be 1, but that is true only for the decimal calculation. The floating-point calculation introduces small errors:

+--------+------------------+
| d      | f                |
+--------+------------------+
| 1.0000 | 0.99999999999991 |
+--------+------------------+

Example 2. Multiplication is performed with the scale required by standard SQL. That is, for two numbers X1 and X2 that have scale S1 and S2, the scale of the result is S1 + S2:

  1. mysql> SELECT .01 * .01;
  2. +-----------+
  3. | .01 * .01 |
  4. +-----------+
  5. | 0.0001    |
  6. +-----------+

Example 3. Rounding behavior for exact-value numbers is well-defined:

Rounding behavior (for example, with the ROUND() function) is independent of the implementation of the underlying C library, which means that results are consistent from platform to platform.

  • Rounding for exact-value columns (DECIMAL and integer) and exact-valued numbers uses the round half away from zero rule. A value with a fractional part of .5 or greater is rounded away from zero to the nearest integer, as shown here:

    1. mysql> SELECT ROUND(2.5), ROUND(-2.5);
    2. +------------+-------------+
    3. | ROUND(2.5) | ROUND(-2.5) |
    4. +------------+-------------+
    5. | 3          | -3          |
    6. +------------+-------------+
  • Rounding for floating-point values uses the C library, which on many systems uses the round to nearest even rule. A value with a fractional part exactly half way between two integers is rounded to the nearest even integer:

    1. mysql> SELECT ROUND(2.5E0), ROUND(-2.5E0);
    2. +--------------+---------------+
    3. | ROUND(2.5E0) | ROUND(-2.5E0) |
    4. +--------------+---------------+
    5. |            2 |            -2 |
    6. +--------------+---------------+

Example 4. In strict mode, inserting a value that is out of range for a column causes an error, rather than truncation to a legal value.

When MySQL is not running in strict mode, truncation to a legal value occurs:

  1. mysql> SET sql_mode='';
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> CREATE TABLE t (i TINYINT);
  5. Query OK, 0 rows affected (0.01 sec)
  6.  
  7. mysql> INSERT INTO t SET i = 128;
  8. Query OK, 1 row affected, 1 warning (0.00 sec)
  9.  
  10. mysql> SELECT i FROM t;
  11. +------+
  12. | i    |
  13. +------+
  14. |  127 |
  15. +------+
  16. 1 row in set (0.00 sec)

However, an error occurs if strict mode is in effect:

  1. mysql> SET sql_mode='STRICT_ALL_TABLES';
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> CREATE TABLE t (i TINYINT);
  5. Query OK, 0 rows affected (0.00 sec)
  6.  
  7. mysql> INSERT INTO t SET i = 128;
  8. ERROR 1264 (22003): Out of range value adjusted for column 'i' at row 1
  9.  
  10. mysql> SELECT i FROM t;
  11. Empty set (0.00 sec)

Example 5: In strict mode and with ERROR_FOR_DIVISION_BY_ZERO set, division by zero causes an error, not a result of NULL.

In nonstrict mode, division by zero has a result of NULL:

  1. mysql> SET sql_mode='';
  2. Query OK, 0 rows affected (0.01 sec)
  3.  
  4. mysql> CREATE TABLE t (i TINYINT);
  5. Query OK, 0 rows affected (0.00 sec)
  6.  
  7. mysql> INSERT INTO t SET i = 1 / 0;
  8. Query OK, 1 row affected (0.00 sec)
  9.  
  10. mysql> SELECT i FROM t;
  11. +------+
  12. | i    |
  13. +------+
  14. | NULL |
  15. +------+
  16. 1 row in set (0.03 sec)

However, division by zero is an error if the proper SQL modes are in effect:

  1. mysql> SET sql_mode='STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO';
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> CREATE TABLE t (i TINYINT);
  5. Query OK, 0 rows affected (0.00 sec)
  6.  
  7. mysql> INSERT INTO t SET i = 1 / 0;
  8. ERROR 1365 (22012): Division by 0
  9.  
  10. mysql> SELECT i FROM t;
  11. Empty set (0.01 sec)

Example 6. Exact-value literals are evaluated as exact values.

Approximate-value literals are evaluated using floating point, but exact-value literals are handled as DECIMAL:

  1. mysql> CREATE TABLE t SELECT 2.5 AS a, 25E-1 AS b;
  2. Query OK, 1 row affected (0.01 sec)
  3. Records: 1  Duplicates: 0  Warnings: 0
  4.  
  5. mysql> DESCRIBE t;
  6. +-------+-----------------------+------+-----+---------+-------+
  7. | Field | Type                  | Null | Key | Default | Extra |
  8. +-------+-----------------------+------+-----+---------+-------+
  9. | a     | decimal(2,1) unsigned | NO   |     | 0.0     |       |
  10. | b     | double                | NO   |     | 0       |       |
  11. +-------+-----------------------+------+-----+---------+-------+
  12. 2 rows in set (0.01 sec)

Example 7. If the argument to an aggregate function is an exact numeric type, the result is also an exact numeric type, with a scale at least that of the argument.

Consider these statements:

  1. mysql> CREATE TABLE t (i INT, d DECIMAL, f FLOAT);
  2. mysql> INSERT INTO t VALUES(1,1,1);
  3. mysql> CREATE TABLE y SELECT AVG(i), AVG(d), AVG(f) FROM t;

The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type:

  1. mysql> DESCRIBE y;
  2. +--------+---------------+------+-----+---------+-------+
  3. | Field  | Type          | Null | Key | Default | Extra |
  4. +--------+---------------+------+-----+---------+-------+
  5. | AVG(i) | decimal(14,4) | YES  |     | NULL    |       |
  6. | AVG(d) | decimal(14,4) | YES  |     | NULL    |       |
  7. | AVG(f) | double        | YES  |     | NULL    |       |
  8. +--------+---------------+------+-----+---------+-------+

The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-precision-math-examples.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut