Exponential, logarithm, and root functions
Use these functions to compute exponentials, square and cube roots, and logarithms to base e, 2, or 10, or to a base that you choose. To raise a number to another power, use the ** (exponent) operator, which also has the function names pow and power.
cbrt function
Returns the number whose third power is x. x can be negative, and the cube root of a negative number is negative. If x is NULL, the result is NULL.
Syntax
cbrt(x)
Arguments
The cbrt function takes the following argument:
x-
The number to take the cube root of, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the cube root of 8.
SELECT cbrt(8) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the cube root of -27, and shows that power with the exponent 1.0 / 3 returns NaN for the same number.
SELECT cbrt(-27) AS cube_root, isnan(power(-27, 1.0 / 3)) AS power_is_nan
The query returns the following result.
| cube_root | power_is_nan |
|---|---|
|
|
Considerations
To get the cube root of a negative number, use cbrt, not power(. x, 1.0 / 3)power returns NaN (not a number) for a negative base with a fractional exponent.
exp function
Returns e, the base of the natural logarithm, raised to the power x. exp(1) returns e itself, and exp(0) returns 1.0. If x is NULL, the result is NULL.
exp is the inverse of ln, so ln(exp( returns x))x, apart from floating-point rounding in the last digits. To raise another base to a power, use power.
Syntax
exp(x)
Arguments
The exp function takes the following argument:
x-
The exponent, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns e raised to the power of 1.
SELECT exp(1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes the result of exp to ln, which returns the original exponent.
SELECT ln(exp(2)) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a very large exponent returns infinity, and a very small one returns 0.
SELECT isinf(exp(1000)) AS overflows, exp(-1000) = 0 AS underflows
The query returns the following result.
| overflows | underflows |
|---|---|
|
|
Considerations
When the result is too large for DOUBLE, as with exp(1000), the function returns infinity, and the query does not fail. When the result is too small, as with exp(-1000), the function returns 0.
Differences from Athena engine version 3
Athena engine version 3 has the function e(), which returns e, the base of the natural logarithm. DuckDB does not have e(), so replace it with exp(1).
ln function
Returns the logarithm of x to base e. ln is the inverse of exp. If x is NULL, the result is NULL.
For the base-10 logarithm, use the log10 function, or log with one argument. For the base-2 logarithm, use the log2 function. For any other base, use the log function with two arguments.
Syntax
ln(x)
Arguments
The ln function takes the following argument:
x-
The number to take the natural logarithm of, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the natural logarithm of 2.
SELECT ln(2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the result is negative infinity when x is 0, and NaN when x is negative.
SELECT ln(0) = CAST('-infinity' AS DOUBLE) AS zero_input, isnan(ln(-1)) AS negative_input
The query returns the following result.
| zero_input | negative_input |
|---|---|
|
|
Considerations
ln(0) returns negative infinity, and a negative x returns NaN (not a number). The query does not fail in either case. To find such results, test them with isinf and isnan.
log function
Returns the logarithm of x to the base that you pass as base, or to base 10 when you omit base. With one argument, log returns the same result as log10. With two arguments, the base comes first, so log(2, 64) returns 6.0. If any argument is NULL, the result is NULL.
For the natural logarithm, use the ln function. For the base-2 logarithm, you can also use the log2 function.
Syntax
log(x)
log(base, x)
Arguments
The log function takes the following arguments:
base-
The base of the logarithm, as a
DOUBLEvalue. The default is10. Values of other numeric types are converted toDOUBLE. This argument is optional. x-
The number to take the logarithm of, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the logarithm of 64 to base 2.
SELECT log(2, 64) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares log with one argument, which uses base 10, with ln, which uses base e. The ln result is rounded to six decimal places.
SELECT log(100) AS base_10, round(ln(100), 6) AS natural
The query returns the following result.
| base_10 | natural |
|---|---|
|
|
The following SQL query shows that the result is negative infinity when x is 0, and NaN when x is negative.
SELECT log(2, 0) = CAST( '-infinity' AS DOUBLE ) AS zero_input, isnan(log(2, -8)) AS negative_input
The query returns the following result.
| zero_input | negative_input |
|---|---|
|
|
Considerations
With one argument, log is the base-10 logarithm, not the natural logarithm. For the natural logarithm, use the ln function.
When x is 0, the result is negative infinity for a base greater than 1, and positive infinity for a base between 0 and 1. When x or base is negative, the result is NaN (not a number). The query does not fail in any of these cases.
log10 function
Returns the logarithm of x to base 10, which is the exponent that you apply to 10 to get x. If x is NULL, the result is NULL.
log with one argument returns the same result. For the natural logarithm, use the ln function.
Syntax
log10(x)
Arguments
The log10 function takes the following argument:
x-
The number to take the base-10 logarithm of, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the base-10 logarithm of 1000.
SELECT log10(1000) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the base-10 logarithm of a number between 0 and 1, which is negative.
SELECT log10(0.001) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the result is negative infinity when x is 0, and NaN when x is negative.
SELECT log10(0) = CAST( '-infinity' AS DOUBLE ) AS zero_input, isnan(log10(-1)) AS negative_input
The query returns the following result.
| zero_input | negative_input |
|---|---|
|
|
Considerations
log10(0) returns negative infinity, and a negative x returns NaN (not a number). The query does not fail in either case. To find such results, test them with isinf and isnan.
log2 function
Returns the logarithm of x to base 2, which is the exponent that you apply to 2 to get x. If x is NULL, the result is NULL.
log(2, returns nearly the same value, but the last digit can differ, so do not compare the two for equality. For the natural logarithm, use the ln function.x)
Syntax
log2(x)
Arguments
The log2 function takes the following argument:
x-
The number to take the base-2 logarithm of, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the base-2 logarithm of 8.
SELECT log2(8) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the base-2 logarithm of 0.5, which is negative.
SELECT log2(0.5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the result is negative infinity when x is 0, and NaN when x is negative.
SELECT log2(0) = CAST('-infinity' AS DOUBLE) AS zero_input, isnan(log2(-1)) AS negative_input
The query returns the following result.
| zero_input | negative_input |
|---|---|
|
|
Considerations
log2(0) returns negative infinity, and a negative x returns NaN (not a number). The query does not fail in either case. To find such results, test them with isinf and isnan.
sqrt function
Returns the square root of x. If x is NULL, the result is NULL.
Syntax
sqrt(x)
Arguments
The sqrt function takes the following argument:
x-
The number to take the square root of, as a
DOUBLEvalue. Integer,DECIMAL, andFLOATvalues are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the square root of 4.
SELECT sqrt(4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the square root of 2.
SELECT sqrt(2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the square root of a negative number is NaN.
SELECT isnan(sqrt(-1)) AS is_nan
The query returns the following result.
| is_nan |
|---|
|
Considerations
For a negative x, sqrt returns NaN (not a number), not an error and not NULL. To check for this, use the isnan function.