日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PostgreSQL extra_float_digits——控制浮点数精度

發布時間:2023/12/10 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PostgreSQL extra_float_digits——控制浮点数精度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PostgreSQL中extra_float_digits參數可以用來控制浮點數輸出的精度,可能我們在平時使用中并不太會留意,那我們下來看下面這個例子。

建一張表,兩個字段類型分別是float和numeric,然后插入數據

bill@bill=>create table t1(c1 float,c2 numeric); CREATE TABLE bill@bill=>insert into t1 values(0.55555555555555555,0.55555555555555555); INSERT 0 1

接下來我們去查詢,你就會發現查出來的數據竟然和我們插入的不一樣了!

bill@bill=>select * from t1;c1 | c2 -------------------+---------------------0.555555555555556 | 0.55555555555555555 (1 row)

好像這樣看上去并沒有什么,但是會很容易給我們產生誤導,讓你以為c1字段插入的值是0.555555555555556,其實并不是??梢钥吹较旅娴牟樵儾]有記錄。

bill@bill=>select * from t1 where c1 >= 0.555555555555556;c1 | c2 ----+---- (0 rows)

這是為什么呢?其實表中存的數據還是原先的0.55555555555555555,只是顯示出來錯誤了,所以我們要這樣去查詢:

bill@bill=>select * from t1 where c1::numeric >= 0.555555555555556;c1 | c2 -------------------+---------------------0.555555555555556 | 0.55555555555555555 (1 row)

所以這種情況下我們就特別需要注意float類型的精度了!

接下來我們言歸正傳,看看extra_float_digits這個參數。

介紹這個參數前,我們需要知道在pg中float4默認精確到6位數字,float8精確到15位數字,這也是和大多數平臺是一樣的。

bill@bill=>select 12.23333333333333::float(24);float4 ---------12.2333 (1 row)bill@bill=>select 12.23333333333333::float(25);float8 ------------------12.2333333333333 (1 row)

這里要注意下,float()中的數字并不表示精度,而是表示存儲浮點數的比特位數。而且在PostgreSQL中這個值并沒有意義,只是用來區分它是float4還是float8而已。在pg中1到24個比特位表示float4,25到53個比特位表示float8。


而要控制float類型的精度便是需要通過extra_float_digits參數了。

extra_float_digits取值范圍為-15~3,默認是0。等于0時float4精確到6位數字,float8精確到15位數字。增大該值則會增加精確的位數,減小則會降低精度,例如設置該值為3,則float4精確到9位數字。

–float4類型:

bill@bill=>do bill-# $$ bill$# declare i int; bill$# begin bill$# for i in -15..3 loop bill$# perform set_config('extra_float_digits',i::text, 'true'); bill$# raise notice 'extra_float_digits = %, result = %', i, 1.333333333333333::float(24); bill$# end loop; bill$# end; bill$# $$language plpgsql; NOTICE: extra_float_digits = -15, result = 1 NOTICE: extra_float_digits = -14, result = 1 NOTICE: extra_float_digits = -13, result = 1 NOTICE: extra_float_digits = -12, result = 1 NOTICE: extra_float_digits = -11, result = 1 NOTICE: extra_float_digits = -10, result = 1 NOTICE: extra_float_digits = -9, result = 1 NOTICE: extra_float_digits = -8, result = 1 NOTICE: extra_float_digits = -7, result = 1 NOTICE: extra_float_digits = -6, result = 1 NOTICE: extra_float_digits = -5, result = 1 NOTICE: extra_float_digits = -4, result = 1.3 NOTICE: extra_float_digits = -3, result = 1.33 NOTICE: extra_float_digits = -2, result = 1.333 NOTICE: extra_float_digits = -1, result = 1.3333 NOTICE: extra_float_digits = 0, result = 1.33333 NOTICE: extra_float_digits = 1, result = 1.3333334 NOTICE: extra_float_digits = 2, result = 1.3333334 NOTICE: extra_float_digits = 3, result = 1.3333334 DO

–float8類型:

bill@bill=>do bill-# $$ bill$# declare i int; bill$# begin bill$# for i in -15..3 loop bill$# perform set_config('extra_float_digits',i::text, 'true'); bill$# raise notice 'extra_float_digits = %, result = %', i, 1.333333333333333::float(25); bill$# end loop; bill$# end; bill$# $$language plpgsql; NOTICE: extra_float_digits = -15, result = 1 NOTICE: extra_float_digits = -14, result = 1 NOTICE: extra_float_digits = -13, result = 1.3 NOTICE: extra_float_digits = -12, result = 1.33 NOTICE: extra_float_digits = -11, result = 1.333 NOTICE: extra_float_digits = -10, result = 1.3333 NOTICE: extra_float_digits = -9, result = 1.33333 NOTICE: extra_float_digits = -8, result = 1.333333 NOTICE: extra_float_digits = -7, result = 1.3333333 NOTICE: extra_float_digits = -6, result = 1.33333333 NOTICE: extra_float_digits = -5, result = 1.333333333 NOTICE: extra_float_digits = -4, result = 1.3333333333 NOTICE: extra_float_digits = -3, result = 1.33333333333 NOTICE: extra_float_digits = -2, result = 1.333333333333 NOTICE: extra_float_digits = -1, result = 1.3333333333333 NOTICE: extra_float_digits = 0, result = 1.33333333333333 NOTICE: extra_float_digits = 1, result = 1.333333333333333 NOTICE: extra_float_digits = 2, result = 1.333333333333333 NOTICE: extra_float_digits = 3, result = 1.333333333333333 DO

此外,pg12開始,支持在pg_dump時加上 --extra-float-digits選項,用來控制導出的數據中float的精度,例如:

pg_dump -d bill -t t5 --inserts --extra-float-digits -12 -f t5_1231.txt

-- Data for Name: t5; Type: TABLE DATA; Schema: public; Owner: bill--INSERT INTO public.t5 VALUES (1.23);INSERT INTO public.t5 VALUES (1.24);INSERT INTO public.t5 VALUES (1.57);

參考鏈接:
http://www.postgres.cn/docs/12/runtime-config-client.html#RUNTIME-CONFIG-CLIENT-FORMAT
http://www.postgres.cn/docs/12/datatype-numeric.html

總結

以上是生活随笔為你收集整理的PostgreSQL extra_float_digits——控制浮点数精度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。