9.3. Mathematical Functions and Operators (original) (raw)

Function Description Example(s)
abs ( numeric_type ) → numeric_type Absolute value abs(-17.4) → 17.4
cbrt ( double precision ) → double precision Cube root cbrt(64.0) → 4
ceil ( numeric ) → numeric ceil ( double precision ) → double precision Nearest integer greater than or equal to argument ceil(42.2) → 43 ceil(-42.8) → -42
ceiling ( numeric ) → numeric ceiling ( double precision ) → double precision Nearest integer greater than or equal to argument (same as ceil) ceiling(95.3) → 96
degrees ( double precision ) → double precision Converts radians to degrees degrees(0.5) → 28.64788975654116
div ( y numeric, x numeric ) → numeric Integer quotient of y/x (truncates towards zero) div(9, 4) → 2
erf ( double precision ) → double precision Error function erf(1.0) → 0.8427007929497149
erfc ( double precision ) → double precision Complementary error function (1 - erf(x), without loss of precision for large inputs) erfc(1.0) → 0.15729920705028513
exp ( numeric ) → numeric exp ( double precision ) → double precision Exponential (e raised to the given power) exp(1.0) → 2.7182818284590452
factorial ( bigint ) → numeric Factorial factorial(5) → 120
floor ( numeric ) → numeric floor ( double precision ) → double precision Nearest integer less than or equal to argument floor(42.8) → 42 floor(-42.8) → -43
gcd ( numeric_type, numeric_type ) → numeric_type Greatest common divisor (the largest positive number that divides both inputs with no remainder); returns 0 if both inputs are zero; available for integer, bigint, and numeric gcd(1071, 462) → 21
lcm ( numeric_type, numeric_type ) → numeric_type Least common multiple (the smallest strictly positive number that is an integral multiple of both inputs); returns 0 if either input is zero; available for integer, bigint, and numeric lcm(1071, 462) → 23562
ln ( numeric ) → numeric ln ( double precision ) → double precision Natural logarithm ln(2.0) → 0.6931471805599453
log ( numeric ) → numeric log ( double precision ) → double precision Base 10 logarithm log(100) → 2
log10 ( numeric ) → numeric log10 ( double precision ) → double precision Base 10 logarithm (same as log) log10(1000) → 3
log ( b numeric, x numeric ) → numeric Logarithm of x to base b log(2.0, 64.0) → 6.0000000000000000
min_scale ( numeric ) → integer Minimum scale (number of fractional decimal digits) needed to represent the supplied value precisely min_scale(8.4100) → 2
mod ( y numeric_type, x numeric_type ) → numeric_type Remainder of y/x; available for smallint, integer, bigint, and numeric mod(9, 4) → 1
pi ( ) → double precision Approximate value of π pi() → 3.141592653589793
power ( a numeric, b numeric ) → numeric power ( a double precision, b double precision ) → double precision a raised to the power of b power(9, 3) → 729
radians ( double precision ) → double precision Converts degrees to radians radians(45.0) → 0.7853981633974483
round ( numeric ) → numeric round ( double precision ) → double precision Rounds to nearest integer. For numeric, ties are broken by rounding away from zero. For double precision, the tie-breaking behavior is platform dependent, but “round to nearest even” is the most common rule. round(42.4) → 42
round ( v numeric, s integer ) → numeric Rounds v to s decimal places. Ties are broken by rounding away from zero. round(42.4382, 2) → 42.44 round(1234.56, -1) → 1230
scale ( numeric ) → integer Scale of the argument (the number of decimal digits in the fractional part) scale(8.4100) → 4
sign ( numeric ) → numeric sign ( double precision ) → double precision Sign of the argument (-1, 0, or +1) sign(-8.4) → -1
sqrt ( numeric ) → numeric sqrt ( double precision ) → double precision Square root sqrt(2) → 1.4142135623730951
trim_scale ( numeric ) → numeric Reduces the value's scale (number of fractional decimal digits) by removing trailing zeroes trim_scale(8.4100) → 8.41
trunc ( numeric ) → numeric trunc ( double precision ) → double precision Truncates to integer (towards zero) trunc(42.8) → 42 trunc(-42.8) → -42
trunc ( v numeric, s integer ) → numeric Truncates v to s decimal places trunc(42.4382, 2) → 42.43
width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer Returns the number of the bucket in which operand falls in a histogram having count equal-width buckets spanning the range low to high. Returns 0 or count+1 for an input outside that range. width_bucket(5.35, 0.024, 10.06, 5) → 3
width_bucket ( operand anycompatible, thresholds anycompatiblearray ) → integer Returns the number of the bucket in which operand falls given an array listing the lower bounds of the buckets. Returns 0 for an input less than the first lower bound. operand and the array elements can be of any type having standard comparison operators. The thresholds array must be sorted, smallest first, or unexpected results will be obtained. width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) → 2