

# Numeric functions and operators in DuckDB

Use numeric functions and operators to do arithmetic, raise numbers to powers, round and truncate numbers, compute logarithms and roots, compute trigonometric values, and work with the bits of integers. Several operators also have a function name: `add` for `+`, `subtract` for `-`, `multiply` for `*`, `divide` for `//`, and `mod` for `%`. In the same way, `pow` and `power` are function names for `**`, `abs` for `@`, and `factorial` for `!`.

DuckDB has the signed integer types `TINYINT`, `SMALLINT`, `INTEGER`, `BIGINT`, and `HUGEINT` (128 bits). It also has the unsigned integer types `UTINYINT`, `USMALLINT`, `UINTEGER`, `UBIGINT`, and `UHUGEINT`. The exact type `DECIMAL({{precision}}, {{scale}})` holds up to 38 digits, and the approximate types are `FLOAT` and `DOUBLE`. A whole-number literal, such as `42`, is an `INTEGER`, or a `BIGINT` or `HUGEINT` when it is too large for `INTEGER`. A literal with a decimal point, such as `1.5`, is a `DECIMAL`, and a literal with an exponent, such as `1.5e0`, is a `DOUBLE`.

When the operands of an arithmetic operator have different numeric types, DuckDB converts them to a common type, and the result has that type. For example, an `INTEGER` plus a `BIGINT` is a `BIGINT`, an `INTEGER` times a `DECIMAL` is a `DECIMAL`, and arithmetic with a `DOUBLE` operand returns a `DOUBLE`. An integer result is not widened when it is too large for its type: the query fails instead. Because `2147483647` and `1` are both `INTEGER` literals, `2147483647 + 1` fails. To avoid the error, cast an operand to a wider type, as in `CAST(2147483647 AS BIGINT) + 1`.

The `/` operator always returns a fractional result, so `7 / 2` returns `3.5`. To divide integers and drop the fractional part, use `//`, and to get the remainder, use `%`. If an operand of an arithmetic or bitwise operator on numbers is `NULL`, the result is `NULL`.

When an expression has more than one operator, DuckDB applies them in the following order. The factorial operator `!` comes first. Next comes a sign, `-` or `+`, in front of a value, and then `**` and `^`. After those come `*`, `/`, `//`, and `%`, and then `+` and `-` between two values. Then come `&`, `|`, `<<`, `>>`, `~`, and `@`, which all have the same precedence. Comparison operators such as `=` come after all of these. DuckDB applies operators at the same level from left to right. This order differs from many programming languages. For example, `-2 ** 2` returns `4.0`, and `2 ** 3 ** 2` returns `64.0`. Also, `4 | 1 & 2` returns `0`, and `~12 + 1` returns `-14`. Use parentheses to make the order explicit.

**Topics**
+ [Arithmetic](duckdb-functions-numeric-arithmetic-operators.md)
+ [Division](duckdb-functions-numeric-division-and-divisors.md)
+ [Rounding](duckdb-functions-numeric-rounding.md)
+ [Sign and NaN](duckdb-functions-numeric-sign-and-floating-point.md)
+ [Logs and roots](duckdb-functions-numeric-logarithms-and-roots.md)
+ [Trigonometry](duckdb-functions-numeric-trigonometry.md)
+ [Factorials](duckdb-functions-numeric-factorial-and-gamma.md)
+ [Random numbers](duckdb-functions-numeric-random-numbers.md)
+ [Bitwise](duckdb-functions-numeric-bitwise.md)

The following table lists the numeric functions and operators, with a link to the description of each one.


