View a markdown version of this page

Sign and floating-point functions - Amazon Athena

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, also named abs.

isfinite function

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. To test for NaN, use the isnan function.

Syntax

isfinite(x)

Arguments

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

BOOLEAN

Examples

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

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.

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.

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

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

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.

Syntax

isinf(x)

Arguments

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

BOOLEAN

Examples

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

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.

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.

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

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

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. To test whether a value is neither NaN nor infinity, use the isfinite function.

Syntax

isnan(x)

Arguments

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

BOOLEAN

Examples

The following SQL query tests a NaN value.

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.

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.

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

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

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

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

nextafter(x, y)

Arguments

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

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

Examples

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

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.

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.

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

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.

Syntax

sign(x)

Arguments

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

TINYINT, for every input type.

Examples

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

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.

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.

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

The query returns the following result.

result

0

Considerations

sign returns 0 for NaN, not NaN or NULL. To tell NaN apart from zero, use the isnan function.

Differences from Athena engine version 3

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

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

signbit(x)

Arguments

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

BOOLEAN

Examples

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

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.

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.

SELECT signbit(-0.0) AS result

The query returns the following result.

result

false

Considerations

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).