9.3. Mathematical Functions and Operators
Mathematical Functions and Operators
Mathematical operators are provided for many PostgreSQL types. For types without standard mathematical conventions (e.g., date/time types) we describe the actual behavior in subsequent sections.
Mathematical Operators shows the mathematical operators that are available for the standard numeric types.
Unless otherwise noted, operators shown as accepting numeric_type are available for all the types smallint, integer, bigint, numeric, real, and double precision.
Operators shown as accepting integral_type are available for the types smallint, integer, and bigint.
Except where noted, each form of an operator returns the same data type as its argument(s).
Calls involving multiple argument data types, such as integer + numeric, are resolved by using the type appearing later in these lists.
Operator Description Example(s) |
|---|
numeric_type Addition
|
Unary plus (no operation)
|
numeric_type Subtraction
|
Negation
|
numeric_type Multiplication
|
numeric_type Division (for integral types, division truncates the result towards zero)
|
numeric_type Modulo (remainder); available for
|
Exponentiation
Unlike typical mathematical practice, multiple uses of
|
Square root
|
Cube root
|
Absolute value
|
integral_type Bitwise AND
|
integral_type Bitwise OR
|
integral_type Bitwise exclusive OR
|
Bitwise NOT
|
integral_type Bitwise shift left
|
integral_type Bitwise shift right
|
: Mathematical Operators
Mathematical Functions shows the available mathematical functions. Many of these functions are provided in multiple forms with different argument types. Except where noted, any given form of a function returns the same data type as its argument(s); cross-type cases are resolved in the same way as explained above for operators. The functions working with double precision data are mostly implemented on top of the host system's C library; accuracy and behavior in boundary cases can therefore vary depending on the host system.
Function Description Example(s) |
|---|
Absolute value
|
Cube root
|
Nearest integer greater than or equal to argument
|
Nearest integer greater than or equal to argument (same as
|
Converts radians to degrees
|
Integer quotient of
|
Error function
|
Complementary error function (
|
Exponential (
|
Factorial
|
Nearest integer less than or equal to argument
|
Gamma function
|
Greatest common divisor (the largest positive number that divides both inputs with no remainder); returns
|
Least common multiple (the smallest strictly positive number that is an integral multiple of both inputs); returns
|
Natural logarithm of the absolute value of the gamma function
|
Natural logarithm
|
Base 10 logarithm
|
Base 10 logarithm (same as
|
Logarithm of
|
Minimum scale (number of fractional decimal digits) needed to represent the supplied value precisely
|
Remainder of
|
Approximate value of [] |
|
|
Converts degrees to radians
|
Rounds to nearest integer. For
|
Rounds
|
Scale of the argument (the number of decimal digits in the fractional part)
|
Sign of the argument (-1, 0, or +1)
|
Square root
|
Reduces the value's scale (number of fractional decimal digits) by removing trailing zeroes
|
Truncates to integer (towards zero)
|
Truncates
|
Returns the number of the bucket in which
|
Returns the number of the bucket in which
|
: Mathematical Functions
Random Functions shows functions for generating random numbers.
Function Description Example(s) |
|---|
Returns a random value in the range 0.0 <= x < 1.0
|
Returns a random value in the range
|
Returns a random value from the normal distribution with the given parameters;
|
Sets the seed for subsequent
|
: Random Functions
The random() and random_normal() functions listed in Random Functions use a deterministic pseudo-random number generator. It is fast but not suitable for cryptographic applications; see the F.26. pgcrypto — cryptographic functions module for a more secure alternative. If setseed() is called, the series of results of subsequent calls to these functions in the current session can be repeated by re-issuing setseed() with the same argument. Without any prior setseed() call in the same session, the first call to any of these functions obtains a seed from a platform-dependent source of random bits.
Trigonometric Functions shows the available trigonometric functions. Each of these functions comes in two variants, one that measures angles in radians and one that measures angles in degrees.
Function Description Example(s) |
|---|
Inverse cosine, result in radians
|
Inverse cosine, result in degrees
|
Inverse sine, result in radians
|
Inverse sine, result in degrees
|
Inverse tangent, result in radians
|
Inverse tangent, result in degrees
|
Inverse tangent of
|
Inverse tangent of
|
Cosine, argument in radians
|
Cosine, argument in degrees
|
Cotangent, argument in radians
|
Cotangent, argument in degrees
|
Sine, argument in radians
|
Sine, argument in degrees
|
Tangent, argument in radians
|
Tangent, argument in degrees
|
: Trigonometric Functions
Another way to work with angles measured in degrees is to use the unit transformation functions radians() and degrees() shown earlier. However, using the degree-based trigonometric functions is preferred, as that way avoids round-off error for special cases such as sind(30).
Hyperbolic Functions shows the available hyperbolic functions.
Function Description Example(s) |
|---|
Hyperbolic sine
|
Hyperbolic cosine
|
Hyperbolic tangent
|
Inverse hyperbolic sine
|
Inverse hyperbolic cosine
|
Inverse hyperbolic tangent
|
: Hyperbolic Functions