数值函数
数值运算符
下表展示了可用于数值类型的数学运算符。
| 运算符 | 描述 | 示例 | 结果 |
|---|---|---|---|
+ | 加法 | 2 + 3 | 5 |
- | 减法 | 2 - 3 | -1 |
* | 乘法 | 2 * 3 | 6 |
/ | 浮点除法 | 5 / 2 | 2.5 |
// | 整数除法 | 5 // 2 | 2 |
% | 取模(余数) | 5 % 4 | 1 |
** | 幂运算 | 3 ** 4 | 81 |
^ | 幂运算(** 的别名) | 3 ^ 4 | 81 |
& | 按位与 | 91 & 15 | 11 |
| ` | ` | 按位或 | `32 |
<< | 按位左移 | 1 << 4 | 16 |
>> | 按位右移 | 8 >> 2 | 2 |
~ | 按位取反 | ~15 | -16 |
! | x 的阶乘 | 4! | 24 |
除法与取模运算符
有两种除法运算符:/ 和 //。
当至少一个操作数是 FLOAT 或 DOUBLE 时,两者等价。
当两个操作数都是整数时,/ 执行浮点除法(5 / 2 = 2.5),而 // 执行整数除法(5 // 2 = 2)。
支持的类型
取模、按位运算、按位取反和阶乘运算符仅适用于整型数据类型, 其余运算符适用于所有数值数据类型。
数值函数
下表展示可用的数学函数。
| 名称 | 描述 |
|---|---|
@(x) | 绝对值。若 x 是列名,括号可省略。 |
abs(x) | 绝对值。 |
acos(x) | 计算 x 的反余弦。 |
acosh(x) | 计算 x 的反双曲余弦。 |
add(x, y) | x + y 的别名。 |
asin(x) | 计算 x 的反正弦。 |
asinh(x) | 计算 x 的反双曲正弦。 |
atan(x) | 计算 x 的反正切。 |
atanh(x) | 计算 x 的反双曲正切。 |
atan2(y, x) | 计算 (y, x) 的反正切。 |
bit_count(x) | 返回置位(值为 1)的位数。 |
cbrt(x) | 返回该数的立方根。 |
ceil(x) | 向上取整。 |
ceiling(x) | 向上取整,ceil 的别名。 |
cos(x) | 计算 x 的余弦。 |
cot(x) | 计算 x 的余切。 |
degrees(x) | 将弧度转换为角度。 |
divide(x, y) | x // y 的别名。 |
even(x) | 按远离零方向舍入到下一个偶数。 |
exp(x) | 计算 e ** x。 |
factorial(x) | 参见 ! 运算符。计算当前整数及其以下所有整数的乘积。 |
fdiv(x, y) | 执行整数除法(x // y),但返回 DOUBLE 值。 |
floor(x) | 向下取整。 |
fmod(x, y) | 计算取模值。始终返回 DOUBLE 值。 |
gamma(x) | 对 x - 1 的阶乘进行插值,允许分数输入。 |
gcd(x, y) | 计算 x 与 y 的最大公约数。 |
greatest_common_divisor(x, y) | 计算 x 与 y 的最大公约数。 |
greatest(x1, x2, ...) | 选取最大值。 |
isfinite(x) | 若浮点值有限则返回 true,否则返回 false。 |
isinf(x) | 若浮点值为无穷则返回 true,否则返回 false。 |
isnan(x) | 若浮点值为 NaN 则返回 true,否则返回 false。 |
lcm(x, y) | 计算 x 与 y 的最小公倍数。 |
least_common_multiple(x, y) | 计算 x 与 y 的最小公倍数。 |
least(x1, x2, ...) | 选取最小值。 |
lgamma(x) | 计算 gamma 函数的对数。 |
ln(x) | 计算 x 的自然对数。 |
log(x) | 计算 x 的以 10 为底的对数。 |
log10(x) | log 的别名,计算 x 的以 10 为底的对数。 |
log2(x) | 计算 x 的以 2 为底对数。 |
multiply(x, y) | x * y 的别名。 |
nextafter(x, y) | 返回从 x 朝 y 方向的下一个浮点值。 |
pi() | 返回 pi 的值。 |
pow(x, y) | 计算 x 的 y 次幂。 |
power(x, y) | pow 的别名,计算 x 的 y 次幂。 |
radians(x) | 将角度转换为弧度。 |
random() | 返回范围 0.0 <= x < 1.0 内的随机数 x。 |
round_even(v NUMERIC, s INTEGER) | roundbankers(v, s) 的别名。按_四舍六入五成双_规则将值舍入到 s 位小数。允许 s < 0。 |
round(v NUMERIC, s INTEGER) | 将值舍入到 s 位小数。允许 s < 0。 |
setseed(x) | 设置随机函数使用的种子。 |
sign(x) | 返回 x 的符号:-1、0 或 1。 |
signbit(x) | 返回符号位是否被设置。 |
sin(x) | 计算 x 的正弦。 |
sqrt(x) | 返回该数的平方根。 |
subtract(x, y) | x - y 的别名。 |
tan(x) | 计算 x 的正切。 |
trunc(x) | 截断该数。 |
xor(x, y) | 按位异或。 |
@(x)
| Description | Absolute value. Parentheses are optional if x is a column name. |
| Example | @(-17.4) |
| Result | 17.4 |
| Alias | abs |
abs(x)
| Description | Absolute value. |
| Example | abs(-17.4) |
| Result | 17.4 |
| Alias | @ |
acos(x)
| Description | Computes the inverse cosine of x. |
| Example | acos(0.5) |
| Result | 1.0471975511965976 |
acosh(x)
| Description | Computes the inverse hyperbolic cosine of x. |
| Example | acosh(1.5) |
| Result | 0.9624236501192069 |
add(x, y)
| Description | Alias for x + y. |
| Example | add(2, 3) |
| Result | 5 |
asin(x)
| Description | Computes the inverse sine of x. |
| Example | asin(0.5) |
| Result | 0.5235987755982989 |
asinh(x)
| Description | Computes the inverse hyperbolic sine of x. |
| Example | asinh(0.5) |
| Result | 0.48121182505960347 |
atan(x)
| Description | Computes the inverse tangent of x. |
| Example | atan(0.5) |
| Result | 0.4636476090008061 |
atanh(x)
| Description | Computes the inverse hyperbolic tangent of x. |
| Example | atanh(0.5) |
| Result | 0.5493061443340549 |
atan2(y, x)
| Description | Computes the inverse tangent (y, x). |
| Example | atan2(0.5, 0.5) |
| Result | 0.7853981633974483 |
bit_count(x)
| Description | Returns the number of bits that are set. |
| Example | bit_count(31) |
| Result | 5 |
cbrt(x)
| Description | Returns the cube root of the number. |
| Example | cbrt(8) |
| Result | 2 |
ceil(x)
| Description | Rounds the number up. |
| Example | ceil(17.4) |
| Result | 18 |
ceiling(x)
| Description | Rounds the number up. Alias of ceil. |
| Example | ceiling(17.4) |
| Result | 18 |
cos(x)
| Description | Computes the cosine of x. |
| Example | cos(pi() / 3) |
| Result | 0.5000000000000001 |
cot(x)
| Description | Computes the cotangent of x. |
| Example | cot(0.5) |
| Result | 1.830487721712452 |
degrees(x)
| Description | Converts radians to degrees. |
| Example | degrees(pi()) |
| Result | 180 |
divide(x, y)
| Description | Alias for x // y. |
| Example | divide(5, 2) |
| Result | 2 |
even(x)
| Description | Round to next even number by rounding away from zero. |
| Example | even(2.9) |
| Result | 4 |
exp(x)
| Description | Computes e ** x. |
| Example | exp(0.693) |
| Result | 2 |
factorial(x)
| Description | See the ! operator. Computes the product of the current integer and all integers below it. |
| Example | factorial(4) |
| Result | 24 |
fdiv(x, y)
| Description | Performs integer division (x // y) but returns a DOUBLE value. |
| Example | fdiv(5, 2) |
| Result | 2.0 |
floor(x)
| Description | Rounds the number down. |
| Example | floor(17.4) |
| Result | 17 |
fmod(x, y)
| Description | Calculates the modulo value. Always returns a DOUBLE value. |
| Example | fmod(5, 2) |
| Result | 1.0 |
gamma(x)
| Description | Interpolation of the factorial of x - 1. Fractional inputs are allowed. |
| Example | gamma(5.5) |
| Result | 52.34277778455352 |
gcd(x, y)
| Description | Computes the greatest common divisor of x and y. |
| Example | gcd(42, 57) |
| Result | 3 |
greatest_common_divisor(x, y)
| Description | Computes the greatest common divisor of x and y. |
| Example | greatest_common_divisor(42, 57) |
| Result | 3 |
greatest(x1, x2, ...)
| Description | Selects the largest value. |
| Example | greatest(3, 2, 4, 4) |
| Result | 4 |
isfinite(x)
| Description | Returns true if the floating point value is finite, false otherwise. |
| Example | isfinite(5.5) |
| Result | true |
isinf(x)
| Description | Returns true if the floating point value is infinite, false otherwise. |
| Example | isinf('Infinity'::float) |
| Result | true |
isnan(x)
| Description | Returns true if the floating point value is not a number, false otherwise. |
| Example | isnan('NaN'::float) |
| Result | true |
lcm(x, y)
| Description | Computes the least common multiple of x and y. |
| Example | lcm(42, 57) |
| Result | 798 |
least_common_multiple(x, y)
| Description | Computes the least common multiple of x and y. |
| Example | least_common_multiple(42, 57) |
| Result | 798 |
least(x1, x2, ...)
| Description | Selects the smallest value. |
| Example | least(3, 2, 4, 4) |
| Result | 2 |
lgamma(x)
| Description | Computes the log of the gamma function. |
| Example | lgamma(2) |
| Result | 0 |
ln(x)
| Description | Computes the natural logarithm of x. |
| Example | ln(2) |
| Result | 0.693 |
log(x)
| Description | Computes the base-10 log of x. |
| Example | log(100) |
| Result | 2 |
log10(x)
| Description | Alias of log. Computes the base-10 log of x. |
| Example | log10(1000) |
| Result | 3 |
log2(x)
| Description | Computes the base-2 log of x. |
| Example | log2(8) |
| Result | 3 |
multiply(x, y)
| Description | Alias for x * y. |
| Example | multiply(2, 3) |
| Result | 6 |
nextafter(x, y)
| Description | Return the next floating point value after x in the direction of y. |
| Example | nextafter(1::float, 2::float) |
| Result | 1.0000001 |
pi()
| Description | Returns the value of pi. |
| Example | pi() |
| Result | 3.141592653589793 |
pow(x, y)
| Description | Computes x to the power of y. |
| Example | pow(2, 3) |
| Result | 8 |
power(x, y)
| Description | Alias of pow. Computes x to the power of y. |
| Example | power(2, 3) |
| Result | 8 |
radians(x)
| Description | Converts degrees to radians. |
| Example | radians(90) |
| Result | 1.5707963267948966 |
random()
| Description | Returns a random number x in the range 0.0 <= x < 1.0. |
| Example | random() |
| Result | various |
round_even(v NUMERIC, s INTEGER)
| Description | Alias of roundbankers(v, s). Round to s decimal places using the rounding half to even rule. Values s < 0 are allowed. |
| Example | round_even(24.5, 0) |
| Result | 24.0 |
round(v NUMERIC, s INTEGER)
| Description | Round to s decimal places. Values s < 0 are allowed. |
| Example | round(42.4332, 2) |
| Result | 42.43 |
setseed(x)
| Description | Sets the seed to be used for the random function. |
| Example | setseed(0.42) |
sign(x)
| Description | Returns the sign of x as -1, 0 or 1. |
| Example | sign(-349) |
| Result | -1 |
signbit(x)
| Description | Returns whether the signbit is set or not. |
| Example | signbit(-1.0) |
| Result | true |
sin(x)
| Description | Computes the sin of x. |
| Example | sin(pi() / 6) |
| Result | 0.49999999999999994 |
sqrt(x)
| Description | Returns the square root of the number. |
| Example | sqrt(9) |
| Result | 3 |
subtract(x, y)
| Description | Alias for x - y. |
| Example | subtract(2, 3) |
| Result | -1 |
tan(x)
| Description | Computes the tangent of x. |
| Example | tan(pi() / 4) |
| Result | 0.9999999999999999 |
trunc(x)
| Description | Truncates the number. |
| Example | trunc(17.4) |
| Result | 17 |
xor(x, y)
| Description | Bitwise XOR. |
| Example | xor(17, 5) |
| Result | 20 |