

# Sign and floating-point functions

Use these functions to get the sign of a number, test values for infinity and NaN (not a number), and step a floating-point number to the next value. To get the absolute value of a number, use the [`@` (absolute value) operator](duckdb-functions-numeric-arithmetic-operators.md#athena-duckdb-functions-numeric-absolute-value-operator), also named `abs`.

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

Returns `true` if `x` is finite, and `false` if it is positive or negative infinity. For a floating-point number, NaN (not a number) is not finite either, so the function returns `false` for it. For a date or timestamp, the special values `infinity` and `-infinity` return `false`. If `x` is `NULL`, the result is `NULL`.

To test only for infinity, use the [`isinf` function](#athena-duckdb-functions-numeric-isinf). To test for NaN, use the [`isnan` function](#athena-duckdb-functions-numeric-isnan).

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

```sql
isfinite({{x}})
```

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

The `isfinite` function takes the following argument:

**{{x}}**  
The value to test, as a `DOUBLE`, `FLOAT`, `DATE`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE` value. Integer and `DECIMAL` values are converted to `DOUBLE`.

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

`BOOLEAN`

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

The following SQL query tests whether the number `5.5` is finite.

```sql
SELECT isfinite(5.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query tests positive infinity and NaN. Neither is finite.

```sql
SELECT
    isfinite(
        CAST('infinity' AS DOUBLE)
    ) AS positive_infinity,
    isfinite(CAST('nan' AS DOUBLE)) AS not_a_number
```

The query returns the following result.


| positive\_infinity | not\_a\_number | 
| --- | --- | 
| `false` | `false` | 

The following SQL query tests the special date `infinity` and a regular date.

```sql
SELECT
    isfinite(DATE 'infinity') AS infinite_date,
    isfinite(DATE '2021-08-03') AS regular_date
```

The query returns the following result.


| infinite\_date | regular\_date | 
| --- | --- | 
| `false` | `true` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-numeric-isfinite-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `is_finite`, and writes positive infinity as `infinity()`. DuckDB does not have `is_finite` or `infinity()`, so use `isfinite`, and write infinity as `CAST('infinity' AS DOUBLE)`.

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

Returns `true` if `x` is positive or negative infinity, and `false` otherwise. NaN (not a number) is not infinite, so the function returns `false` for it. For a date or timestamp, the special values `infinity` and `-infinity` return `true`. If `x` is `NULL`, the result is `NULL`.

To test whether a value is finite, which also rules out NaN, use the [`isfinite` function](#athena-duckdb-functions-numeric-isfinite).

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

```sql
isinf({{x}})
```

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

The `isinf` function takes the following argument:

**{{x}}**  
The value to test, as a `DOUBLE`, `FLOAT`, `DATE`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE` value. Integer and `DECIMAL` values are converted to `DOUBLE`.

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

`BOOLEAN`

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

The following SQL query uses the `::` cast operator to convert the string `'Infinity'` to a `FLOAT` value, and tests whether that value is infinite.

```sql
SELECT isinf('Infinity'::FLOAT) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query tests negative infinity and NaN. NaN is not infinite.

```sql
SELECT
    isinf(
        CAST('-infinity' AS DOUBLE)
    ) AS negative_infinity,
    isinf(CAST('nan' AS DOUBLE)) AS not_a_number
```

The query returns the following result.


| negative\_infinity | not\_a\_number | 
| --- | --- | 
| `true` | `false` | 

The following SQL query tests the special timestamp `infinity` and a regular timestamp.

```sql
SELECT
    isinf(TIMESTAMP 'infinity') AS infinite_timestamp,
    isinf(
        TIMESTAMP '2021-08-03 11:59:44'
    ) AS regular_timestamp
```

The query returns the following result.


| infinite\_timestamp | regular\_timestamp | 
| --- | --- | 
| `true` | `false` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-numeric-isinf-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `is_infinite`, and writes positive infinity as `infinity()`. DuckDB does not have `is_infinite` or `infinity()`, so use `isinf`, and write infinity as `CAST('infinity' AS DOUBLE)`.

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

Returns `true` if `x` is NaN (not a number), and `false` for any other value, including positive and negative infinity. If `x` is `NULL`, the result is `NULL`, not `false`.

To write a NaN value in a query, use `CAST('NaN' AS DOUBLE)`. To test for infinity, use the [`isinf` function](#athena-duckdb-functions-numeric-isinf). To test whether a value is neither NaN nor infinity, use the [`isfinite` function](#athena-duckdb-functions-numeric-isfinite).

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

```sql
isnan({{x}})
```

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

The `isnan` function takes the following argument:

**{{x}}**  
The value to test, as a `DOUBLE` or `FLOAT` value. Integer and `DECIMAL` values are converted to `DOUBLE`, so the result for them is always `false`.

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

`BOOLEAN`

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

The following SQL query tests a NaN value.

```sql
SELECT isnan(CAST('NaN' AS DOUBLE)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query tests a number, a NaN value, and a `NULL`. For the `NULL`, the result is `NULL`, not `false`.

```sql
SELECT label, isnan(x) AS is_nan
FROM (VALUES
    ('number', 1.5),
    ('not a number', CAST('NaN' AS DOUBLE)),
    ('missing', NULL)
) AS t(label, x)
ORDER BY label
```

The query returns the following results.


| label | is\_nan | 
| --- | --- | 
| `missing` | `NULL` | 
| `not a number` | `true` | 
| `number` | `false` | 

The following SQL query shows that NaN is equal to itself and greater than positive infinity.

```sql
SELECT
    CAST(
        'NaN' AS DOUBLE
    ) = CAST('NaN' AS DOUBLE) AS nan_equals_nan,
    CAST(
        'NaN' AS DOUBLE
    ) > CAST('infinity' AS DOUBLE) AS nan_greater_than_infinity
```

The query returns the following result.


| nan\_equals\_nan | nan\_greater\_than\_infinity | 
| --- | --- | 
| `true` | `true` | 

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

In DuckDB, NaN is equal to itself and greater than every other number, including positive infinity. A comparison such as `{{x}} = CAST('NaN' AS DOUBLE)` finds NaN values, and a filter such as `{{x}} > 1000` also returns them. In ascending order, NaN values come after positive infinity. To leave NaN values out, add a condition such as `NOT isnan({{x}})`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-numeric-isnan-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `is_nan`, and writes NaN as `nan()`. DuckDB does not have `is_nan` or `nan()`, so use `isnan`, and write NaN as `CAST('NaN' AS DOUBLE)`.

In Athena engine version 3, a comparison such as `=` or `>` with NaN returns `false`, so `nan() = nan()` is `false`. In DuckDB, NaN equals itself and is greater than every other number, so a filter such as `{{x}} > 1000` can return NaN values that Athena engine version 3 leaves out. To get the Athena engine version 3 result, add `NOT isnan({{x}})` to the filter.

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

Returns the floating-point number that is next to `x` in the direction of `y`. This is the next larger value when `y` is greater than `x`, and the next smaller value when `y` is less than `x`. If `x` equals `y`, the result is `y`. If either argument is `NULL`, the result is `NULL`.

The size of the step depends on the type. With `FLOAT` arguments, the result is the next `FLOAT` value. Integer and `DECIMAL` arguments are converted to `DOUBLE`, or to `FLOAT` when the other argument is `FLOAT`. With `DOUBLE` arguments, the result is the next `DOUBLE` value, which is a much smaller step.

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

```sql
nextafter({{x}}, {{y}})
```

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

The `nextafter` function takes the following arguments:

**{{x}}**  
The starting value, as a `DOUBLE` or `FLOAT` value.

**{{y}}**  
The value that sets the direction, as a `DOUBLE` or `FLOAT` value.

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

`DOUBLE`, or `FLOAT` when one argument is `FLOAT` and the other is not `DOUBLE`.

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

The following SQL query returns the next `FLOAT` value after `1`, in the direction of `2`.

```sql
SELECT
    nextafter(
        CAST(1 AS FLOAT), CAST(2 AS FLOAT)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1.0000001` | 

The following SQL query returns the next `DOUBLE` value before `1`, in the direction of `0`.

```sql
SELECT nextafter(CAST(1 AS DOUBLE), 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.9999999999999999` | 

The following SQL query shows that the result is greater than `x` when `y` is greater, and equal to `x` when both arguments are the same.

```sql
SELECT
    nextafter(1, 2) > 1 AS is_greater,
    nextafter(1, 1) = 1 AS unchanged
```

The query returns the following result.


| is\_greater | unchanged | 
| --- | --- | 
| `true` | `true` | 

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

Returns `-1` when `x` is negative, `0` when it is zero, and `1` when it is positive. If `x` is `NULL`, the result is `NULL`.

For `FLOAT` and `DOUBLE` values, `sign` returns `-1` for negative infinity and `1` for positive infinity. It returns `0` for NaN (not a number) and for negative zero. To tell negative zero from zero, use the [`signbit` function](#athena-duckdb-functions-numeric-signbit).

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

```sql
sign({{x}})
```

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

The `sign` function takes the following argument:

**{{x}}**  
The number whose sign to return, as a value of any integer type, signed or unsigned, or a `FLOAT` or `DOUBLE` value. A `DECIMAL` value is converted to `DOUBLE`.

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

`TINYINT`, for every input type.

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

The following SQL query returns the sign of a negative number.

```sql
SELECT sign(-349) AS result
```

The query returns the following result.


| result | 
| --- | 
| `-1` | 

The following SQL query returns the sign of a negative number, zero, and a positive number.

```sql
SELECT x, sign(x) AS result
FROM (VALUES (-5), (0), (3)) AS v(x)
ORDER BY x
```

The query returns the following results.


| x | result | 
| --- | --- | 
| `-5` | `-1` | 
| `0` | `0` | 
| `3` | `1` | 

The following SQL query shows that the sign of NaN is `0`.

```sql
SELECT sign(CAST('nan' AS DOUBLE)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

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

`sign` returns `0` for NaN, not NaN or `NULL`. To tell NaN apart from zero, use the [`isnan` function](#athena-duckdb-functions-numeric-isnan).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-numeric-sign-differences-from-engine-version-3"></a>

In Athena engine version 3, `sign` of NaN returns NaN. In DuckDB, it returns `0`, as a `TINYINT`.

In Athena engine version 3, `sign` of a `DOUBLE` value returns a `DOUBLE`, so `sign(-2.5E0)` returns `-1.0`. In DuckDB, `sign` always returns `TINYINT`, so the same call returns `-1`.

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

Returns `true` when the sign bit of `x` is set, and `false` when it is not. The sign bit is set for negative numbers, for negative infinity, and for negative zero, a value that compares equal to `0`. If `x` is `NULL`, the result is `NULL`.

Use `signbit` to tell negative zero from zero. `sign` returns `0` for both.

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

```sql
signbit({{x}})
```

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

The `signbit` function takes the following argument:

**{{x}}**  
The number to test, as a `FLOAT` or `DOUBLE` value. Integer and `DECIMAL` values are converted to `DOUBLE`.

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

`BOOLEAN`

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

The following SQL query checks the sign bit of a negative number.

```sql
SELECT signbit(-1.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query passes negative zero as a `DOUBLE`. The value equals `0`, and `sign` returns `0`, but its sign bit is set.

```sql
SELECT
    sign(x) AS sign_result,
    signbit(x) AS signbit_result,
    x = 0 AS equals_zero
FROM (VALUES (CAST('-0.0' AS DOUBLE))) AS v(x)
```

The query returns the following result.


| sign\_result | signbit\_result | equals\_zero | 
| --- | --- | --- | 
| `0` | `true` | `true` | 

The following SQL query passes the literal `-0.0`. It is a `DECIMAL` value, which has no negative zero, so the result is `false`.

```sql
SELECT signbit(-0.0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

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

A literal such as `-0.0` is a `DECIMAL` value, and `DECIMAL` values have no negative zero, so `signbit(-0.0)` returns `false`. `CAST(-0.0 AS DOUBLE)` also returns a positive zero. To pass negative zero, write a `DOUBLE` literal such as `-0.0e0`, or use `CAST('-0.0' AS DOUBLE)`.