MySQL provides support for precision math: numeric value handling that results in extremely accurate results and a high degree control over invalid values. MySQL提供了对精确数学的支持:数值处理可以产生非常精确的结果,并对无效值进行高度控制。Precision math is based on these two features:精确数学基于以下两个特征:
SQL modes that control how strict the server is about accepting or rejecting invalid data.控制服务器接受或拒绝无效数据的严格程度的SQL模式。
The MySQL library for fixed-point arithmetic.定点算法的MySQL库。
These features have several implications for numeric operations and provide a high degree of compliance with standard SQL:这些特性对数值运算有多方面的影响,并提供了与标准SQL的高度一致性:
Precise calculations精确计算: For exact-value numbers, calculations do not introduce floating-point errors. :对于精确值数字,计算不会引入浮点错误。Instead, exact precision is used. For example, MySQL treats a number such as 取而代之的是精确的精度。例如,MySQL将.0001等数字视为精确值而不是近似值,将其相加10000次将得到精确的.0001
as an exact value rather than as an approximation, and summing it 10,000 times produces a result of exactly 1
, not a value that is merely “close” to 1.1
,而不是仅仅“接近”1的值。
Well-defined rounding behavior明确的舍入行为: For exact-value numbers, the result of :对于精确的数值,ROUND()
depends on its argument, not on environmental factors such as how the underlying C library works.ROUND()
的结果取决于它的参数,而不是环境因素,例如底层C库的工作方式。
Platform independence平台独立性: Operations on exact numeric values are the same across different platforms such as Windows and Unix.:在不同的平台(如Windows和Unix)上,对精确数值的操作是相同的。
Control over handling of invalid values对无效值处理的控制: Overflow and division by zero are detectable and can be treated as errors. :溢出和被零除是可检测的,可以视为错误。For example, you can treat a value that is too large for a column as an error rather than having the value truncated to lie within the range of the column's data type. 例如,可以将对某列来说太大的值视为错误,而不是将该值截断为在该列的数据类型范围内。Similarly, you can treat division by zero as an error rather than as an operation that produces a result of 类似地,您可以将被零除视为错误,而不是产生NULL
. NULL
结果的操作。The choice of which approach to take is determined by the setting of the server SQL mode.选择哪种方法取决于服务器SQL模式的设置。
The following discussion covers several aspects of how precision math works, including possible incompatibilities with older applications. 下面的讨论涵盖了精度数学如何工作的几个方面,包括可能与旧应用程序不兼容。At the end, some examples are given that demonstrate how MySQL handles numeric operations precisely. 最后给出了MySQL如何精确处理数值运算的实例。For information about controlling the SQL mode, see Section 5.1.11, “Server SQL Modes”.有关控制SQL模式的信息,请参阅第5.1.11节,“服务器SQL模式”。