

# 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](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-exponent-operator), which also has the function names `pow` and `power`.

## `cbrt` function
<a name="athena-duckdb-functions-numeric-cbrt"></a>

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
<a name="athena-duckdb-functions-numeric-cbrt-syntax"></a>

```sql
cbrt({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-cbrt-arguments"></a>

The `cbrt` function takes the following argument:

**{{x}}**  
The number to take the cube root of, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-cbrt-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-cbrt-examples"></a>

The following SQL query returns the cube root of `8`.

```sql
SELECT cbrt(8) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2.0` | 

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.

```sql
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 | 
| --- | --- | 
| `-3.0` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-cbrt-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-exp"></a>

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({{x}}))` returns `x`, apart from floating-point rounding in the last digits. To raise another base to a power, use `power`.

### Syntax
<a name="athena-duckdb-functions-numeric-exp-syntax"></a>

```sql
exp({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-exp-arguments"></a>

The `exp` function takes the following argument:

**{{x}}**  
The exponent, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-exp-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-exp-examples"></a>

The following SQL query returns *e* raised to the power of `1`.

```sql
SELECT exp(1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2.718281828459045` | 

The following SQL query passes the result of `exp` to `ln`, which returns the original exponent.

```sql
SELECT ln(exp(2)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2.0` | 

The following SQL query shows that a very large exponent returns infinity, and a very small one returns `0`.

```sql
SELECT
    isinf(exp(1000)) AS overflows,
    exp(-1000) = 0 AS underflows
```

The query returns the following result.


| overflows | underflows | 
| --- | --- | 
| `true` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-exp-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-exp-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-numeric-ln"></a>

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](#athena-duckdb-functions-numeric-log10), or `log` with one argument. For the base-2 logarithm, use the [`log2` function](#athena-duckdb-functions-numeric-log2). For any other base, use the [`log` function](#athena-duckdb-functions-numeric-log) with two arguments.

### Syntax
<a name="athena-duckdb-functions-numeric-ln-syntax"></a>

```sql
ln({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-ln-arguments"></a>

The `ln` function takes the following argument:

**{{x}}**  
The number to take the natural logarithm of, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-ln-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-ln-examples"></a>

The following SQL query returns the natural logarithm of `2`.

```sql
SELECT ln(2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.6931471805599453` | 

The following SQL query shows that the result is negative infinity when `x` is `0`, and NaN when `x` is negative.

```sql
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 | 
| --- | --- | 
| `true` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-ln-considerations"></a>

`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
<a name="athena-duckdb-functions-numeric-log"></a>

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](#athena-duckdb-functions-numeric-ln). For the base-2 logarithm, you can also use the [`log2` function](#athena-duckdb-functions-numeric-log2).

### Syntax
<a name="athena-duckdb-functions-numeric-log-syntax"></a>

```sql
log({{x}})
log({{base}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-log-arguments"></a>

The `log` function takes the following arguments:

**{{base}}**  
The base of the logarithm, as a `DOUBLE` value. The default is `10`. Values of other numeric types are converted to `DOUBLE`. This argument is optional.

**{{x}}**  
The number to take the logarithm of, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-log-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-log-examples"></a>

The following SQL query returns the logarithm of `64` to base `2`.

```sql
SELECT log(2, 64) AS result
```

The query returns the following result.


| result | 
| --- | 
| `6.0` | 

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.

```sql
SELECT log(100) AS base_10, round(ln(100), 6) AS natural
```

The query returns the following result.


| base\_10 | natural | 
| --- | --- | 
| `2.0` | `4.60517` | 

The following SQL query shows that the result is negative infinity when `x` is `0`, and NaN when `x` is negative.

```sql
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 | 
| --- | --- | 
| `true` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-log-considerations"></a>

With one argument, `log` is the base-10 logarithm, not the natural logarithm. For the natural logarithm, use the [`ln` function](#athena-duckdb-functions-numeric-ln).

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
<a name="athena-duckdb-functions-numeric-log10"></a>

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](#athena-duckdb-functions-numeric-ln).

### Syntax
<a name="athena-duckdb-functions-numeric-log10-syntax"></a>

```sql
log10({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-log10-arguments"></a>

The `log10` function takes the following argument:

**{{x}}**  
The number to take the base-10 logarithm of, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-log10-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-log10-examples"></a>

The following SQL query returns the base-10 logarithm of `1000`.

```sql
SELECT log10(1000) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3.0` | 

The following SQL query returns the base-10 logarithm of a number between `0` and `1`, which is negative.

```sql
SELECT log10(0.001) AS result
```

The query returns the following result.


| result | 
| --- | 
| `-3.0` | 

The following SQL query shows that the result is negative infinity when `x` is `0`, and NaN when `x` is negative.

```sql
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 | 
| --- | --- | 
| `true` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-log10-considerations"></a>

`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
<a name="athena-duckdb-functions-numeric-log2"></a>

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, {{x}})` 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](#athena-duckdb-functions-numeric-ln).

### Syntax
<a name="athena-duckdb-functions-numeric-log2-syntax"></a>

```sql
log2({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-log2-arguments"></a>

The `log2` function takes the following argument:

**{{x}}**  
The number to take the base-2 logarithm of, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-log2-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-log2-examples"></a>

The following SQL query returns the base-2 logarithm of `8`.

```sql
SELECT log2(8) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3.0` | 

The following SQL query returns the base-2 logarithm of `0.5`, which is negative.

```sql
SELECT log2(0.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `-1.0` | 

The following SQL query shows that the result is negative infinity when `x` is `0`, and NaN when `x` is negative.

```sql
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 | 
| --- | --- | 
| `true` | `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-log2-considerations"></a>

`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
<a name="athena-duckdb-functions-numeric-sqrt"></a>

Returns the square root of `x`. If `x` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-numeric-sqrt-syntax"></a>

```sql
sqrt({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-sqrt-arguments"></a>

The `sqrt` function takes the following argument:

**{{x}}**  
The number to take the square root of, as a `DOUBLE` value. Integer, `DECIMAL`, and `FLOAT` values are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-sqrt-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-sqrt-examples"></a>

The following SQL query returns the square root of 4.

```sql
SELECT sqrt(4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2.0` | 

The following SQL query returns the square root of 2.

```sql
SELECT sqrt(2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1.4142135623730951` | 

The following SQL query shows that the square root of a negative number is NaN.

```sql
SELECT isnan(sqrt(-1)) AS is_nan
```

The query returns the following result.


| is\_nan | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-numeric-sqrt-considerations"></a>

For a negative `x`, `sqrt` returns NaN (not a number), not an error and not `NULL`. To check for this, use the [`isnan` function](duckdb-functions-numeric-sign-and-floating-point.md#athena-duckdb-functions-numeric-isnan).