Skip to content

Commit cd6a40b

Browse files
committed
Add documentation on avoiding FLOAT columns.
1 parent 7a860a7 commit cd6a40b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

docs/content/tutorials/best-practices.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
lastmod: 2019-06-30
2+
lastmod: 2020-02-08
33
date: 2016-10-16
44
menu:
55
main:
@@ -26,6 +26,24 @@ use `TINYINT` (or `TINYINT UNSIGNED`). The `(1)` suffix simply indicates the "di
2626
(which is typically ignored by .NET programs), not the number of bytes used for storage. (And
2727
for a `bool` C# value, use `BOOL` in SQL.)
2828

29+
## Avoid FLOAT
30+
31+
MySQL stores `FLOAT` values as 32-bit single-precision IEEE 754 values. However, when returning
32+
these values to a client application, "MySQL uses the `FLT_DIG` constant (which equals to 6 with
33+
IEEE 754 encoding) to print float-type numbers". This can lead to an apparent loss of precision in
34+
the least-significant digit when selecting values (even though they're stored with full precision).
35+
36+
Do not use a `FLOAT` column if your application needs to retrieve the exact same values that were
37+
stored (with no loss of precision). You can instead use the `DOUBLE` column type (although it has
38+
double the storage requirements), perform a calculation on the value (e.g., `SELECT value+0`)
39+
to coerce it to double-precison (using the original `float` value), or use a prepared statement
40+
(i.e., `MySqlCommand.Prepare`) which uses a binary protocol to retrieve the original value.
41+
42+
References:
43+
44+
* [MySQL bug 87794](https://bugs.mysql.com/bug.php?id=87794)
45+
* [StackOverflow answer](https://stackoverflow.com/a/60084985/23633)
46+
2947
## Asynchronous Operation
3048

3149
MySqlConnector is fully asynchronous, supporting the async ADO.NET methods added in .NET 4.5 without blocking

0 commit comments

Comments
 (0)