PostgreSQL:float(1) 和 float(24) 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16889042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:12:56  来源:igfitidea点击:

PostgreSQL: what is the difference between float(1) and float(24)?

postgresqlfloating-pointprecisionsqldatatypesfloating-point-precision

提问by Xin

I am having a hard time understanding the precision parameter p for float(p)in PostgreSQL. For example, float(1)and float(24)seem to be exactly the same to me.

我很难理解float(p)PostgreSQL 中的精度参数 p 。例如,float(1)并且float(24)似乎是完全一样的我。

Can anyone provide me with some examples of their differences, please?

谁能给我提供一些他们差异的例子吗?

回答by Craig Ringer

It's in the manual:

它在手册中

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) 作为选择实数类型,而 float(25) 到 float(53) 选择双精度。超出允许范围的 p 值会产生错误。没有指定精度的 float 表示双精度。

However, the key thing here is that it specifies the minimumacceptable precision. PostgreSQL uses this to select the underlying data type (float4or float8) that meets the requirement.

然而,这里的关键是它指定了可接受的最小精度。PostgreSQL 使用它来选择满足要求的底层数据类型(float4float8)。

regress=> \x
Expanded display is on.
regress=> SELECT
        '1.123456789123456789'::float,
        '1.123456789123456789'::double precision,
        '1.123456789123456789'::float(1),
        '1.123456789123456789'::float(2),
        '1.123456789123456789'::float(24),                                                                                                                                     
        '1.123456789123456789'::float(48);                                                                                                                                     
-[ RECORD 1 ]------------                                                                                                                                                      
float8 | 1.12345678912346
float8 | 1.12345678912346
float4 | 1.12346
float4 | 1.12346
float4 | 1.12346
float8 | 1.12345678912346

You can use pg_typeofto confirm the type selections.

您可以使用pg_typeof来确认类型选择。

Effectively it's just there to support the standard syntax and gets translated into PostgreSQL's dialect - through selection of appropriate types - where it's used.

实际上,它只是在那里支持标准语法并被翻译成 PostgreSQL 的方言 - 通过选择适当的类型 - 在它使用的地方。