选择 打开 改范围 完整检索页
受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10
当前 PostgreSQL 版本不在支持生命周期内。
您可以参阅当前版本的对应页面,或其他在上面列出的活跃大版本。

9.3. 数学函数和操作符 #

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

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

Table 9.4. 数学操作符

操作符 描述 示例 结果
+ 加法 2 + 3 5
- 减法 2 - 3 -1
* 乘法 2 * 3 6
/ 除法(整数除法会截断结果) 4 / 2 2
% 取模(余数) 5 % 4 1
^ 求幂(从左向右结合) 2.0 ^ 3.0 8
|/ 平方根 |/ 25.0 5
||/ 立方根 ||/ 27.0 3
! 阶乘(已弃用,改用 factorial() 5 ! 120
!! 以前缀操作符形式求阶乘(已弃用,改用 factorial() !! 5 120
@ 绝对值 @ -5.0 5
& 按位与 91 & 15 11
| 按位或 32 | 3 35
# 按位异或 17 # 5 20
~ 按位非 ~1 -2
<< 按位左移 1 << 4 16
>> 按位右移 8 >> 2 2

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

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

Table 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) numeric y/x 的整数商 div(9,4) 2
exp(dp or numeric) (与输入相同) 指数函数 exp(1.0) 2.71828182845905
factorial(bigint) numeric factorial 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 ab次幂 power(9.0, 3.0) 729
power(a numeric, b numeric) numeric ab次幂 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 在直方图中所属的桶编号;该直方图将 b1b2 的范围划分为 count 个等宽桶。对于范围之外的输入,返回 0count+1 width_bucket(5.35, 0.024, 10.06, 5) 3
width_bucket(operand numeric, b1 numeric, b2 numeric, count int) int 返回 operand 在直方图中所属的桶编号;该直方图将 b1b2 的范围划分为 count 个等宽桶。对于范围之外的输入,返回 0count+1 width_bucket(5.35, 0.024, 10.06, 5) 3
width_bucket(operand anyelement, thresholds anyarray) int 根据列出各桶下界的数组,返回 operand 所属的桶编号;对于小于第一个下界的输入,返回 0thresholds 数组必须按升序排序,否则将产生意外结果 width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) 2

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

Table 9.6. 随机函数

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

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

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

Table 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) 正切

Note

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