Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

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

按位 AND

91 & 1511

integral_type | integral_typeintegral_type

按位 OR

32 | 335

integral_type # integral_typeintegral_type

按位异或

17 # 520

~ integral_typeintegral_type

按位 NOT

~1-2

integral_type << integerintegral_type

按位左移

1 << 416

integral_type >> integerintegral_type

按位右移

8 >> 22


表 9.5显示了可用的数学函数。其中很多函数都提供了多种形式,带有不同参数类型。除另行注明的以外,任何给定形式的函数都会返回与其参数相同的数据类型;跨类型方面的解决方式与上述针对运算符的解释相同。使用双精度数据工作的函数大部分是在主机系统的 C 库之上实现的;因此,边界情况中的准确性和行为会根据主机系统而有所不同。

表 9.5 数学函数

函数

说明

示例

abs ( numeric_type ) → numeric_type

绝对值

abs(-17.4)17.4

cbrt ( 双精度 ) → 双精度

立方根

cbrt(64.0)4

ceil ( 数值 ) → 数值

ceil ( 双精度 ) → 双精度

大于或等于参数的整数

ceil(42.2)43

ceil(-42.8)-42

ceiling ( 数值 ) → 数值

ceiling ( 双精度 ) → 双精度

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

ceiling(95.3)96

degrees ( 双精度 ) → 双精度

将弧度转换为度

degrees(0.5)28.64788975654116

div ( y 数值, x 数值 ) → 数值

y/x 的整数商(朝零截断)

div(9, 4)2

erf ( 双精度 ) → 双精度

错误函数

erf(1.0)0.8427007929497149

erfc ( 双精度 ) → 双精度

互补错误函数 (1 - erf(x),对大型输入而言不会丢失精度)

erfc(1.0)0.15729920705028513

exp ( 数值 ) → 数值

exp ( 双精度 ) → 双精度

指数 (e 乘以给定幂)

exp(1.0)2.7182818284590452

factorial ( bigint ) → 数值

阶乘

factorial(5)120

floor ( 数值 ) → 数值

floor ( 双精度 ) → 双精度

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

floor(42.8)42

floor(-42.8)-43

gcd ( numeric_type, numeric_type ) → numeric_type

最大公约数(同时整除这两个输入且无余数的最大正整数);如果两个输入均为 0,则返回 0;对于 整数bigint数值 可用

gcd(1071, 462)21

lcm ( numeric_type, numeric_type ) → numeric_type

最小公倍数(同时是两个输入的整数倍的最小正整数);如果任何一个输入为 0,则返回 0;对于 整数bigint数值 可用

lcm(1071, 462)23562

ln ( 数值 ) → 数值

ln ( 双精度 ) → 双精度

自然对数

ln(2.0)0.6931471805599453

log ( 数字类型 ) → 数字类型

log ( 双精度 ) → 双精度

以 10 为底的对数

log(100)2

log10 ( 数字类型 ) → 数字类型

log10 ( 双精度 ) → 双精度

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

log10(1000)3

log ( b 数字类型, x 数字类型 ) → 数字类型

xb 为底的对数

log(2.0, 64.0)6.0000000000000000

min_scale ( 数字类型 ) → 整数

精确表示所提供值所需的最小比例(小数点后小数位数)

min_scale(8.4100)2

mod ( y 数字类型, x 数字类型 ) → 数字类型

y/x 的余数;适用于 smallintintegerbigintnumeric

mod(9, 4)1

pi ( ) → 双精度

π 的近似值

pi()3.141592653589793

power ( a 数字类型, b 数字类型 ) → 数字类型

power ( a 双精度, b 双精度 ) → 双精度

a 乘方为 b

power(9, 3)729

radians ( 双精度 ) → 双精度

将度数转换为弧度

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 精度 ) → double 精度

截断为整数(朝零方向)

trunc(42.8)42

trunc(-42.8)-42

trunc ( v 数字, s 整数 ) → 数字

v 截断为 s 位小数

trunc(42.4382, 2)42.43

width_bucket ( 操作数 数字, low 数字, high 数字, count 整数 ) → 整数

width_bucket ( 操作数 double 精度, low double 精度, high double 精度, count 整数 ) → 整数

返回要使 操作数 位于范围 lowhigh 的直方图中包含 count 个等宽层放置的层号。对于超出该范围的输入,将返回 0count+1

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket ( 操作数 anycompatible, thresholds anycompatiblearray ) → 整数

返回放置 操作数 的桶的编号,其中数组列出了桶的下界。对于小于第一个下界的输入,将返回 0操作数 和数组元素可以是具有标准比较运算符的任何类型。thresholds 数组必须按升序排列,否则将会获得意外结果。

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


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

表 9.6。随机函数

函数

说明

示例

random ( ) → 双精度

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

random()0.897124072839091

random ( min 整数, max 整数 ) → 整数

random ( min 大整数, max 大整数 ) → 大整数

random ( min 数值, max 数值 ) → 数值

返回 min <= x <= max 范围内的随机值。对于类型 numeric,结果将具有与 minmax(以具有的小数部分更多的为准)相同数量的小数部分。

random(1, 10)7

random(-0.499, 0.499)0.347

random_normal ( [ mean 双精度 [, stddev 双精度 ]] ) → 双精度

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

random_normal(0.0, 1.0)0.051285419

setseed ( 双精度 ) → 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 ( 双精度 ) → 双精度

反余弦,以弧度为单位

acos(1)0

acosd ( 双精度 ) → 双精度

反余弦,以度为单位

acosd(0.5)60

asin ( 双精度 ) → 双精度

反正弦,以弧度为单位

asin(1)1.5707963267948966

asind ( 双精度 ) → 双精度

反正弦,以度为单位

asind(0.5)30

atan ( 双精度 ) → 双精度

反正切,以弧度为单位

atan(1)0.7853981633974483

atand ( 双精度 ) → 双精度

反正切,以度为单位

atand(1)45

atan2 ( y 双精度, x 双精度 ) → 双精度

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

atan2(1, 0)1.5707963267948966

atan2d ( y 双精度, x 双精度 ) → 双精度

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

atan2d(1, 0)90

cos ( 双精度 ) → 双精度

余弦,自变量以弧度表示

cos(0)1

cosd ( 双精度 ) → 双精度

余弦,自变量以度表示

cosd(60)0.5

cot ( 双精度 ) → 双精度

余切,自变量以弧度表示

cot(0.5)1.830487721712452

cotd ( 双精度 ) → 双精度

余切,自变量以度数表示

cotd(45)1

sin ( 双精度 ) → 双精度

正弦,自变量以弧度表示

sin(1)0.8414709848078965

sind ( 双精度 ) → 双精度

正弦,自变量以度数表示

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