2025年9月25日: PostgreSQL 18 发布!
支持的版本:当前 (18) / 17 / 16 / 15 / 14 / 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

9.3. 数学函数和运算符 #

对于许多 PostgreSQL 类型,都提供了数学运算符。对于没有标准数学约定(例如,日期/时间类型)的类型,我们在后续章节中描述实际行为。

表 9.4 显示了可用于标准数值类型的数学运算符。除非另有说明,接受 numeric_type 的运算符适用于所有类型 smallintintegerbigintnumericrealdouble precision。接受 integral_type 的运算符适用于类型 smallintintegerbigint。除非另有说明,每种形式的运算符都返回与其参数相同的数据类型。涉及多种参数数据类型(例如,integer + numeric)的调用,将使用列表中出现较晚的类型进行解析。

表 9.4. 数学运算符

运算符

描述

示例

numeric_type + numeric_typenumeric_type

加法。

2 + 35

+ numeric_typenumeric_type

一元加号(无操作)

+ 3.53.5

numeric_type - numeric_typenumeric_type

减法。

2 - 3-1

- numeric_typenumeric_type

负号

- (-4)4

numeric_type * numeric_typenumeric_type

乘法。

2 * 36

numeric_type / numeric_typenumeric_type

除法(对于整数类型,除法将结果向零截断)

5.0 / 22.5000000000000000

5 / 22

(-5) / 2-2

numeric_type % numeric_typenumeric_type

模(余数);适用于 smallintintegerbigintnumeric

5 % 41

numeric ^ numericnumeric

double precision ^ double precisiondouble precision

幂运算

2 ^ 38

与典型的数学实践不同,^ 的多次使用默认从左到右关联

2 ^ 3 ^ 3512

2 ^ (3 ^ 3)134217728

|/ double precisiondouble precision

平方根

|/ 25.05

||/ double precisiondouble precision

立方根

||/ 64.04

@ numeric_typenumeric_type

绝对值

@ -5.05.0

integral_type & integral_typeintegral_type

按位与

91 & 1511

integral_type | integral_typeintegral_type

按位或

32 | 335

integral_type # integral_typeintegral_type

按位异或

17 # 520

~ integral_typeintegral_type

按位非

~1-2

integral_type << integerintegral_type

按位左移

1 << 416

integral_type >> integerintegral_type

按位右移

8 >> 22


表 9.5 显示了可用的数学函数。其中许多函数都有多种形式,参数类型不同。除非另有说明,函数的任何一种形式都返回与其参数相同的数据类型;跨类型的情况与上面为运算符解释的相同。处理 double precision 数据的函数大部分是基于宿主系统的 C 库实现的;因此,准确性和边界情况下的行为可能会因宿主系统而异。

表 9.5. 数学函数

函数

描述

示例

abs ( numeric_type ) → numeric_type

绝对值

abs(-17.4)17.4

cbrt ( double precision ) → double precision

立方根

cbrt(64.0)4

ceil ( numeric ) → numeric

ceil ( double precision ) → double precision

大于或等于参数的最近整数

ceil(42.2)43

ceil(-42.8)-42

ceiling ( numeric ) → numeric

ceiling ( double precision ) → double precision

大于或等于参数的最近整数(与 ceil 相同)

ceiling(95.3)96

degrees ( double precision ) → double precision

弧度转换为度

degrees(0.5)28.64788975654116

div ( y numeric, x numeric ) → numeric

整数商 y/x(向零截断)

div(9, 4)2

erf ( double precision ) → double precision

误差函数

erf(1.0)0.8427007929497149

erfc ( double precision ) → double precision

互补误差函数(1 - erf(x),对于大输入值不会丢失精度)

erfc(1.0)0.15729920705028513

exp ( numeric ) → numeric

exp ( double precision ) → double precision

指数函数(e 的给定次幂)

exp(1.0)2.7182818284590452

factorial ( bigint ) → numeric

阶乘

factorial(5)120

floor ( numeric ) → numeric

floor ( double precision ) → double precision

小于或等于参数的最近整数

floor(42.8)42

