Mathematical functions

The following mathematical functions are available for numeric types.

Function Return type Description
abs(numeric_type) numeric_type Returns the absolute value of the argument.
cbrt(double precision) double precision Returns the cube root of the argument.
ceil(numeric) / ceil(double precision) Same as argument type Rounds the value up to the nearest integer.
ceiling(numeric) / ceiling(double precision) Same as argument type Alias for ceil.
degrees(double precision) double precision Converts an angle from radians to degrees.
div(y numeric, x numeric) numeric Returns the integer quotient of y / x truncated toward zero.
erf(double precision) double precision Returns the Gauss error function of the argument.
erfc(double precision) double precision Returns the complementary error function 1 - erf(x).
exp(numeric) / exp(double precision) Same as argument type Returns e raised to the given power.
factorial(bigint) numeric Returns the factorial of the argument.
floor(numeric) / floor(double precision) Same as argument type Rounds the value down to the nearest integer.
gamma(double precision) double precision Returns the gamma function, a generalized factorial.
gcd(numeric_type, numeric_type) numeric_type Returns the greatest common divisor of the arguments.
lcm(numeric_type, numeric_type) numeric_type Returns the least common multiple of the arguments.
lgamma(double precision) double precision Returns the natural logarithm of the absolute value of the gamma function.
ln(numeric) / ln(double precision) Same as argument type Returns the natural logarithm of the argument.
log(double precision) / log(numeric) Same as argument type Returns the base-10 logarithm of the argument.
log(b numeric, x numeric) numeric Returns the logarithm of x to base b.
log10(numeric) / log10(double precision) Same as argument type Alias for the base-10 logarithm.
min_scale(numeric) integer Returns the minimum scale (fractional digits) required to represent the value exactly.
mod(y numeric_type, x numeric_type) numeric_type Returns the remainder of y / x.
pi() double precision Returns the constant π.
power(a, b) numeric / double precision Returns a raised to the power of b.
radians(double precision) double precision Converts an angle from degrees to radians.
round(numeric) / round(double precision) Same as argument type Rounds the value to the nearest integer.
round(v numeric, s integer) numeric Rounds v to s decimal places (negative s rounds to the left of the decimal point).
scale(numeric) integer Returns the number of fractional digits in the value.
sign(numeric) / sign(double precision) Same as argument type Returns -1, 0, or +1 depending on the sign of the value.
sqrt(numeric) / sqrt(double precision) Same as argument type Returns the square root of the argument.
trim_scale(numeric) numeric Removes trailing zeros from the fractional part of the value.
trunc(numeric) / trunc(double precision) Same as argument type Truncates the value toward zero (returns the integer part).
trunc(v numeric, s integer) numeric Truncates v to s decimal places.
width_bucket(operand, low, high, count) integer Returns the bucket number for operand in an equi-width histogram from low to high with count buckets.
width_bucket(operand, thresholds array) integer Returns the bucket number for operand using an array of lower bounds as thresholds.

Random number and seed functions

These functions generate pseudo-random values and control the random sequence.

Function Return type Description
random() double precision Returns a pseudo-random value between 0.0 (inclusive) and 1.0 (exclusive).
random(min integer, max integer) integer Returns a pseudo-random integer between min and max (inclusive).
random(min bigint, max bigint) bigint Returns a pseudo-random bigint between min and max (inclusive).
random(min numeric, max numeric) numeric Returns a pseudo-random numeric value between min and max (inclusive); precision matches the more precise argument.
random_normal([mean double precision, stddev double precision]) double precision Returns a pseudo-random value from a normal (Gaussian) distribution. Defaults: mean = 0, stddev = 1.
setseed(double precision) void Sets the seed for random() and random_normal(). The seed must be between -1.0 and 1.0.

Trigonometric functions

These functions operate on angles and return trigonometric values in radians or degrees.

Function Description
acos(double precision) Returns the inverse cosine of the argument, in radians.
acosd(double precision) Returns the inverse cosine of the argument, in degrees.
asin(double precision) Returns the inverse sine of the argument, in radians.
asind(double precision) Returns the inverse sine of the argument, in degrees.
atan(double precision) Returns the inverse tangent of the argument, in radians.
atand(double precision) Returns the inverse tangent of the argument, in degrees.
atan2(y double precision, x double precision) Returns the inverse tangent of y / x, in radians.
atan2d(y double precision, x double precision) Returns the inverse tangent of y / x, in degrees.
cos(double precision) Returns the cosine of an angle given in radians.
cosd(double precision) Returns the cosine of an angle given in degrees.
cot(double precision) Returns the cotangent of an angle in radians.
cotd(double precision) Returns the cotangent of an angle in degrees.
sin(double precision) Returns the sine of an angle in radians.
sind(double precision) Returns the sine of an angle in degrees.
tan(double precision) Returns the tangent of an angle in radians.
tand(double precision) Returns the tangent of an angle in degrees.

Hyperbolic functions

These functions compute hyperbolic values and their inverses.

Function Description
sinh(double precision) Returns the hyperbolic sine of the argument.
cosh(double precision) Returns the hyperbolic cosine of the argument.
tanh(double precision) Returns the hyperbolic tangent of the argument.
asinh(double precision) Returns the inverse hyperbolic sine of the argument.
acosh(double precision) Returns the inverse hyperbolic cosine of the argument.
atanh(double precision) Returns the inverse hyperbolic tangent of the argument.