PostgreSQL 中文参考文档
🌐 英文原版 官方 ↗

9.3. 数学函数与操作符 #

PostgreSQL 为许多数据类型提供了丰富的数学操作符。对于不具备标准数学惯例的数据类型(例如日期/时间类型),我们将在后续专门小节中详述其具体行为。

Table 9.4 列出了适用于标准数值类型的数学操作符。除另有说明外,标注接受 numeric_type 的操作符均适用于 smallint、integer、bigint、numeric、real 以及 double precision 类型。标注接受 integral_type 的操作符适用于整数类型 smallint、integer 和 bigint。除特别注明外,每种操作符形式均返回与其参数相同的数据类型。涉及混合多参数类型的调用(例如 integer + numeric),将依据上述列表中位置靠后的较通用类型来决定返回类型。

Table 9.4. 数学操作符

操作符

说明

示例

numeric_type + numeric_type → numeric_type

加法

2 + 3 → 5

+ numeric_type → numeric_type

一元正号(无操作)

+ 3.5 → 3.5

numeric_type - numeric_type → numeric_type

减法

2 - 3 → -1

- numeric_type → numeric_type

求相反数(一元负号)

- (-4) → 4

numeric_type * numeric_type → numeric_type

乘法

2 * 3 → 6

numeric_type / numeric_type → numeric_type

除法(对于整数类型,除法向零截断小数部分)

5.0 / 2 → 2.5000000000000000

5 / 2 → 2

(-5) / 2 → -2

numeric_type % numeric_type → numeric_type

取模(求余数);适用于 smallint、integer、bigint 和 numeric

5 % 4 → 1

numeric ^ numeric → numeric

double precision ^ double precision → double precision

求幂运算

2 ^ 3 → 8

与常规数学书写惯例不同,连续使用多个 ^ 默认从左向右结合:

2 ^ 3 ^ 3 → 512

2 ^ (3 ^ 3) → 134217728

|/ double precision → double precision

平方根

|/ 25.0 → 5

||/ double precision → double precision

立方根

||/ 64.0 → 4

@ numeric_type → numeric_type

绝对值

@ -5.0 → 5.0

integral_type & integral_type → integral_type

按位与

91 & 15 → 11

integral_type | integral_type → integral_type

按位或

32 | 3 → 35

integral_type # integral_type → integral_type

按位异或

17 # 5 → 20

~ integral_type → integral_type

按位取反

~1 → -2

integral_type << integer → integral_type

按位左移

1 << 4 → 16

integral_type >> integer → integral_type

按位右移

8 >> 2 → 2


Table 9.5 展示了可用的数学函数。 其中许多函数以多种形式提供, 带有不同的参数类型。 除非特别说明, 函数的任何给定形式都返回与其参数相同的数据类型; 跨类型的情况以上文针对操作符解释的相同方式解决。 处理 double precision 数据的函数大多构建在宿主系统的 C 库之上; 因此在边界情况下的精度和行为可能因宿主系统而异。

Table 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; 可用于 integer、 bigint 和 numeric

gcd(1071, 462) → 21

lcm ( numeric_type, numeric_type ) → numeric_type

最小公倍数 (是两个输入的整数倍的严格最小正数); 如果任一输入为零则返回 0; 可用于 integer、 bigint 和 numeric

lcm(1071, 462) → 23562

lgamma ( double precision ) → double precision

伽马函数绝对值的自然对数

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

x 以 b 为底的对数

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 的余数; 可用于 smallint、 integer、 bigint 和 numeric

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

a 的 b 次幂

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

返回 operand 在一个直方图中落入的桶的编号, 该直方图具有 count 个等宽的桶, 覆盖从 low 到 high 的范围。 这些桶的下界是包含的, 上界是不包含的。 对于小于 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 落入的桶的编号。 对于小于第一个下界的输入返回 0。 operand 和数组元素可以是任何拥有标准比较操作符的类型。 thresholds 数组必须被排序, 最小的在前, 否则会得到意外的结果。

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


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

Table 9.6. 随机数函数

函数

说明

示例

random ( ) → double precision

返回范围 0.0 <= x < 1.0 内的一个随机值

random() → 0.897124072839091

random ( min integer, max integer ) → integer

random ( min bigint, max bigint ) → bigint

random ( min numeric, max numeric ) → numeric

返回范围 min <= x <= max 内的一个随机值。 对于 numeric 类型, 结果将具有与 min 或 max 中小数位较多者相同的小数位数。

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)


Table 9.6 中列出的 random() 和 random_normal() 函数使用一个确定性的伪随机数生成器。 它速度很快, 但不适合加密应用; 更安全的替代方案请参见 pgcrypto 模块。 如果调用了 setseed(), 那么当前会话中后续对这些函数调用所产生的结果序列, 可以通过用相同的参数重新发出 setseed() 来重现。 如果在同一会话中没有任何先前的 setseed() 调用, 那么对其中任何一个函数的第一次调用都会从一个平台相关的随机位来源 获取种子。

Table 9.7 展示了可用的三角函数。 这些函数中的每一个都有两种变体, 一种以弧度度量角度, 另一种以度数度量角度。

Table 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


Note

处理以度数度量的角度的另一种方式, 是使用前面展示的单位转换函数 radians() 和 degrees()。 不过, 优先的做法是使用基于度数的三角函数, 因为这样可以避免诸如 sind(30) 之类的特殊情形出现舍入误差。

Table 9.8 展示了可用的双曲函数。

Table 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