View a markdown version of this page

Division and divisor functions - Amazon Athena

Division and divisor functions

Use these functions to get a quotient or remainder that differs from what the /, //, and % operators return, and to find common divisors and multiples of integers. For example, fdiv rounds a negative quotient down, and // rounds it toward zero.

decimal_division function

Returns x divided by y as a DECIMAL, without converting the values to floating point. When the exact quotient has more digits after the decimal point than the result scale, the function rounds it. A quotient that is exactly halfway between two values is rounded to the even one. If any argument is NULL, the result is NULL.

Use decimal_division when you need an exact DECIMAL quotient. The / operator converts DECIMAL values to DOUBLE, so its result is a floating-point approximation.

Syntax

decimal_division(x, y [, scale ])

Arguments

The decimal_division function takes the following arguments:

x

The dividend, a DECIMAL value. An integer value is converted to DECIMAL.

y

The divisor, a DECIMAL value. An integer value is converted to DECIMAL.

scale

The number of digits after the decimal point in the result, as a constant INTEGER from 0 to 38. If you omit it, the scale is the larger of 6 and s1 + p2 + 1. Here, s1 is the scale of x, and p2 is the precision of y. This argument is optional.

Return type

DECIMAL(p1 - s1 + s2 + scale, scale), where p1 and s1 are the precision and scale of x, and s2 is the scale of y. For example, dividing two DECIMAL(10,2) values with scale set to 2 returns DECIMAL(12,2).

Examples

The following SQL query divides 10.00 by 3.00 and returns the result with two digits after the decimal point.

SELECT decimal_division( CAST('10.00' AS DECIMAL(10,2)), CAST('3.00' AS DECIMAL(10,2)), 2 ) AS result

The query returns the following result.

result

3.33

The following SQL query divides 1.00 by 8.00 at a scale of 2. The exact quotient, 0.125, is halfway between 0.12 and 0.13, so the function rounds to the even value.

SELECT decimal_division( CAST('1.00' AS DECIMAL(10,2)), CAST('8.00' AS DECIMAL(10,2)), 2 ) AS result

The query returns the following result.

result

0.12

The following SQL query omits scale. The result scale is 13. That is the scale of x (2) plus the precision of y (10) plus 1, because that sum is larger than 6.

SELECT decimal_division( CAST('10.00' AS DECIMAL(10,2)), CAST('3.00' AS DECIMAL(10,2)) ) AS result

The query returns the following result.

result

3.3333333333333

The following SQL query compares the / operator, which returns a DOUBLE, with decimal_division, which returns a DECIMAL.

SELECT CAST( '10.00' AS DECIMAL(10,2) ) / CAST('3.00' AS DECIMAL(10,2)) AS slash_result, decimal_division( CAST('10.00' AS DECIMAL(10,2)), CAST('3.00' AS DECIMAL(10,2)), 2 ) AS exact_result

The query returns the following result.

slash_result exact_result

3.3333333333333335

3.33

Considerations

Always pass scale. Without it, the result scale is larger, and the query can fail because the result needs more than 38 digits.

If the result type needs more than 38 digits, the query fails. For example, dividing two DECIMAL(38,10) values with scale set to 10 fails. For more information about how to rewrite such a division, see Exact decimal division.

scale must be a constant, not a column. decimal_division does not accept DOUBLE values, so cast them to DECIMAL first. If y is 0, the query fails.

Differences from Athena engine version 3

In Athena engine version 3, the / operator divides two DECIMAL values and returns a DECIMAL. In DuckDB, / returns DOUBLE. To get a DECIMAL result, use decimal_division(x, y, scale), and set scale to the larger of the two input scales, which is the scale that Athena engine version 3 returns. An INTEGER or BIGINT input has a scale of 0.

When the exact quotient is halfway between two values at the result scale, decimal_division rounds to the even value. For example, 1.00 divided by 8.00 at a scale of 2 returns 0.12. Athena engine version 3 rounds away from zero and returns 0.13. For more information about getting the Athena engine version 3 result, see Exact decimal division.

fdiv function

Divides x by y and returns the largest integer value that is less than or equal to the quotient, as a floating-point number. The expression fdiv(x, y) is the same as floor(x / y). If either argument is NULL, the result is NULL.

For a negative quotient, rounding down moves away from zero, so fdiv(-7, 2) returns -4.0. The integer division operator // moves toward zero instead, and returns -3 for the same arguments.

Syntax

fdiv(x, y)

Arguments

The fdiv function takes the following arguments:

x

The dividend, as a value of any numeric type.

y

The divisor, as a value of any numeric type.

Return type

DOUBLE for integer, DECIMAL, and DOUBLE arguments. FLOAT when one argument is FLOAT and the other is not DOUBLE.

Examples

The following SQL query divides 7 by 2 and rounds the quotient down.

SELECT fdiv(7, 2) AS result

The query returns the following result.

result

3.0

The following SQL query compares fdiv with the // operator for a negative quotient. fdiv rounds down, and // rounds toward zero.

SELECT fdiv(-7, 2) AS fdiv_result, -7 // 2 AS integer_division

The query returns the following result.

fdiv_result integer_division

-4.0

-3

The following SQL query casts the result of fdiv to BIGINT to get an integer instead of a DOUBLE.

