首页 > 其他分享 >lightdb extra_float_digits--控制浮点数精度

lightdb extra_float_digits--控制浮点数精度

时间:2022-12-19 14:00:55浏览次数:81  
标签:digits NOTICE lightdb extra 浮点数 float zjh result

lightdb中extra_float_digits参数可以用来控制浮点数输出的精度,其采用原生c语言的float4/float8实现,可能我们在平时使用中并不太会留意,但是显示的时候会有一些问题。

建一张表,两个字段类型分别是float和numeric,然后插入数据,如下:

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

接下来我们去查询,你就会发现查出来的数据竟然和我们插入的不一样了!

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

好像这样看上去并没有什么,但是会很容易给我们产生误导,让你以为c1字段插入的值是0.555555555555556,其实并不是。可以看到下面的查询并没有记录。

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

这是为什么呢?其实表中存的数据还是原先的0.55555555555555555,只是显示出来错误了,所以我们要这样去查询:

zjh@postgres=>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位数字,这也是和大多数平台是一样的。

zjh@postgres=>select 12.23333333333333::float(24);
 float4
---------
 12.2333
(1 row)

zjh@postgres=>select 12.23333333333333::float(25);
      float8
------------------
 12.2333333333333
(1 row)

这里要注意下,float()中的数字并不表示精度,而是表示存储浮点数的比特位数。而且在lightdb中这个值并没有意义,只是用来区分它是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类型:

zjh@postgres=>do
zjh-# $$
zjh$# declare i int;
zjh$# begin
zjh$# for i in -15..3 loop
zjh$# perform set_config('extra_float_digits',i::text, 'true');
zjh$# raise notice 'extra_float_digits = %, result = %', i, 1.333333333333333::float(24);
zjh$# end loop;
zjh$# end;
zjh$# $$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类型:

zjh@postgres=>do
zjh-# $$
zjh$# declare i int;
zjh$# begin
zjh$# for i in -15..3 loop
zjh$# perform set_config('extra_float_digits',i::text, 'true');
zjh$# raise notice 'extra_float_digits = %, result = %', i, 1.333333333333333::float(25);
zjh$# end loop;
zjh$# 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

在lt_dump时加上 --extra-float-digits选项,用来控制导出的数据中float的精度,例如:

lt_dump -d postgres -t ttest --inserts --extra-float-digits -12 -f ttest_1219.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.light-pg.com/docs/lightdb/13.8-22.3/runtime-config-client.html#RUNTIME-CONFIG-CLIENT-FORMAT
http://www.light-pg.com/docs/lightdb/13.8-22.3/datatype-numeric.html



标签:digits,NOTICE,lightdb,extra,浮点数,float,zjh,result
From: https://www.cnblogs.com/lightdb/p/16991971.html

相关文章

  • c中浮点数类型数据存储
    浮点数在内存的表示方式根据国际标准IEEE(电气和电子工程协会)754,任意一个二进制浮点数V可以表示成下面形式:       (-1)^s*M*2^E       (-1)^s表......
  • IEEE浮点数表示
    **浮点数表示**IEEE浮点标准用$V=(-1)^s*M*2^E的形式来表达一个数$: -符号(sign)s决定这个数是负数(s=1)还是正数(s=0),而对于数值0的符号解释作为特殊情况处理......
  • 1945.sum-of-digits-of-string-after-convert 字符串转化后的各位数字之和
    问题描述1945.字符串转化后的各位数字之和解题思路正常思路就好。代码classSolution{public:intgetLucky(strings,intk){vector<int>num;......
  • java 浮点数 判断相等
    浮点数之间的等值判断,基本数据类型不能使用==进行比较,包装数据类型不能使用equals进行判断。说明:浮点数采用“尾数+阶码”的编码方式,类似于科学计数法的“有效数字+指......
  • postgresql 9.4引入的with ordinaly在lightdb中使用rownum替换
    pg9.4为表函数引入了自动生成行号的功能,如下:=#SELECT*FROMgenerate_series(4,1,-1)WITHORDINALITY;generate_series|ordinality-----------------+--------......
  • 使用CAtlRegExp正则表达式检查浮点数输入
    一、VS2008(Sp1)环境使用CAtlRegExp类需要额外下载ATLServer,(因为开源代码,没集成在VS2008里了)http://atlserver.codeplex.com/二、下载后解压,如F:\CPP\ATL_Server_S......
  • postgresql/lightdb主要的extension及客户端工具清单(持续更新)
    如果说mysql因为其多引擎架构被人称赞,那么在postgresql中,extension开放性则完全可以说是完胜,而且其生态相比mysql而言,明显不在一个级别。本文维护了postgresql重要的三......
  • pgpool ii在lightdb下的性能测试
    1、从​​https://www.pgpool.net/​​下载最新版pgpoolii,如4.3.2。2、假设安装了postgresql或lightdb,百度一搜即可3、解压包,执行./configure &&make&&makeinstall4......
  • LightDB内置特性之访问oracle之oracle_fdw介绍
    LightDB发行版内置了直接访问oracle的扩展oracle_fdw。只要lightdb所在服务器安装了oracle客户端/服务器或轻量客户端,并配置环境变量即可使用。如下:exportORACLE_HOME=......
  • lightdb/postgresql高可用之repmgr日常管理及异常排查指南
    在postgresql的高可用架构中,通常会采用流复制机制实现主备,其历史可参考如下。​​edb​​提供了一个性能影响的参考:  从上可知,HA模式大约会下降1......