关于ROUND函数的舍入问题分析

概述

round函数相信大家都比较了解,可以返回离输入参数最近的整数。但是pg中round用法还是有区别的:

复制代码
postgres=# \df round
                                       List of functions
   Schema   | Name  | Result data type | Argument data types |  Type  | fencedmode | propackage
------------+-------+------------------+---------------------+--------+------------+------------
 pg_catalog | round | double precision | double precision    | normal | f          | f
 pg_catalog | round | numeric          | numeric             | normal | f          | f
 pg_catalog | round | numeric          | numeric, integer    | normal | f          | f
(3 rows)

通过函数定义可知,round函数支持两种类型的入参,如下:

1,round(double)

复制代码
语法:ROUND(double)
入参:浮点类型(近似值),FLOAT/DOUBLE
功能:将double类数值截去所有的小数部分,返回离输入参数最近的整数。

2,round(numeric [, decimals])

复制代码
语法:ROUND(numeric[, decimals])
入参:定点类型(精确值),DECIMAL/NUMERIC
功能:decimals指明需保留小数点后面的位数,四舍五入返回截断后的值。若忽略decimals选项,则截去所有的小数部分,并四舍五入。

接着来说下区别

1,适用类型不同

java 复制代码
# 入参类型为浮点类型,不支持指定小数位,因为入参本身是近似值,小数位截断没有意义
postgres=# select round(1.234::float);
 round
-------
     1
(1 row)

# 入参类型为定点类型,可选指定小数位
postgres=# select round(1.234::numeric);
 round
-------
     1
(1 row)

postgres=# select round(1.234::numeric,2);
 round
-------
  1.23
(1 row)

2,舍入规则不同

java 复制代码
# 入参类型为浮点类型,截断时的原则为奇进偶不进
postgres=# select round(2.5::float),round(3.5::float);
 round | round
-------+-------
     2 |     4
(1 row)

# 入参类型为定点类型,截断时的原则四舍五入
postgres=# select round(2.5::numeric),round(3.5::numeric);
 round | round
-------+-------
     3 |     4
(1 row)

postgres=# select round(2.25::numeric,1),round(2.35::numeric,1);
 round | round
-------+-------
   2.3 |   2.4
(1 row)

原理(此处可参考PG源码):

round(numeric)舍入模式为四舍五入,这里不再展开说明。

round(double)内核调用rint函数,根据手册可知舍入模式受参数影响,默认是FE_TONEAREST,规则为四舍六入五凑偶,5前为奇数进位,5前为偶数舍去。

常量宏 解释
FE_DOWNWARD 向负无穷大舍入
FE_TONEAREST 向最接近可表示值舍入
FE_TOWARDZERO 向零舍入
FE_UPWARD 向正无穷大舍入

可以通过函数fegetround和fesetround查看和修改浮点数的舍入方向。

定义于头文件<fenv.h> 解释
int fesetround(int round); 试图建立等于参数 round 的浮点舍入方向,参数为浮点舍入宏之一
int fegetround(); 返回对应当前舍入方向的浮点舍入宏

小结

一般来讲我们无需过多关注round舍入情况,但对于一些业务敏感场景,有必要区分浮点数和定点数的使用,必要时可以通过指定传参类型来确保结果符合预期。

相关推荐
ZhengEnCi5 小时前
01-如何监听接口调用情况?
java·spring boot·后端
JAVA面经实录9176 小时前
MyBatis学习体系
java·mybatis
java1234_小锋6 小时前
在 Spring AI 中如何实现函数调用(Function Calling)?请说明其基本原理和应用场景。
java·人工智能·spring
小马爱打代码6 小时前
Spring源码 第九篇:Spring 5 源码深度拆解 - Spring 事件驱动模型
java·后端·spring
ForgeAI码匠7 小时前
ForgeAdmin|Spring Boot 3 后台框架的自动配置设计:少写配置,多做组合
java·spring boot·后端
tongluowan0077 小时前
Redisson的参数及工作原理
java·redis·lua·分布式锁
仙俊红8 小时前
Integer\int对比,equals()\hashcode面试
java·面试·职场和发展
WiChP8 小时前
【V0.1B10】从零开始的2D游戏引擎开发之路
java·数据库·游戏引擎
云烟成雨TD8 小时前
Spring AI Alibaba 1.x 系列【60】检查点机制原理与全流程剖析
java·人工智能·spring
ForgeAI码匠8 小时前
Maven 多模块项目如何避免越写越乱?Forge Admin 的模块边界实践
java·人工智能·开源·maven