floor(-42.8)-43

gamma ( double precision ) → double precision

Gamma 函数

gamma(0.5)1.772453850905516

gamma(6)120

gcd ( numeric_type, numeric_type ) → numeric_type

最大公约数(能整除两个输入的最大的正数);如果两个输入都为零,则返回 0;适用于 integerbigintnumeric

gcd(1071, 462)21

lcm ( numeric_type, numeric_type ) → numeric_type

最小公倍数(是两个输入的整倍数的最小严格正数);如果任一输入为零,则返回 0;适用于 integerbigintnumeric

lcm(1071, 462)23562

lgamma ( double precision ) → double precision

Gamma 函数绝对值的自然对数

lgamma(1000)5905.220423209181

ln ( numeric ) → numeric

ln ( double precision ) → double precision

自然对数

ln(2.0)0.6931471805599453

log ( numeric ) → numeric

log ( double precision ) → double precision

以 10 为底的对数

log(100)2

log10 ( numeric ) → numeric

log10 ( double precision ) → double precision

以 10 为底的对数(与 log 相同)

log10(1000)3

log ( b numeric, x numeric ) → numeric

b 为底 x 的对数

log(2.0, 64.0)6.0000000000000000

min_scale ( numeric ) → integer

最小刻度(小数点后有效数字位数),能够精确表示给定值所需的

min_scale(8.4100)2

mod ( y numeric_type, x numeric_type ) → numeric_type

余数 y/x;适用于 smallintintegerbigintnumeric

mod(9, 4)1

pi ( ) → double precision

圆周率 π 的近似值

pi()3.141592653589793

power ( a numeric, b numeric ) → numeric

power ( a double precision, b double precision ) → double precision

ab 次幂

power(9, 3)729

radians ( double precision ) → double precision

度转换为弧度

radians(45.0)0.7853981633974483

round ( numeric ) → numeric

round ( double precision ) → double precision

四舍五入到最近的整数。对于 numeric,相等时向远离零的方向舍入。对于 double precision,相等时的舍入行为取决于平台,但 舍入到最近的偶数 是最常见的规则。

round(42.4)42

round ( v numeric, s integer ) → numeric

v 四舍五入到 s 位小数。相等时向远离零的方向舍入。

round(42.4382, 2)42.44

round(1234.56, -1)1230

scale ( numeric ) → integer

参数的刻度(小数点后有效数字位数)

scale(8.4100)4

sign ( numeric ) → numeric

sign ( double precision ) → double precision

参数的符号(-1、0 或 +1)

sign(-8.4)-1

sqrt ( numeric ) → numeric

sqrt ( double precision ) → double precision

平方根

sqrt(2)1.4142135623730951

trim_scale ( numeric ) → numeric

通过移除尾随零来减小值的刻度(小数点后有效数字位数)

trim_scale(8.4100)8.41

trunc ( numeric ) → numeric

trunc ( double precision ) → double precision

截断为整数(向零方向)

trunc(42.8)42

trunc(-42.8)-42

trunc ( v numeric, s integer ) → numeric

v 截断到 s 位小数

trunc(42.4382, 2)42.43

width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer

width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer

在具有 count 个等宽存储桶(跨越 lowhigh 的范围)的直方图中,返回 operand 所属的存储桶编号。存储桶的下限是包含的,上限是不包含的。小于 low 的输入返回 0,大于或等于 high 的输入返回 count+1。如果 low > high,行为会反转,存储桶 1 现在是紧邻 low 的那个,并且包含的边界现在位于上侧。

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket(9, 10, 0, 10)2

width_bucket ( operand anycompatible, thresholds anycompatiblearray ) → integer

给定一个列出存储桶包含的下限的数组,返回 operand 所属的存储桶编号。小于第一个下限的输入返回 0operand 和数组元素可以是任何具有标准比较运算符的类型。thresholds 数组必须排序,从小到大,否则结果可能不正确。

width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2


表 9.6 显示了用于生成随机数的函数。

表 9.6. 随机函数

函数

描述

示例

random ( ) → double precision

