8.1.3. Floating-Point Types

8.1.3. Floating-Point Types
8.1.3.浮点数类型
The data types real and double precision are inexact, variable-precision numeric types. In  practice, these types are usually implementations of IEEE Standard 754 for Binary Floating-Point Arithmetic (single and double precision, respectively), to the extent that the underlying processor,  operating system, and compiler support it.
real和double数据类型是不准确的、变精度数字类型。 实际上,这些类型通常是针对二进制浮点算法的IEEE标准754的实现(分别为单精度和双精度),只要底层处理器,操作系统和编译器支持它即可。
 
Inexact means that some values cannot be converted exactly to the internal format and are stored as  approximations, so that storing and retrieving a value might show slight discrepancies. Managing these  errors and how they propagate through calculations is the subject of an entire branch of mathematics  and computer science and will not be discussed here, except for the following points:
不精确的意思是某些值无法准确转换为内部格式,只能近似存储,因此存储的值和检索出的值可能会有一些差异。 管理这些错误及其在计算中的传播方式是数学和计算机科学整个分支的主题,此处仅讨论以下内容:
 
• If you require exact storage and calculations (such as for monetary amounts), use the numeric  type instead.
•如果要求存储和计算的精确(例如用作资金账户),那么请使用numeric数据类型。
• If you want to do complicated calculations with these types for anything important, especially if  you rely on certain behavior in boundary cases (infinity, underflow), you should evaluate the implementation  carefully.
•如果需要使用这些类型对某些重要的事情进行复杂的计算,特别是那些依赖于在边界情况下(无穷大、下溢)的某些行为,那么建议再慎重的考虑下。
• Comparing two floating-point values for equality might not always work as expected.
•对两个浮点数进行等值比较,结果可能并不会总如预期的那样。
 
On most platforms, the real type has a range of at least 1E-37 to 1E+37 with a precision of at least  6 decimal digits. The double precision type typically has a range of around 1E-307 to 1E+308  with a precision of at least 15 digits. Values that are too large or too small will cause an error. Rounding  might take place if the precision of an input number is too high. Numbers too close to zero that are  not representable as distinct from zero will cause an underflow error.
在大多数平台中,real数据类型的范围至少为 1E-37到 1E+37,精度至少为6位小数。而double数据类型一般范围为 1E-307到1E+308 ,精度为15位小数。插入太大或太小的值,会报错。 如果输入数字的精度过高,则可能会四舍五入。而太接近零且无法表示为不同于零的数字将导致下溢错误。
 
 
Note
The extra_float_digits setting controls the number of extra significant digits included  when a floating point value is converted to text for output. With the default value of 0 , the output is the same on every platform supported by PostgreSQL. Increasing it will  produce output that more accurately represents the stored value, but may be unportable.
extra_float_digits参数控制在将浮点值转换为文本以输出时所包括的额外有效数字的数量。默认值为0,在PostgreSQL支持的每个平台上的输出都是相同的。增加它会产生更准确地表示存储值的输出,但可能可移植性会变差。
 
In addition to ordinary numeric values, the floating-point types have several special values:
除普通数值外,浮点类型还具有几个特殊值:
 
Infinity
-Infinity
NaN
 
These represent the IEEE 754 special values “infinity”, “negative infinity”, and “not-a-number”, respectively.  (On a machine whose floating-point arithmetic does not follow IEEE 754, these values  will probably not work as expected.) When writing these values as constants in an SQL command, you must put quotes around them, for example UPDATE table SET x = '-Infinity' . On  input, these strings are recognized in a case-insensitive manner.
此为符合IEEE745标准的特殊值,分别为“正无穷”、“负无穷”和“非数字”。(在不遵守IEEE745标准的机器上,这些值可能并不能像预期的那样工作。)当将这些值在SQL命令中作为常量写入时,必须使用引号引起来,例如 UPDATE table SET x = '-Infinity'。输入时,这些值为大小写不敏感。
 
Note
IEEE754 specifies that NaN should not compare equal to any other floating-point value  (including NaN ). In order to allow floating-point values to be sorted and used in treebased  indexes, PostgreSQL treats NaN values as equal, and greater than all non- NaN  values.
IEEE745规定NaN不应该与其他浮点数相等(包括NaN自己)。但是,为了让浮点数可以使用树索引排序,PostgreSQL设定NaN与其自身相等,且不所有非NaN值都大。
 
PostgreSQL also supports the SQL-standard notations float and float( p ) for specifying inexact  numeric types. Here, p specifies the minimum acceptable precision in binary digits. PostgreSQL accepts  float(1) to float(24) as selecting the real type, while float(25) to float(53)  select double precision . Values of p outside the allowed range draw an error. float with no  precision specified is taken to mean double precision .
PostgreSQL也支持SQL标准中的float和float(p)来指定不精确的数字类型。此处,p 指定二进制数的最小可接受精度。PostgreSQL接受float(1)到float(24)作为real选择,而float(25)到float(53)作为double类型。 p的值超出允许范围会产生错误。未指定精度的float类型表示double。
 
Note
The assumption that real and double precision have exactly 24 and 53 bits in  the mantissa respectively is correct for IEEE-standard floating point implementations.  On non-IEEE platforms it might be off a little, but for simplicity the same ranges of  p are used on all platforms.
real和double在尾数中分别具有24位和53位的假设对于IEEE标准中浮点的实现是正确的。在非IEEE平台上,可能略有不同,但是为简单起见,所有平台上都使用相同的p值范围。
 
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104610851