9.3 9.4 9.5 9.6 10 11 12 13 14
阿里云PostgreSQL 问题报告 纠错本页面

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

|/ double precisiondouble precision

平方根

|/ 25.05

||/ double precisiondouble precision

立方根

||/ 64.04

bigint !numeric

阶乘(已弃用, 使用 factorial() 代替)

5 !120

!! bigintnumeric

阶乘作为前缀操作符(已弃用, 使用 factorial() 代替)

!! 5120

@ numeric_typenumeric_type

绝对值

@ -5.05

integral_type & integral_typeintegral_type

按位与(AND)

91 & 1511

integral_type | integral_typeintegral_type

按位或(OR)

32 | 335

integral_type # integral_typeintegral_type

按位异或(exclusive OR)

17 # 520

~ integral_typeintegral_type

按位求反(NOT)

~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

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

gcd ( numeric_type, numeric_type ) → numeric_type

最大公约数 (能将两个输入数整除而无余数的最大正数); 如果两个输入为零则返回 0 ; 适用于 integer, bigint,和 numeric

gcd(1071, 462)21

lcm ( numeric_type, numeric_type ) → numeric_type

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

lcm(1071, 462)23562

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.0000000000

min_scale ( numeric ) → integer

精确表示所提供值所需的最小刻度(小数位数)

min_scale(8.4100)2

mod ( y numeric_type, x numeric_type ) → numeric_type

y/x的余数; 适用于smallintintegerbigint、和 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

ab次幂

power(9, 3)729

radians ( double precision ) → double precision

将角度转换为弧度

radians(45.0)0.7853981633974483

round ( numeric ) → numeric

round ( double precision ) → double precision

四舍五入到最近的整数

round(42.4)42

round ( v numeric, s integer ) → numeric

v 四舍五入到 s 位小数

round(42.4382, 2)42.44

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

截断 vs 位小数位置的数字

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等宽柱的柱状图中operand所在的柱的编号,范围从lowhigh。 超出该范围的输入则返回0计数+1

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket ( operand anyelement, thresholds anyarray ) → integer

返回一个柱号,这个柱是在给定数组中operand将被分配的柱。 对于一个低于第一个下界的输入返回0operand和数组元素可以是具有标准比较操作符的任何类型。 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

setseed ( double precision ) → void

为后续的random()调用设置种子;参数必须在-1.0和1.0之间,包括边界值

setseed(0.12345)


random()函数使用了一个简单的线性共轭算法。 它的速度很快,但不适合于密码学应用;关于更安全的替代方案,请参阅 pgcrypto模块。 如果setseed()被调用,那么当前会话中的一系列后续random()调用的结果能够通过使用相同的参数重新发布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