# 9.3. 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.
Table 9.4 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.
Table 9.4. Mathematical Operators
Operator Description Example(s) |
---|
numeric_type + numeric_type → * numeric_type* Addition 2 + 3 → 5 |
+ numeric_type → * numeric_type* Unary plus (no operation) + 3.5 → 3.5 |
numeric_type - numeric_type → * numeric_type* Subtraction 2 - 3 → -1 |
- numeric_type → * numeric_type* Negation - (-4) → 4 |
numeric_type * numeric_type → * numeric_type* Multiplication 2 * 3 → 6 |
numeric_type / numeric_type → * numeric_type* Division (for integral types, division truncates the result towards zero) 5.0 / 2 → 2.5000000000000000 5 / 2 → 2 (-5) / 2 → -2 |
numeric_type % numeric_type → * numeric_type* Modulo (remainder); available for smallint , integer , bigint , and numeric 5 % 4 → 1 |
numeric ^ numeric → numeric double precision ^ double precision → double precision Exponentiation 2 ^ 3 → 8 Unlike typical mathematical practice, multiple uses of ^ will associate left to right by default:2 ^ 3 ^ 3 → 512 2 ^ (3 ^ 3) → 134217728 |
|/ double precision → double precision Square root |/ 25.0 → 5 |
||/ double precision → double precision Cube root ||/ 64.0 → 4 |
@ numeric_type → * numeric_type* Absolute value @ -5.0 → 5 |
integral_type & integral_type → * integral_type* Bitwise AND 91 & 15 → 11 |
integral_type | integral_type → * integral_type* Bitwise OR 32 | 3 → 35 |
integral_type # integral_type → * integral_type* Bitwise exclusive OR 17 # 5 → 20 |
~ integral_type → * integral_type* Bitwise NOT ~1 → -2 |
integral_type << integer → * integral_type* Bitwise shift left 1 << 4 → 16 |
integral_type >> integer → * integral_type* Bitwise shift right 8 >> 2 → 2 |
Table 9.5 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.
Table 9.5. Mathematical Functions
Table 9.6 shows functions for generating random numbers.
Table 9.6. Random Functions
The random()
function uses a simple linear congruential algorithm. It is fast but not suitable for cryptographic applications; see the pgcrypto module for a more secure alternative. If setseed()
is called, the series of results of subsequent random()
calls in the current session can be repeated by re-issuing setseed()
with the same argument.
Table 9.7 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.
Table 9.7. Trigonometric Functions
# Note
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)
.
Table 9.8 shows the available hyperbolic functions.
Table 9.8. Hyperbolic Functions