9.3. math 函数和运算符

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

Table 9.4显示可用的 math 运算符。

表 9.4. math 运算符

OperatorDescriptionExampleResult
+addition2 + 35
-subtraction2 - 3-1
*multiplication2 * 36
/除法(整数除法将结果截断)4 / 22
%modulo (remainder)5 % 41
^求幂(从左到右关联)2.0 ^ 3.08
|/square root|/ 25.05
||/cube root||/ 27.03
!factorial5 !120
!!阶乘(前缀运算符)!! 5120
@absolute value@ -5.05
&bitwise AND91 & 1511
|bitwise OR32 | 335
#bitwise XOR17 # 520
~bitwise NOT~1-2
<<左移1 << 416
>>右移8 >> 22

按位运算符仅适用于整数数据类型,也可用于位串类型bitbit varying,如Table 9.13所示。

Table 9.5显示可用的 math 函数。在表中,dp表示double precision。这些功能中的许多功能都以不同的参数类型以多种形式提供。除非另有说明,否则函数的任何给定形式都将返回与其参数相同的数据类型。处理double precision数据的功能大部分是在主机系统的 C 库的顶部实现的;因此,边界情况下的准确性和行为可能会因主机系统而异。

表 9.5. math 函数

FunctionReturn TypeDescriptionExampleResult
abs(x)(与 Importing 相同)absolute valueabs(-17.4)17.4
cbrt(dp)dpcube rootcbrt(27.0)3
ceil(dp or numeric)(与 Importing 相同)大于或等于参数的最接近整数ceil(-42.8)-42
ceiling(dp or numeric)(与 Importing 相同)大于或等于参数的最接近整数(与ceil相同)ceiling(-95.3)-95
degrees(dp)dp弧度到度degrees(0.5)28.6478897565412
div(y numeric, x numeric)numeric* y / x *的整数商div(9,4)2
exp(dp or numeric)(与 Importing 相同)exponentialexp(1.0)2.71828182845905
floor(dp or numeric)(与 Importing 相同)小于或等于参数的最接近整数floor(-42.8)-43
ln(dp or numeric)(与 Importing 相同)natural logarithmln(2.0)0.693147180559945
log(dp or numeric)(与 Importing 相同)以 10 为底的对数log(100.0)2
log(b numeric, x numeric)numeric以对数为底的对数* b *log(2.0, 64.0)6.0000000000
mod(y, x)(与参数类型相同)* y / x *的其余部分mod(9,4)1
pi()dp"π" constantpi()3.14159265358979
power(a dp, b dp)dp* a 提升为 b *power(9.0, 3.0)729
power(a numeric, b numeric)numeric* a 提升为 b *power(9.0, 3.0)729
radians(dp)dp度到弧度radians(45.0)0.785398163397448
round(dp or numeric)(与 Importing 相同)舍入到最接近的整数round(42.4)42
round(v numeric, s int)numeric舍入到* s *小数位round(42.4382, 2)42.44
scale(numeric)integer参数的小数位数(小数部分中的小数位数)scale(8.41)2
sign(dp or numeric)(与 Importing 相同)参数的符号(-1、0、1)sign(-8.4)-1
sqrt(dp or numeric)(与 Importing 相同)square rootsqrt(2.0)1.4142135623731
trunc(dp or numeric)(与 Importing 相同)截断为零trunc(42.8)42
trunc(v numeric, s int)numeric截断到* s *小数位trunc(42.4382, 2)42.43
width_bucket(operand dp, b1 dp, b2 dp, count int)int返回在直方图中将* operand 分配给的存储桶编号,该直方图中具有 count 等宽宽度的存储桶,范围在 b1 b2 *范围内;对于超出范围的 Importing,返回0count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand numeric, b1 numeric, b2 numeric, count int)int返回在直方图中将* operand 分配给的存储桶编号,该直方图中具有 count 等宽宽度的存储桶,范围在 b1 b2 *范围内;对于超出范围的 Importing,返回0count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand anyelement, thresholds anyarray)int给定一个列出存储桶下限的数组,返回将被分配* operand 的存储桶号;对于小于第一个下限的 Importing,返回0; * thresholds 数组必须排序,最小的优先,否则将获得意外的结果width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2

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

表 9.6. 随机函数

FunctionReturn TypeDescription
random()dp范围内的随机值 0.0 <= x <1.0
setseed(dp)void为后续的random()调用设置种子(值介于-1.0 和 1.0 之间,包括-1.0 和 1.0)

random()返回的值的 Feature 取决于系统实现。它不适用于加密应用程序;另请参见pgcrypto模块。

最后,Table 9.7显示了可用的三角函数。所有三角函数均采用参数并返回double precision类型的值。每个三角函数都有两种变体,一种以弧度为单位,另一种以度为单位。

表 9.7. 三角函数

Function (radians)Function (degrees)Description
acos(x) acosd(x)inverse cosine
asin(x) asind(x)inverse sine
atan(x) atand(x)inverse tangent
atan2(y, x) atan2d(y, x)y/x的反正切
cos(x) cosd(x)cosine
cot(x) cotd(x)cotangent
sin(x) sind(x)sine
tan(x) tand(x)tangent

Note

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