返回一个介于 0.0(包含)和 1.0(不包含)之间的随机值

random()0.897124072839091

random ( min integer, max integer ) → integer

random ( min bigint, max bigint ) → bigint

random ( min numeric, max numeric ) → numeric

返回一个介于 min(包含)和 max(包含)之间的随机值。对于 numeric 类型,结果的小数位数将与 minmax 中小数位数较多者相同。

random(1, 10)7

random(-0.499, 0.499)0.347

random_normal ( [ mean double precision [, stddev double precision ]] ) → double precision

返回具有给定参数的正态分布随机值;mean 默认为 0.0,stddev 默认为 1.0

random_normal(0.0, 1.0)0.051285419

setseed ( double precision ) → void

设置后续 random()random_normal() 调用的种子;参数必须在 -1.0 到 1.0 之间(包含)

setseed(0.12345)


表 9.6 中列出的 random()random_normal() 函数使用确定性伪随机数生成器。它速度很快,但不适合加密应用;有关更安全的替代方案,请参阅 pgcrypto 模块。如果在当前会话中调用 setseed(),则通过使用相同的参数重新发出 setseed() 调用,可以重复后续对这些函数的调用系列。如果在同一会话中未进行任何 setseed() 调用,则对这些函数中的任何一个的首次调用都将从平台相关的随机位源获取种子。

表 9.7 显示了可用的三角函数。这些函数中的每一个都有两个变体,一个以弧度测量角度,一个以度测量角度。

表 9.7. 三角函数

函数

描述

示例

acos ( double precision ) → double precision

反余弦,结果以弧度表示

acos(1)0

acosd ( double precision ) → double precision

反余弦,结果以度表示

acosd(0.5)60

asin ( double precision ) → double precision

反正弦,结果以弧度表示

asin(1)1.5707963267948966

asind ( double precision ) → double precision

反正弦,结果以度表示

asind(0.5)30

atan ( double precision ) → double precision

反正切,结果以弧度表示

atan(1)0.7853981633974483

atand ( double precision ) → double precision

反正切,结果以度表示

atand(1)45

atan2 ( y double precision, x double precision ) → double precision

反切 y/x,结果以弧度表示

atan2(1, 0)1.5707963267948966

atan2d ( y double precision, x double precision ) → double precision

反切 y/x,结果以度表示

atan2d(1, 0)90

cos ( double precision ) → double precision

余弦,参数以弧度表示

cos(0)1

cosd ( double precision ) → double precision

余弦,参数以度表示

cosd(60)0.5

cot ( double precision ) → double precision

余切,参数以弧度表示

cot(0.5)1.830487721712452

cotd ( double precision ) → double precision

余切,参数以度表示

cotd(45)1

sin ( double precision ) → double precision

正弦,参数以弧度表示

sin(1)0.8414709848078965

sind ( double precision ) → double precision

正弦,参数以度表示

sind(30)0.5

tan ( double precision ) → double precision

正切,参数以弧度表示

tan(1)1.5574077246549023

tand ( double precision ) → double precision

正切,参数以度表示

tand(45)1


注意

另一种处理以度为单位的角度的方法是使用单位转换函数 radians()degrees()(如前所示)。但是,推荐使用基于度的三角函数,这样可以避免在 sind(30) 等特殊情况下产生舍入误差。

表 9.8 显示了可用的双曲函数。

表 9.8. 双曲函数

函数

描述

示例

sinh ( double precision ) → double precision

双曲正弦

sinh(1)1.1752011936438014

cosh ( double precision ) → double precision

双曲余弦

cosh(0)1

tanh ( double precision ) → double precision

双曲正切

tanh(1)0.7615941559557649

asinh ( double precision ) → double precision

反双曲正弦

asinh(1)0.881373587019543

acosh ( double precision ) → double precision

反双曲余弦

acosh(1)0

atanh ( double precision ) → double precision

反双曲正切

atanh(0.5)0.5493061443340548


提交更正

如果您在文档中发现任何不正确之处、与您对特定功能的体验不符或需要进一步澄清的内容,请使用此表单报告文档问题。