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 + .2) = .3 |
- +----------------+
- | 1 |
- +----------------+
For floating-point values, results are inexact:
- +----------------------+
- | (.1E0 + .2E0) = .3E0 |
- +----------------------+
- | 0 |
- +----------------------+
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.
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
- +-----------+
- | .01 * .01 |
- +-----------+
- | 0.0001 |
- +-----------+
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: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:
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:
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.01 sec)
- Query OK, 1 row affected, 1 warning (0.00 sec)
- +------+
- | i |
- +------+
- | 127 |
- +------+
However, an error occurs if strict mode is in effect:
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (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
:
- Query OK, 0 rows affected (0.01 sec)
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 1 row affected (0.00 sec)
- +------+
- | i |
- +------+
- +------+
However, division by zero is an error if the proper SQL modes are in effect:
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- ERROR 1365 (22012): Division by 0
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
:
- Query OK, 1 row affected (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:
The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type:
- +--------+---------------+------+-----+---------+-------+
- +--------+---------------+------+-----+---------+-------+
- +--------+---------------+------+-----+---------+-------+
The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-precision-math-examples.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.