floor_divide¶
- floor_divide(x1: array, x2: array, /) array¶
Rounds the result of dividing each element
x1_iof the input arrayx1by the respective elementx2_iof the input arrayx2to the greatest (i.e., closest to+infinity) integer-value number that is not greater than the division result.Note
For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined.
Special cases
Note
Floor division was introduced in Python via PEP 238 with the goal to disambiguate “true division” (i.e., computing an approximation to the mathematical operation of division) from “floor division” (i.e., rounding the result of division toward negative infinity). The former was computed when one of the operands was a
float, while the latter was computed when both operands wereints. Overloading the/operator to support both behaviors led to subtle numerical bugs when integers are possible, but not expected.To resolve this ambiguity,
/was designated for true division, and//was designated for floor division. Semantically, floor division was defined as equivalent toa // b == floor(a/b); however, special floating-point cases were left ill-defined.Accordingly, floor division is not implemented consistently across array libraries for some of the special cases documented below. Namely, when one of the operands is
infinity, libraries may diverge with some choosing to strictly followfloor(a/b)and others choosing to pair//with%according to the relationb = a % b + b * (a // b). The special cases leading to divergent behavior are documented below.This specification prefers floor division to match
floor(divide(x1, x2))in order to avoid surprising and unexpected results; however, array libraries may choose to more strictly follow Python behavior.For floating-point operands,
If either
x1_iorx2_iisNaN, the result isNaN.If
x1_iis either+infinityor-infinityandx2_iis either+infinityor-infinity, the result isNaN.If
x1_iis either+0or-0andx2_iis either+0or-0, the result isNaN.If
x1_iis+0andx2_iis greater than0, the result is+0.If
x1_iis-0andx2_iis greater than0, the result is-0.If
x1_iis+0andx2_iis less than0, the result is-0.If
x1_iis-0andx2_iis less than0, the result is+0.If
x1_iis greater than0andx2_iis+0, the result is+infinity.If
x1_iis greater than0andx2_iis-0, the result is-infinity.If
x1_iis less than0andx2_iis+0, the result is-infinity.If
x1_iis less than0andx2_iis-0, the result is+infinity.If
x1_iis+infinityandx2_iis a positive (i.e., greater than0) finite number, the result is+infinity. (note: libraries may returnNaNto match Python behavior.)If
x1_iis+infinityandx2_iis a negative (i.e., less than0) finite number, the result is-infinity. (note: libraries may returnNaNto match Python behavior.)If
x1_iis-infinityandx2_iis a positive (i.e., greater than0) finite number, the result is-infinity. (note: libraries may returnNaNto match Python behavior.)If
x1_iis-infinityandx2_iis a negative (i.e., less than0) finite number, the result is+infinity. (note: libraries may returnNaNto match Python behavior.)If
x1_iis a positive (i.e., greater than0) finite number andx2_iis+infinity, the result is+0.If
x1_iis a positive (i.e., greater than0) finite number andx2_iis-infinity, the result is-0. (note: libraries may return-1.0to match Python behavior.)If
x1_iis a negative (i.e., less than0) finite number andx2_iis+infinity, the result is-0. (note: libraries may return-1.0to match Python behavior.)If
x1_iis a negative (i.e., less than0) finite number andx2_iis-infinity, the result is+0.If
x1_iandx2_ihave the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.If
x1_iandx2_ihave different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign.In the remaining cases, where neither
-infinity,+0,-0, norNaNis involved, the quotient must be computed and rounded to the greatest (i.e., closest to+infinity) representable integer-value number that is not greater than the division result. If the magnitude is too large to represent, the operation overflows and the result is aninfinityof appropriate mathematical sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
- Parameters:
x1 (array) – dividend input array. Should have a numeric data type.
x2 (array) – divisor input array. Must be compatible with
x1(see Broadcasting). Should have a numeric data type.
- Returns:
out (array) – an array containing the element-wise results. The returned array must have a data type determined by Type Promotion Rules.