Numeric Functions
Numeric Operators
The table below shows the available mathematical operators for numeric types.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 2 + 3 | 5 |
- | Subtraction | 2 - 3 | -1 |
* | Multiplication | 2 * 3 | 6 |
/ | Float division | 5 / 2 | 2.5 |
// | Division | 5 // 2 | 2 |
% | Modulo (remainder) | 5 % 4 | 1 |
** | Exponent | 3 ** 4 | 81 |
^ | Exponent (alias for **) | 3 ^ 4 | 81 |
& | Bitwise AND | 91 & 15 | 11 |
| | Bitwise OR | 32 | 3 | 35 |
<< | Bitwise shift left | 1 << 4 | 16 |
>> | Bitwise shift right | 8 >> 2 | 2 |
~ | Bitwise negation | ~15 | -16 |
! | Factorial of x | 4! | 24 |
Division and Modulo Operators
There are two division operators: / and //.
They are equivalent when at least one of the operands is a FLOAT or a DOUBLE.
When both operands are integers, / performs floating points division (5 / 2 = 2.5) while // performs integer division (5 // 2 = 2).
Supported Types
The modulo, bitwise, negation, and factorial operators work only on integral data types, whereas the others are available for all numeric data types.
Numeric Functions
The table below shows the available mathematical functions.
| Name | Description |
|---|---|
@(x) | Absolute value. Parentheses are optional if x is a column name. |
abs(x) | Absolute value. |
acos(x) | Computes the inverse cosine of x. |
acosh(x) | Computes the inverse hyperbolic cosine of x. |
add(x, y) | Alias for x + y. |
asin(x) | Computes the inverse sine of x. |
asinh(x) | Computes the inverse hyperbolic sine of x. |
atan(x) | Computes the inverse tangent of x. |
atanh(x) | Computes the inverse hyperbolic tangent of x. |
atan2(y, x) | Computes the inverse tangent of (y, x). |
bit_count(x) | Returns the number of bits that are set. |
cbrt(x) | Returns the cube root of the number. |
ceil(x) | Rounds the number up. |
ceiling(x) | Rounds the number up. Alias of ceil. |
cos(x) | Computes the cosine of x. |
cot(x) | Computes the cotangent of x. |
degrees(x) | Converts radians to degrees. |
divide(x, y) | Alias for x // y. |
even(x) | Round to next even number by rounding away from zero. |
exp(x) | Computes e ** x. |
factorial(x) | See the ! operator. Computes the product of the current integer and all integers below it. |
fdiv(x, y) | Performs integer division (x // y) but returns a DOUBLE value. |
floor(x) | Rounds the number down. |
fmod(x, y) | Calculates the modulo value. Always returns a DOUBLE value. |
gamma(x) | Interpolation of the factorial of x - 1. Fractional inputs are allowed. |
gcd(x, y) | Computes the greatest common divisor of x and y. |
greatest_common_divisor(x, y) | Computes the greatest common divisor of x and y. |
greatest(x1, x2, ...) | Selects the largest value. |
isfinite(x) | Returns true if the floating point value is finite, false otherwise. |
isinf(x) | Returns true if the floating point value is infinite, false otherwise. |
isnan(x) | Returns true if the floating point value is not a number, false otherwise. |
lcm(x, y) | Computes the least common multiple of x and y. |
least_common_multiple(x, y) | Computes the least common multiple of x and y. |
least(x1, x2, ...) | Selects the smallest value. |
lgamma(x) | Computes the log of the gamma function. |
ln(x) | Computes the natural logarithm of x. |
log(x) | Computes the base-10 logarithm of x. |
log10(x) | Alias of log. Computes the base-10 logarithm of x. |
log2(x) | Computes the base-2 log of x. |
multiply(x, y) | Alias for x * y. |
nextafter(x, y) | Return the next floating point value after x in the direction of y. |
pi() | Returns the value of pi. |
pow(x, y) | Computes x to the power of y. |
power(x, y) | Alias of pow. Computes x to the power of y. |
radians(x) | Converts degrees to radians. |
random() | Returns a random number x in the range 0.0 <= x < 1.0. |
round_even(v NUMERIC, s INTEGER) | Alias of roundbankers(v, s). Round to s decimal places using the rounding half to even rule. Values s < 0 are allowed. |
roundbankers(v NUMERIC, s INTEGER) | Alias of round_even(v, s). Round to s decimal places using the rounding half to even rule. Values s < 0 are allowed. |
round(v NUMERIC, s INTEGER) | Round to s decimal places. Values s < 0 are allowed. |
setseed(x) | Sets the seed to be used for the random function. |
sign(x) | Returns the sign of x as -1, 0 or 1. |
signbit(x) | Returns whether the signbit is set or not. |
sin(x) | Computes the sin of x. |
sqrt(x) | Returns the square root of the number. |
subtract(x, y) | Alias for x - y. |
tan(x) | Computes the tangent of x. |
trunc(x) | Truncates the number. |
xor(x, y) | Bitwise XOR. |
@(x)
| Description | Absolute value. Parentheses are optional if x is a column name. |
|---|---|
| Example | @(-17.4) |
| Result | 17.4 |
| Alias | abs |
abs(x)
Absolute value. Alias: @.
SELECT abs(-17.4) AS abs; abs------ 17.4acos(x)
Computes the inverse cosine of x.
SELECT acos(0.5) AS acos; acos-------------------- 1.0471975511965979acosh(x)
Computes the inverse hyperbolic cosine of x.
SELECT acosh(1.5) AS acosh; acosh-------------------- 0.9624236501192069add(x, y)
Alias for x + y.
SELECT add(2, 3) AS add; add----- 5asin(x)
Computes the inverse sine of x.
SELECT asin(0.5) AS asin; asin-------------------- 0.5235987755982989asinh(x)
Computes the inverse hyperbolic sine of x.
SELECT asinh(0.5) AS asinh; asinh--------------------- 0.48121182505960347atan(x)
Computes the inverse tangent of x.
SELECT atan(0.5) AS atan; atan-------------------- 0.4636476090008061atanh(x)
Computes the inverse hyperbolic tangent of x.
SELECT atanh(0.5) AS atanh; atanh-------------------- 0.5493061443340548atan2(y, x)
Computes the inverse tangent of (y, x).
SELECT atan2(0.5, 0.5) AS atan2; atan2-------------------- 0.7853981633974483bit_count(x)
Returns the number of bits that are set.
SELECT bit_count(31) AS bit_count; bit_count----------- 5cbrt(x)
Returns the cube root of the number.
SELECT cbrt(8) AS cbrt; cbrt------ 2ceil(x)
Rounds the number up.
SELECT ceil(17.4) AS ceil; ceil------ 18ceiling(x)
Rounds the number up. Alias of ceil.
SELECT ceiling(17.4) AS ceiling; ceiling--------- 18cos(x)
Computes the cosine of x.
SELECT cos(pi() / 3) AS cos; cos-------------------- 0.5000000000000001cot(x)
Computes the cotangent of x.
SELECT cot(0.5) AS cot; cot------------------- 1.830487721712452degrees(x)
Converts radians to degrees.
SELECT degrees(pi()) AS degrees; degrees--------- 180divide(x, y)
Alias for x // y.
SELECT divide(5, 2) AS divide; divide-------- 2even(x)
Round to next even number by rounding away from zero.
SELECT even(2.9) AS even; even------ 4exp(x)
Computes e ** x.
SELECT exp(0.693) AS exp; exp-------------------- 1.9997056605411638factorial(x)
See the ! operator. Computes the product of the current integer and all integers below it.
SELECT factorial(4) AS factorial; factorial----------- 24fdiv(x, y)
Performs integer division (x // y) but returns a DOUBLE value.
SELECT fdiv(5, 2) AS fdiv; fdiv------ 2floor(x)
Rounds the number down.
SELECT floor(17.4) AS floor; floor------- 17fmod(x, y)
Calculates the modulo value. Always returns a DOUBLE value.
SELECT fmod(5, 2) AS fmod; fmod------ 1gamma(x)
Interpolation of the factorial of x - 1. Fractional inputs are allowed.
SELECT gamma(5.5) AS gamma; gamma------------------- 52.34277778455352gcd(x, y)
Computes the greatest common divisor of x and y.
SELECT gcd(42, 57) AS gcd; gcd----- 3greatest_common_divisor(x, y)
Computes the greatest common divisor of x and y.
SELECT greatest_common_divisor(42, 57) AS greatest_common_divisor; greatest_common_divisor------------------------- 3greatest(x1, x2, ...)
Selects the largest value.
SELECT greatest(3, 2, 4, 4) AS greatest; greatest---------- 4isfinite(x)
Returns true if the floating point value is finite, false otherwise.
SELECT isfinite(5.5) AS isfinite; isfinite---------- tisinf(x)
Returns true if the floating point value is infinite, false otherwise.
SELECT isinf('Infinity'::FLOAT) AS isinf; isinf------- tisnan(x)
Returns true if the floating point value is not a number, false otherwise.
SELECT isnan('NaN'::FLOAT) AS isnan; isnan------- tlcm(x, y)
Computes the least common multiple of x and y.
SELECT lcm(42, 57) AS lcm; lcm----- 798least_common_multiple(x, y)
Computes the least common multiple of x and y.
SELECT least_common_multiple(42, 57) AS least_common_multiple; least_common_multiple----------------------- 798least(x1, x2, ...)
Selects the smallest value.
SELECT least(3, 2, 4, 4) AS least; least------- 2lgamma(x)
Computes the log of the gamma function.
SELECT lgamma(2) AS lgamma; lgamma-------- 0ln(x)
Computes the natural logarithm of x.
SELECT ln(2) AS ln; ln-------------------- 0.6931471805599453log(x)
Computes the base-10 logarithm of x.
SELECT log(100) AS log; log----- 2log10(x)
Alias of log. Computes the base-10 logarithm of x.
SELECT log10(1000) AS log10; log10------- 3log2(x)
Computes the base-2 log of x.
SELECT log2(8) AS log2; log2------ 3multiply(x, y)
Alias for x * y.
SELECT multiply(2, 3) AS multiply; multiply---------- 6nextafter(x, y)
Return the next floating point value after x in the direction of y.
SELECT nextafter(1::FLOAT, 2::FLOAT) AS nextafter; nextafter----------- 1.0000001pi()
Returns the value of pi.
SELECT pi() AS pi; pi------------------- 3.141592653589793pow(x, y)
Computes x to the power of y.
SELECT pow(2, 3) AS pow; pow----- 8power(x, y)
Alias of pow. Computes x to the power of y.
SELECT power(2, 3) AS power; power------- 8radians(x)
Converts degrees to radians.
SELECT radians(90) AS radians; radians-------------------- 1.5707963267948966random()
| Description | Returns a random number x in the range 0.0 <= x < 1.0. |
|---|---|
| Example | random() |
| Result | various |
round_even(v NUMERIC, s INTEGER)
Alias of roundbankers(v, s). Round to s decimal places using the rounding half to even rule. Values s < 0 are allowed.
SELECT round_even(24.5, 0) AS round_even; round_even------------ 24roundbankers(v NUMERIC, s INTEGER)
Alias of round_even(v, s). Round to s decimal places using the rounding half to even rule. Values s < 0 are allowed.
SELECT roundbankers(24.5, 0) AS roundbankers; roundbankers-------------- 24round(v NUMERIC, s INTEGER)
Round to s decimal places. Values s < 0 are allowed.
SELECT round(42.4332, 2) AS round; round------- 42.43setseed(x)
| Description | Sets the seed to be used for the random function. |
|---|---|
| Example | setseed(0.42) |
sign(x)
Returns the sign of x as -1, 0 or 1.
SELECT sign(-349) AS sign; sign------ -1signbit(x)
Returns whether the signbit is set or not.
SELECT signbit(-1.0) AS signbit; signbit--------- tsin(x)
Computes the sin of x.
SELECT sin(pi() / 6) AS sin; sin--------------------- 0.49999999999999994sqrt(x)
Returns the square root of the number.
SELECT sqrt(9) AS sqrt; sqrt------ 3subtract(x, y)
Alias for x - y.
SELECT subtract(2, 3) AS subtract; subtract---------- -1tan(x)
Computes the tangent of x.
SELECT tan(pi() / 4) AS tan; tan-------------------- 0.9999999999999999trunc(x)
Truncates the number.
SELECT trunc(17.4) AS trunc; trunc------- 17xor(x, y)
Bitwise XOR.
SELECT xor(17, 5) AS xor; xor----- 20