↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 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 的操作符对所有的 smallint、integer、bigint、numeric、real 和 double precision类型都可用。显示为可接受 integral_type 的操作符对 smallint、integer 和 bigint类型是可用的。除了特别说明之处,操作符的每种形式都返回与其参数相同的数据类型。涉及多个参数数据类型的调用,例如 integer + numeric,可通过使用这些列表中稍后出现的类型来解析。

表 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


表 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(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

以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的余数;适用于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 的桶编号。该直方图把 low 到 high 的范围划分为 count 个等宽桶。各桶包含下边界、不包含上边界。对于小于 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


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

表 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)


在表 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


提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。英文原文本身的问题请通过上游文档表单反馈给 PostgreSQL 文档维护者。