SELECT CAST(fdiv(-7, 2) AS BIGINT) AS result

The query returns the following result.

result

-4

The following SQL query divides by zero. The result is infinity, and the query does not fail.

SELECT isinf(fdiv(1, 0)) AS is_infinite

The query returns the following result.

is_infinite

true

Considerations

The result is a DOUBLE even when both arguments are integers. If you need an integer type, cast the result, for example CAST(fdiv(x, y) AS BIGINT).

When y is 0, the result is positive or negative infinity, or NaN (not a number) when x is also 0. The query does not fail. To find such results, test them with isinf and isnan.

fmod function

Returns the remainder of x divided by y, as a floating-point number. The result is x - y * floor(x / y), so it is 0 or has the same sign as y. If either argument is NULL, the result is NULL.

The % operator and mod return a remainder with the same sign as x instead. When the signs of x and y differ, fmod and % give different results. For example, fmod(-10, 3) returns 2.0, and -10 % 3 returns -1.

Syntax

fmod(x, y)

Arguments

The fmod function takes the following arguments:

x

The dividend, as a DOUBLE or FLOAT value. Integer and DECIMAL values are converted to DOUBLE, or to FLOAT when y is FLOAT.

y

The divisor, as a DOUBLE or FLOAT value. Integer and DECIMAL values are converted to DOUBLE, or to FLOAT when x is FLOAT.

Return type

DOUBLE, or FLOAT when one argument is FLOAT and the other is not DOUBLE. The result is a floating-point number even for integer arguments.

Examples

The following SQL query returns the remainder of 7.5 divided by 2.

SELECT fmod(7.5, 2) AS result

The query returns the following result.

result

1.5

The following SQL query compares fmod with the % operator for a negative dividend. fmod takes the sign of the divisor, and % takes the sign of the dividend.

SELECT fmod(-10, 3) AS fmod_result, -10 % 3 AS modulo_result

The query returns the following result.

fmod_result modulo_result

2.0

-1

The following SQL query divides by zero, so the result is NaN.

SELECT isnan(fmod(5, 0)) AS is_nan

The query returns the following result.

is_nan

true

Considerations

If y is 0, the result is NaN (not a number), and the query does not fail. To find such results, test them with isnan.

greatest_common_divisor function

Returns the largest integer that divides both x and y without a remainder. The result is never negative, even when an argument is negative. If one argument is 0, the result is the absolute value of the other, and if both are 0, the result is 0. If either argument is NULL, the result is NULL.

gcd is an alias of greatest_common_divisor. To get the least common multiple, use the least_common_multiple function.

Syntax

greatest_common_divisor(x, y)

Arguments

The greatest_common_divisor function takes the following arguments:

x

The first number, as an integer value, such as INTEGER, BIGINT, or HUGEINT.

y

The second number, as an integer value, such as INTEGER, BIGINT, or HUGEINT.

Return type

BIGINT, or HUGEINT when an argument is HUGEINT or UBIGINT.

Examples

The following SQL query returns the greatest common divisor of 42 and 57.

SELECT greatest_common_divisor(42, 57) AS result

The query returns the following result.

result

3

The following SQL query uses the gcd alias with a negative argument and with 0. Both results are positive.

SELECT gcd(-12, 18) AS negative_input, gcd(0, 5) AS with_zero

The query returns the following result.

negative_input with_zero

6

5

The following SQL query reduces the fraction 12/18 to lowest terms by dividing the numerator and the denominator by their greatest common divisor.

SELECT 12 // gcd(12, 18) AS numerator, 18 // gcd(12, 18) AS denominator

The query returns the following result.

numerator denominator

2

3

Considerations

The function accepts only integer types. A DECIMAL or DOUBLE argument, including a literal such as 12.0, makes the query fail. Cast such a value to an integer type first, for example gcd(CAST(x AS BIGINT), y).

least_common_multiple function

Returns the smallest positive integer that is a multiple of both x and y, or 0 when either argument is 0. The result is never negative, even when an argument is negative. If either argument is NULL, the result is NULL.

lcm is an alias of least_common_multiple. To get the greatest common divisor, use the greatest_common_divisor function.

Syntax

least_common_multiple(x, y)

Arguments

The least_common_multiple function takes the following arguments:

x

The first number, as an integer value, such as INTEGER, BIGINT, or HUGEINT.

y

The second number, as an integer value, such as INTEGER, BIGINT, or HUGEINT.

Return type

BIGINT, or HUGEINT when an argument is HUGEINT or UBIGINT.

Examples

The following SQL query returns the least common multiple of 42 and 57.

SELECT least_common_multiple(42, 57) AS result

The query returns the following result.

result

798

The following SQL query uses the lcm alias with a negative argument and with 0. The result for the negative argument is positive, and the result with 0 is 0.

SELECT lcm(-4, 6) AS negative_input, lcm(0, 6) AS with_zero

The query returns the following result.

negative_input with_zero

12

0

Considerations

If the result does not fit in a BIGINT, the query fails. For a larger result, cast an argument to HUGEINT, for example lcm(CAST(x AS HUGEINT), y).

The function accepts only integer types. A DECIMAL or DOUBLE argument makes the query fail. Cast such a value to an integer type first.