8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 13 种数据类型 / 13.1 数字数据类型 / 13.1.4 浮点类型(近似值)-FLOAT、DOUBLE
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

13.1.4 浮点类型(近似值) - FLOAT, DOUBLE13.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE

FLOAT 和 DOUBLE 类型表示近似数值数据。MySQL 对单精度值使用四个字节,对双精度值使用八个字节。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.

对于 FLOAT,SQL 标准允许在 FLOAT 关键字后用括号可选地指定位精度(但不指定索引范围),即 FLOAT(p)。MySQL 也支持这种可选精度的指定,但在 FLOAT(p) 中的精度值仅用于确定存储大小。精度从 0 到 23 会产生 4 字节的单精度 FLOAT 列。精度从 24 到 53 会产生 8 字节的双精度 DOUBLE 列。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. A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.

MySQL 允许一种非标准语法:FLOAT(M,D) 或 REAL(M,D) 或 DOUBLE PRECISION(M,D)。这里,(M,D) 意味着数值可以存储最多 M 位数字,其中 D 位可以是小数点后的数字。例如,一个定义为 FLOAT(7,4) 的列会显示为 -999.9999。MySQL 在存储数值时会进行四舍五入,所以如果你向 FLOAT(7,4) 列插入 999.00009,近似结果是 999.0001。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) is displayed as -999.9999. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.

FLOAT(M,D) 和 DOUBLE(M,D) 是非标准的 MySQL 扩展;并且已被弃用。您应该预期在未来的 MySQL 版本中这些变体的支持将被移除。FLOAT(M,D)and DOUBLE(M,D) are nonstandard MySQL extensions; and are deprecated. You should expect support for these variants to be removed in a future version of MySQL.

由于浮点值是近似值,而不是以精确值存储,尝试在比较中将它们当作精确值处理可能会导致问题。它们还可能受到平台或实现的依赖。有关更多信息,请参见 B.3.4.8 节,“浮点值的问题”。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.3.4.8, “Problems with Floating-Point Values”.

为了最大程度的可移植性,需要存储近似数值数据的代码应使用 FLOAT 或 DOUBLE PRECISION,且不指定精度或数字位数。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.