| Function or operator | Description | 
| --- | --- | 
| `!` ([factorial](duckdb-functions-numeric-factorial-and-gamma.md#athena-duckdb-functions-numeric-factorial-operator)) | Returns the factorial of an integer. | 
| [%](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-modulo-operator) | Returns the remainder of dividing one number by another. | 
| [&](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-bitwise-and-operator) | Returns the bitwise AND of two integers or two bitstrings. | 
| [\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-multiplication-operator) | Multiplies two numbers, or multiplies an interval by a number. | 
| [\*\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-exponent-operator) | Raises a number to a power. | 
| [\+](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-addition-operator) | Adds two numbers, or adds an interval or a number of days to a date or time value. | 
| [-](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-subtraction-operator) | Subtracts one value from another, or negates a number. | 
| [/](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-division-operator) | Divides one number by another and returns a fractional result. | 
| [//](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-integer-division-operator) | Divides one integer by another and drops the fractional part. | 
| [<<](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-left-shift-operator) | Shifts the bits of an integer or a bitstring to the left. | 
| [>>](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-right-shift-operator) | Shifts the bits of an integer or a bitstring to the right. | 
| [@](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-absolute-value-operator) | Returns the absolute value of a number. | 
| ^ | Alias of [\*\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-exponent-operator). | 
| [\|](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-bitwise-or-operator) | Returns the bitwise OR of two integers or two bitstrings. | 
| [\~](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-bitwise-not-operator) | Inverts every bit of an integer or a bitstring, or tests whether a string matches a regular expression. | 
| abs | Alias of [@](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-absolute-value-operator). | 
| [acos](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-acos) | Returns the arccosine of a number, in radians. | 
| [acosh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-acosh) | Returns the inverse hyperbolic cosine of a number. | 
| add | Alias of [\+](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-addition-operator). | 
| [asin](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-asin) | Returns the arcsine of a number, in radians. | 
| [asinh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-asinh) | Returns the inverse hyperbolic sine of a number. | 
| [atan](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-atan) | Returns the arctangent of a number, in radians. | 
| [atan2](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-atan2) | Returns the angle, in radians, of the point with a given y-coordinate and x-coordinate. | 
| [atanh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-atanh) | Returns the inverse hyperbolic tangent of a number. | 
| [binom](duckdb-functions-numeric-factorial-and-gamma.md#athena-duckdb-functions-numeric-binom) | Returns the binomial coefficient, the number of ways to choose a number of items from a set. | 
| [cbrt](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-cbrt) | Returns the cube root of a number. | 
| [ceil](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-ceil) | Rounds a number up to the nearest integer. | 
| ceiling | Alias of [ceil](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-ceil). | 
| [cos](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-cos) | Returns the cosine of an angle in radians. | 
| [cosh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-cosh) | Returns the hyperbolic cosine of a number. | 
| [cot](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-cot) | Returns the cotangent of an angle in radians. | 
| [decimal\_division](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-decimal-division) | Returns the quotient of two `DECIMAL` values as a `DECIMAL`, without converting them to `DOUBLE`. | 
| [degrees](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-degrees) | Converts an angle from radians to degrees. | 
| divide | Alias of [//](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-integer-division-operator). | 
| [even](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-even) | Rounds the absolute value of a number up to an even integer and keeps the number's sign. | 
| [exp](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-exp) | Returns *e* raised to the power of a number. | 
| factorial | Alias of the [`!` (factorial) operator](duckdb-functions-numeric-factorial-and-gamma.md#athena-duckdb-functions-numeric-factorial-operator). | 
| [fdiv](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-fdiv) | Divides two numbers and rounds the quotient down to the nearest integer. | 
| [floor](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-floor) | Rounds a number down to the nearest integer. | 
| [fmod](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-fmod) | Returns the floating-point remainder of a division, with the sign of the divisor. | 
| [gamma](duckdb-functions-numeric-factorial-and-gamma.md#athena-duckdb-functions-numeric-gamma) | Returns the gamma function of a number, which extends the factorial to real numbers. | 
| gcd | Alias of [greatest\_common\_divisor](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-greatest-common-divisor). | 
| [greatest\_common\_divisor](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-greatest-common-divisor) | Returns the greatest common divisor of two integers. | 
| [isfinite](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-isfinite) | Returns whether a number, date, or timestamp is finite. | 
| [isinf](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-isinf) | Returns whether a number, date, or timestamp is positive or negative infinity. | 
| [isnan](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-isnan) | Returns whether a floating-point value is NaN (not a number). | 
| lcm | Alias of [least\_common\_multiple](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-least-common-multiple). | 
| [least\_common\_multiple](duckdb-functions-numeric-division-and-divisors.md#athena-duckdb-functions-numeric-least-common-multiple) | Returns the least common multiple of two integers. | 
| [lgamma](duckdb-functions-numeric-factorial-and-gamma.md#athena-duckdb-functions-numeric-lgamma) | Returns the natural logarithm of the absolute value of the gamma function of a number. | 
| [ln](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-ln) | Returns the natural logarithm of a number. | 
| [log](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-log) | Returns the logarithm of a number to base 10, or to a base that you choose. | 
| [log10](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-log10) | Returns the base-10 logarithm of a number. | 
| [log2](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-log2) | Returns the base-2 logarithm of a number. | 
| mod | Alias of [%](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-modulo-operator). | 
| multiply | Alias of [\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-multiplication-operator). | 
| [nextafter](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-nextafter) | Moves a floating-point number by the smallest possible step toward a second number. | 
| [pi](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-pi) | Returns the constant pi. | 
| pow | Alias of [\*\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-exponent-operator). | 
| power | Alias of [\*\*](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-exponent-operator). | 
| [radians](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-radians) | Converts an angle from degrees to radians. | 
| [random](duckdb-functions-numeric-random-numbers.md#athena-duckdb-functions-numeric-random) | Returns a random number that is at least 0 and less than 1. | 
| [round](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-round) | Rounds a number to a given number of decimal places. | 
| [round\_even](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-round-even) | Rounds a number in the same way as `round`, except that it rounds halfway values so that the last digit it keeps is even. | 
| roundbankers | Alias of [round\_even](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-round-even). | 
| [sign](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-sign) | Returns -1, 0, or 1 to show whether a number is negative, zero, or positive. | 
| [signbit](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-signbit) | Returns whether the sign bit of a floating-point number is set. | 
| [sin](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-sin) | Returns the sine of an angle given in radians. | 
| [sinh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-sinh) | Returns the hyperbolic sine of a number. | 
| [sqrt](duckdb-functions-numeric-logarithms-and-roots.md#athena-duckdb-functions-numeric-sqrt) | Returns the square root of a number. | 
| subtract | Alias of [-](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-subtraction-operator). | 
| [tan](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-tan) | Returns the tangent of an angle given in radians. | 
| [tanh](duckdb-functions-numeric-trigonometry.md#athena-duckdb-functions-numeric-tanh) | Returns the hyperbolic tangent of a number. | 
| [trunc](duckdb-functions-numeric-rounding.md#athena-duckdb-functions-numeric-trunc) | Removes the digits of a number after a given decimal place, without rounding. | 
| [xor](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-xor) | Returns the bitwise exclusive OR (XOR) of two integers or two bitstrings. | 