官方文档

https://dev.mysql.com/doc/refman/8.0/en/floating-point-types.html
官方文档是这么说的:

11.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE

The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses; ; that is, FLOAT(p). MySQL also supports this optional precision specification, but the precision value in FLOAT(p) is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. 翻译:精度从0到23的结果是一个4字节的单精度浮点列。(也就是说,即使你把类型定义为float(16,2),也不可能达到16位的精度,因为float是用32bit存储的,只能保证6~7位精度。) A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.

MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.

As of MySQL 8.0.17, the nonstandard FLOAT(M,D) and DOUBLE(M,D) syntax is deprecated and support for it will be removed in a future MySQL version.

Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.4.4.8, “Problems with Floating-Point Values”

For maximum portability, code requiring storage of approximate numeric data values should use FLOAT or DOUBLE PRECISION with no specification of precision or number of digits.

翻译

11.1.4浮点类型(近似值)-浮点,双精度浮点

浮点和双精度类型表示近似的数值数据值。MySQL为单精度值使用4个字节,为双精度值使用8个字节。

对于FLOAT, SQL标准允许在圆括号中的关键字FLOAT之后指定以位为单位的精度(但不允许指定指数的范围);;也就是说,浮子§。MySQL也支持这个可选的精度规范,但是FLOAT§中的精度值只用于确定存储大小。精度从0到23的结果是一个4字节的单精度浮点列。精度从24到53将产生一个8字节的双精度双列。

MySQL允许非标准语法:FLOAT(M,D)或REAL(M,D)或DOUBLE PRECISION(M,D)。在这里,(M,D)表示可以存储最多M位的值,其中D位可能在小数点后面。例如,定义为FLOAT(7,4)的列在显示时看起来像-999.9999。MySQL在存储值时执行四舍五入,所以如果您将999.00009插入到浮点数(7,4)列中,近似结果是999.0001。

从MySQL 8.0.17开始,不支持非标准的FLOAT(M,D)和DOUBLE(M,D)语法,在未来的MySQL版本中将不再支持它。

因为浮点值是近似的,而不是作为精确值存储的,所以在比较中试图将它们作为精确值处理可能会导致问题。它们还依赖于平台或实现。有关更多信息,请参见B.4.4.8节,“浮点值的问题”

为了获得最大的可移植性,需要存储近似数值数据值的代码应该使用浮点或双精度,而不指定精度或数字。

Logo

更多推荐