↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
历史版本。 PostgreSQL 10 已结束支持。 2022-11-10. 请参阅 当前版本手册.

9.3. 数学函数和操作符 #

PostgreSQL 为很多类型提供了数学操作符。对于那些没有标准数学惯例的类型(如日期/时间类型),我们将在后续小节中描述实际的行为。

表 9.4 列出了可用的数学操作符。

表 9.4. 数学操作符

操作符描述示例结果
+ 加法2 + 35
-减法2 - 3-1
* 乘法2 * 36
/ 除法(整数除法会截断结果)4 / 22
% 取模(余数)5 % 41
^ 求幂(从左向右结合)2.0 ^ 3.08
|/ 平方根|/ 25.05
||/ 立方根||/ 27.03
! 阶乘(已弃用,改用 factorial())5 !120
!! 以前缀操作符形式求阶乘(已弃用,改用 factorial())!! 5120
@绝对值@ -5.05
& 按位与91 & 1511
| 按位或32 | 335
# 按位异或17 # 520
~按位非~1-2
<< 按位左移1 << 416
>> 按位右移8 >> 22

按位操作符仅适用于整数数据类型,也可用于位串类型 bit 和 bit varying,如表 9.13 所示。

表 9.5 列出了可用的数学函数。表中的 dp 表示 double precision。许多函数提供了参数类型不同的多种形式。除非另有说明,函数的每种形式都返回与其参数相同的数据类型。处理 double precision 数据的函数大多基于主机系统的 C 库实现;因此,其精度和边界情况下的行为可能因主机系统而异。

表 9.5. 数学函数

函数返回类型描述示例结果
abs(x)(与输入相同)绝对值abs(-17.4)17.4
cbrt(dp)dp立方根cbrt(27.0)3
ceil(dp or numeric)(与输入相同)大于或等于参数的最小整数ceil(-42.8)-42
ceiling(dp or numeric)(与输入相同)大于或等于参数的最小整数(与 ceil 相同)ceiling(-95.3)-95
degrees(dp)dp将弧度转换为角度degrees(0.5)28.6478897565412
div(y numeric, x numeric)numericy/x 的整数商div(9,4)2
exp(dp or numeric)(与输入相同)指数函数exp(1.0)2.71828182845905
factorial(bigint)numeric阶乘factorial(5)120
floor(dp or numeric)(与输入相同)小于或等于参数的最大整数floor(-42.8)-43
ln(dp or numeric)(与输入相同)自然对数ln(2.0)0.693147180559945
log(dp or numeric)(与输入相同)以 10 为底的对数log(100.0)2
log(b numeric, x numeric)numeric以 b 为底的对数log(2.0, 64.0)6.0000000000
mod(y, x)(与参数类型相同)y/x 的余数mod(9,4)1
pi()dp常数“π”pi()3.14159265358979
power(a dp, b dp)dp a 的 b 次幂 power(9.0, 3.0)729
power(a numeric, b numeric)numeric a 的 b 次幂 power(9.0, 3.0)729
radians(dp)dp将角度转换为弧度radians(45.0)0.785398163397448
round(dp or numeric)(与输入相同)舍入到最接近的整数round(42.4)42
round(v numeric, s int)numeric舍入到 s 位小数round(42.4382, 2)42.44
scale(numeric)integer参数的小数位数(小数部分的十进制位数)scale(8.41)2
sign(dp or numeric)(与输入相同)参数的符号(-1、0、+1)sign(-8.4)-1
sqrt(dp or numeric)(与输入相同)平方根sqrt(2.0)1.4142135623731
trunc(dp or numeric)(与输入相同)向零截断trunc(42.8)42
trunc(v numeric, s int)numeric截断到 s 位小数trunc(42.4382, 2)42.43
width_bucket(operand dp, b1 dp, b2 dp, count int)int返回 operand 在直方图中所属的桶编号;该直方图将 b1 至 b2 的范围划分为 count 个等宽桶。对于范围之外的输入,返回 0 或 count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand numeric, b1 numeric, b2 numeric, count int)int返回 operand 在直方图中所属的桶编号;该直方图将 b1 至 b2 的范围划分为 count 个等宽桶。对于范围之外的输入,返回 0 或 count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand anyelement, thresholds anyarray)int根据列出各桶下界的数组,返回 operand 所属的桶编号;对于小于第一个下界的输入,返回 0。thresholds 数组必须按升序排序,否则将产生意外结果width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2

表 9.6 展示了用于产生随机数的函数。

表 9.6. 随机函数

函数返回类型描述
random()dp范围 0.0 <= x < 1.0 内的随机值
setseed(dp)void为后续 random() 调用设置种子(值在 -1.0 与 1.0 之间,包含端点)

random() 返回值的特性取决于系统实现。它不适用于加密应用;替代方案见 pgcrypto 模块。

最后,表 9.7 列出了可用的三角函数。所有三角函数的参数和返回值都是 double precision 类型。每个三角函数都有两种变体,一种以弧度度量角,另一种以角度度量角。

表 9.7. 三角函数

函数(弧度)函数(角度)描述
acos(x)acosd(x)反余弦
asin(x) asind(x)反正弦
atan(x) atand(x)反正切
atan2(y, x) atan2d(y, x)y/x 的反正切
cos(x) cosd(x)余弦
cot(x) cotd(x)余切
sin(x) sind(x)正弦
tan(x) tand(x)正切

注意

另一种使用以角度度量的角的方法是使用早前展示的单位转换函数 radians() 和 degrees()。不过,使用基于角度的三角函数更好,因为这类方法能避免 sind(30) 等特殊情况下的舍入误差。

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.