> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/the-sql-language/functions/functions-math.md).

# 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](#FUNCTIONS-MATH-OP-TABLE) 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**

| <p>Operator</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                  |                                                                                                                     |                                                                                                     |   |                                    |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | - | ---------------------------------- |
| <p><em><code>numeric\_type</code></em> <code>+</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Addition</p><p><code>2 + 3</code> → <code>5</code></p>                                                                                                                                                                                                                                                                                                        |                                                                                                                     |                                                                                                     |   |                                    |
| <p><code>+</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Unary plus (no operation)</p><p><code>+ 3.5</code> → <code>3.5</code></p>                                                                                                                                                                                                                                                                                                                         |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>numeric\_type</code></em> <code>-</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Subtraction</p><p><code>2 - 3</code> → <code>-1</code></p>                                                                                                                                                                                                                                                                                                    |                                                                                                                     |                                                                                                     |   |                                    |
| <p><code>-</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Negation</p><p><code>- (-4)</code> → <code>4</code></p>                                                                                                                                                                                                                                                                                                                                           |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>numeric\_type</code></em> <code>\*</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Multiplication</p><p><code>2 \* 3</code> → <code>6</code></p>                                                                                                                                                                                                                                                                                                |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>numeric\_type</code></em> <code>/</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Division (for integral types, division truncates the result towards zero)</p><p><code>5.0 / 2</code> → <code>2.5000000000000000</code></p><p><code>5 / 2</code> → <code>2</code></p><p><code>(-5) / 2</code> → <code>-2</code></p>                                                                                                                            |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>numeric\_type</code></em> <code>%</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Modulo (remainder); available for <code>smallint</code>, <code>integer</code>, <code>bigint</code>, and <code>numeric</code></p><p><code>5 % 4</code> → <code>1</code></p>                                                                                                                                                                                    |                                                                                                                     |                                                                                                     |   |                                    |
| <p><code>numeric</code> <code>^</code> <code>numeric</code> → <code>numeric</code></p><p><code>double precision</code> <code>^</code> <code>double precision</code> → <code>double precision</code></p><p>Exponentiation</p><p><code>2 ^ 3</code> → <code>8</code></p><p>Unlike typical mathematical practice, multiple uses of <code>^</code> will associate left to right by default:</p><p><code>2 ^ 3 ^ 3</code> → <code>512</code></p><p><code>2 ^ (3 ^ 3)</code> → <code>134217728</code></p> |                                                                                                                     |                                                                                                     |   |                                    |
| <p><code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | /</code> <code>double precision</code> → <code>double precision</code></p><p>Square root</p><p><code>               | / 25.0</code> → <code>5</code></p>                                                                  |   |                                    |
| <p><code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |                                                                                                                     | /</code> <code>double precision</code> → <code>double precision</code></p><p>Cube root</p><p><code> |   | / 64.0</code> → <code>4</code></p> |
| <p><code>@</code> <em><code>numeric\_type</code></em> → <em><code>numeric\_type</code></em></p><p>Absolute value</p><p><code>@ -5.0</code> → <code>5.0</code></p>                                                                                                                                                                                                                                                                                                                                   |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>integral\_type</code></em> <code>&</code> <em><code>integral\_type</code></em> → <em><code>integral\_type</code></em></p><p>Bitwise AND</p><p><code>91 & 15</code> → <code>11</code></p>                                                                                                                                                                                                                                                                                               |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>integral\_type</code></em> <code>                                                                                                                                                                                                                                                                                                                                                                                                                                                      | </code> <em><code>integral\_type</code></em> → <em><code>integral\_type</code></em></p><p>Bitwise OR</p><p><code>32 | 3</code> → <code>35</code></p>                                                                      |   |                                    |
| <p><em><code>integral\_type</code></em> <code>#</code> <em><code>integral\_type</code></em> → <em><code>integral\_type</code></em></p><p>Bitwise exclusive OR</p><p><code>17 # 5</code> → <code>20</code></p>                                                                                                                                                                                                                                                                                       |                                                                                                                     |                                                                                                     |   |                                    |
| <p><code>\~</code> <em><code>integral\_type</code></em> → <em><code>integral\_type</code></em></p><p>Bitwise NOT</p><p><code>\~1</code> → <code>-2</code></p>                                                                                                                                                                                                                                                                                                                                       |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>integral\_type</code></em> <code><<</code> <code>integer</code> → <em><code>integral\_type</code></em></p><p>Bitwise shift left</p><p><code>1 << 4</code> → <code>16</code></p>                                                                                                                                                                                                                                                                                                        |                                                                                                                     |                                                                                                     |   |                                    |
| <p><em><code>integral\_type</code></em> <code>>></code> <code>integer</code> → <em><code>integral\_type</code></em></p><p>Bitwise shift right</p><p><code>8 >> 2</code> → <code>2</code></p>                                                                                                                                                                                                                                                                                                        |                                                                                                                     |                                                                                                     |   |                                    |

<br>

