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
DECIMALvalue. An integer value is converted toDECIMAL. y-
The divisor, a
DECIMALvalue. An integer value is converted toDECIMAL. scale-
The number of digits after the decimal point in the result, as a constant
INTEGERfrom0to38. If you omit it, the scale is the larger of6and. Here,s1+p2+ 1s1is the scale ofx, andp2is the precision ofy. This argument is optional.
Return type
DECIMAL(, where p1 - s1 + s2 + scale, scale)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 |
|---|
|
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 |
|---|
|
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 |
|---|
|
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 |
|---|---|
|
|
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(, and set x, y, scale)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( is the same as x, y)floor(. If either argument is x / y)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 |
|---|
|
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 |
|---|---|
|
|
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 |
|---|
|
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 |
|---|
|
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 , so it is x - y * floor(x / y)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
DOUBLEorFLOATvalue. Integer andDECIMALvalues are converted toDOUBLE, or toFLOATwhenyisFLOAT. y-
The divisor, as a
DOUBLEorFLOATvalue. Integer andDECIMALvalues are converted toDOUBLE, or toFLOATwhenxisFLOAT.
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 |
|---|
|
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 |
|---|---|
|
|
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 |
|---|
|
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, orHUGEINT. y-
The second number, as an integer value, such as
INTEGER,BIGINT, orHUGEINT.
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 |
|---|
|
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 |
|---|---|
|
|
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 |
|---|---|
|
|
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, orHUGEINT. y-
The second number, as an integer value, such as
INTEGER,BIGINT, orHUGEINT.
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 |
|---|
|
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 |
|---|---|
|
|
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.