数字类型
Name | Storage Size | Description | Range |
int2 | 2 bytes | small-range integer | -32768 to +32767 |
int4 | 4 bytes | typical choice for integer | -2147483648 to +2147483647 |
int8 | 8 bytes | large-range integer | -9223372036854775808 to 9223372036854775807 |
decimal | variable | user-specified precision,exact | up to 131072 digits before the decimal point; up to 16383 digits after the decimal point |
float4 | 4 bytes | variable-precision,inexact | 6 decimal digits precision |
float8 | 8 bytes | variable-precision,inexact | 15 decimal digits precision |
serial2 | 2 bytes | small autoincrementing integer | 1 to 32767 |
serial4 | 4 bytes | autoincrementing integer | 1 to 2147483647 |
serial8 | 8 bytes | large autoincrementing integer | 1 to 9223372036854775807 |
常用数字操作符
操作符 | 描述 | 例子 | 结果 |
+ | 加 | 2 + 3 | 5 |
- | 减 | 2 - 3 | -1 |
* | 乘 | 2 * 3 | 6 |
/ | 除 | 4 / 2 | 2 |
% | 模 | 5 % 4 | 1 |
^ | 幂 | 2.0 ^ 3.0 | 8 |
|/ | 平方根 | |/ 25.0 | 5 |
||/ | 立方根 | ||/ 27.0 | 3 |
! | 阶乘 | 5 ! | 120 |
!! | 阶乘 | !! 5 | 120 |
@ | 绝对值 | @ -5.0 | 5 |
& | 按位AND | 91 & 15 | 11 |
| | 按位OR | 32 | 3 | 35 |
# | 按位XOR | 17 # 5 | 20 |
~ | 按位NOT | ~1 | -2 |
<< | 按位左移 | 1 << 4 | 16 |
>> | 按位右移 | 8 >> 2 | 2 |
三角函数
函数 | 描述 |
acos(x) | 反余弦 |
asin(x) | 反正弦 |
atan(x) | 反正切 |
atan2(x, y) | 正切 y/x 的反函数 |
cos(x) | 余弦 |
cot(x) | 余切 |
sin(x) | 正弦 |
tan(x) | 正切 |
数值运算函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
abs(x) |
| 绝对值 | abs(-17.4) | 17.4 |
cbrt(double) |
| 立方根 | cbrt(27.0) | 3 |
ceil(double/numeric) |
| 不小于参数的最小的整数 | ceil(-42.8) | -42 |
degrees(double) |
| 把弧度转为角度 | degrees(0.5) | 28.6478897565412 |
exp(double/numeric) |
| 自然指数 | exp(1.0) | 2.71828182845905 |
floor(double/numeric) |
| 不大于参数的最大整数 | floor(-42.8) | -43 |
ln(double/numeric) |
| 自然对数 | ln(2.0) | 0.693147180559945 |
log(double/numeric) |
| 10为底的对数 | log(100.0) | 2 |
log(b numeric,x numeric) |
| numeric指定底数的对数 | log(2.0, 64.0) | 6.0000000000 |
mod(y, x) |
| 取余数 | mod(9,4) | 1 |
pi() | double | "π"常量 | pi() | 3.14159265358979 |
power(a double, b double) | double | 求a的b次幂 | power(9.0, 3.0) | 729 |
power(a numeric, b numeric) | numeric | 求a的b次幂 | power(9.0, 3.0) | 729 |
radians(double) | double | 把角度转为弧度 | radians(45.0) | 0.785398163397448 |
random() | double | 0.0到1.0之间的随机数值 | random() |
|
round(double/numeric) |
| 圆整为最接近的整数 | round(42.4) | 42 |
round(v numeric, s int) | numeric | 圆整为s位小数数字 | round(42.438,2) | 42.44 |
sign(double/numeric) |
| 参数的符号(-1,0,+1) | sign(-8.4) | -1 |
sqrt(double/numeric) |
| 平方根 | sqrt(2.0) | 1.4142135623731 |
trunc(double/numeric) |
| 截断(向零靠近) | trunc(42.8) | 42 |
trunc(v numeric, s int) | numeric | 截断为s小数位置的数字 | trunc(42.438,2) | |
coalesce(null ,0) | 将空转换为0 | coalesce(null,0) | 0 |