[Table 9.5](#FUNCTIONS-MATH-FUNC-TABLE) 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**

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

<br>

[Table 9.6](#FUNCTIONS-MATH-RANDOM-TABLE) shows functions for generating random numbers.

**Table 9.6. Random Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>random</code> ( ) → <code>double precision</code></p><p>Returns a random value in the range 0.0 <= x < 1.0</p><p><code>random()</code> → <code>0.897124072839091</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| <p><code>random</code> ( <em><code>min</code></em> <code>integer</code>, <em><code>max</code></em> <code>integer</code> ) → <code>integer</code></p><p><code>random</code> ( <em><code>min</code></em> <code>bigint</code>, <em><code>max</code></em> <code>bigint</code> ) → <code>bigint</code></p><p><code>random</code> ( <em><code>min</code></em> <code>numeric</code>, <em><code>max</code></em> <code>numeric</code> ) → <code>numeric</code></p><p>Returns a random value in the range <em><code>min</code></em> <= x <= <em><code>max</code></em>. For type <code>numeric</code>, the result will have the same number of fractional decimal digits as <em><code>min</code></em> or <em><code>max</code></em>, whichever has more.</p><p><code>random(1, 10)</code> → <code>7</code></p><p><code>random(-0.499, 0.499)</code> → <code>0.347</code></p> |
| <p><code>random\_normal</code> ( \[ <em><code>mean</code></em> <code>double precision</code> \[, <em><code>stddev</code></em> <code>double precision</code> ]] ) → <code>double precision</code></p><p>Returns a random value from the normal distribution with the given parameters; <em><code>mean</code></em> defaults to 0.0 and <em><code>stddev</code></em> defaults to 1.0</p><p><code>random\_normal(0.0, 1.0)</code> → <code>0.051285419</code></p>                                                                                                                                                                                                                                                                                                                                                                                                     |
| <p><code>setseed</code> ( <code>double precision</code> ) → <code>void</code></p><p>Sets the seed for subsequent <code>random()</code> and <code>random\_normal()</code> calls; argument must be between -1.0 and 1.0, inclusive</p><p><code>setseed(0.12345)</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |

<br>

The `random()` and `random_normal()` functions listed in [Table 9.6](#FUNCTIONS-MATH-RANDOM-TABLE) use a deterministic pseudo-random number generator. It is fast but not suitable for cryptographic applications; see the [pgcrypto](/appendixes/contrib/pgcrypto.md) 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.

[Table 9.7](#FUNCTIONS-MATH-TRIG-TABLE) 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**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                      |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>acos</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse cosine, result in radians</p><p><code>acos(1)</code> → <code>0</code></p>                                                                                                                                                          |
| <p><code>acosd</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse cosine, result in degrees</p><p><code>acosd(0.5)</code> → <code>60</code></p>                                                                                                                                                     |
| <p><code>asin</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse sine, result in radians</p><p><code>asin(1)</code> → <code>1.5707963267948966</code></p>                                                                                                                                           |
| <p><code>asind</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse sine, result in degrees</p><p><code>asind(0.5)</code> → <code>30</code></p>                                                                                                                                                       |
| <p><code>atan</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse tangent, result in radians</p><p><code>atan(1)</code> → <code>0.7853981633974483</code></p>                                                                                                                                        |
| <p><code>atand</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse tangent, result in degrees</p><p><code>atand(1)</code> → <code>45</code></p>                                                                                                                                                      |
| <p><code>atan2</code> ( <em><code>y</code></em> <code>double precision</code>, <em><code>x</code></em> <code>double precision</code> ) → <code>double precision</code></p><p>Inverse tangent of <em><code>y</code></em>/<em><code>x</code></em>, result in radians</p><p><code>atan2(1, 0)</code> → <code>1.5707963267948966</code></p> |
| <p><code>atan2d</code> ( <em><code>y</code></em> <code>double precision</code>, <em><code>x</code></em> <code>double precision</code> ) → <code>double precision</code></p><p>Inverse tangent of <em><code>y</code></em>/<em><code>x</code></em>, result in degrees</p><p><code>atan2d(1, 0)</code> → <code>90</code></p>               |
| <p><code>cos</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Cosine, argument in radians</p><p><code>cos(0)</code> → <code>1</code></p>                                                                                                                                                                  |
| <p><code>cosd</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Cosine, argument in degrees</p><p><code>cosd(60)</code> → <code>0.5</code></p>                                                                                                                                                             |
| <p><code>cot</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Cotangent, argument in radians</p><p><code>cot(0.5)</code> → <code>1.830487721712452</code></p>                                                                                                                                             |
| <p><code>cotd</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Cotangent, argument in degrees</p><p><code>cotd(45)</code> → <code>1</code></p>                                                                                                                                                            |
| <p><code>sin</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Sine, argument in radians</p><p><code>sin(1)</code> → <code>0.8414709848078965</code></p>                                                                                                                                                   |
| <p><code>sind</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Sine, argument in degrees</p><p><code>sind(30)</code> → <code>0.5</code></p>                                                                                                                                                               |
| <p><code>tan</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Tangent, argument in radians</p><p><code>tan(1)</code> → <code>1.5574077246549023</code></p>                                                                                                                                                |
| <p><code>tand</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Tangent, argument in degrees</p><p><code>tand(45)</code> → <code>1</code></p>                                                                                                                                                              |

<br>

## 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](#FUNCTIONS-MATH-HYP-TABLE) shows the available hyperbolic functions.

**Table 9.8. Hyperbolic Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                           |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>sinh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Hyperbolic sine</p><p><code>sinh(1)</code> → <code>1.1752011936438014</code></p>                |
| <p><code>cosh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Hyperbolic cosine</p><p><code>cosh(0)</code> → <code>1</code></p>                               |
| <p><code>tanh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Hyperbolic tangent</p><p><code>tanh(1)</code> → <code>0.7615941559557649</code></p>             |
| <p><code>asinh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse hyperbolic sine</p><p><code>asinh(1)</code> → <code>0.881373587019543</code></p>       |
| <p><code>acosh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse hyperbolic cosine</p><p><code>acosh(1)</code> → <code>0</code></p>                     |
| <p><code>atanh</code> ( <code>double precision</code> ) → <code>double precision</code></p><p>Inverse hyperbolic tangent</p><p><code>atanh(0.5)</code> → <code>0.5493061443340548</code></p> |

<br>

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-math.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/the-sql-language/functions/functions-math.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
