9.3. math 函数和运算符
提供了许多 PostgreSQL 类型的 math 运算符。对于没有标准 math 约定的类型(例如,日期/时间类型),我们将在后续部分中描述实际行为。
Table 9.4显示可用的 math 运算符。
表 9.4. math 运算符
Operator |
Description |
Example |
Result |
+ |
addition |
2 + 3 |
5 |
- |
subtraction |
2 - 3 |
-1 |
* |
multiplication |
2 * 3 |
6 |
/ |
除法(整数除法将结果截断) |
4 / 2 |
2 |
% |
modulo (remainder) |
5 % 4 |
1 |
^ |
求幂(从左到右关联) |
2.0 ^ 3.0 |
8 |
|/ |
square root |
|/ 25.0 |
5 |
||/ |
cube root |
||/ 27.0 |
3 |
! |
factorial |
5 ! |
120 |
!! |
阶乘(前缀运算符) |
!! 5 |
120 |
@ |
absolute value |
@ -5.0 |
5 |
& |
bitwise AND |
91 & 15 |
11 |
| |
bitwise OR |
32 | 3 |
35 |
# |
bitwise XOR |
17 # 5 |
20 |
~ |
bitwise NOT |
~1 |
-2 |
<< |
左移 |
1 << 4 |
16 |
>> |
右移 |
8 >> 2 |
2 |
按位运算符仅适用于整数数据类型,也可用于位串类型bit
和bit varying
,如Table 9.13所示。
Table 9.5显示可用的 math 函数。在表中,dp
表示double precision
。这些功能中的许多功能都以不同的参数类型以多种形式提供。除非另有说明,否则函数的任何给定形式都将返回与其参数相同的数据类型。处理double precision
数据的功能大部分是在主机系统的 C 库的顶部实现的;因此,边界情况下的准确性和行为可能会因主机系统而异。
表 9.5. math 函数
Function |
Return Type |
Description |
Example |
Result |
abs(x) |
(与 Importing 相同) |
absolute value |
abs(-17.4) |
17.4 |
cbrt(dp) |
dp |
cube root |
cbrt(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 相同) |
exponential |
exp(1.0) |
2.71828182845905 |
floor(dp or numeric) |
(与 Importing 相同) |
小于或等于参数的最接近整数 |
floor(-42.8) |
-43 |
ln(dp or numeric) |
(与 Importing 相同) |
natural logarithm |
ln(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 |
"π" constant |
pi() |
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 root |
sqrt(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,返回0 或count+1 |
width_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,返回0 或count+1 |
width_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. 随机函数
Function |
Return Type |
Description |
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)
等特殊情况下的舍入误差。