floor_divide#
- ivy.floor_divide(x1, x2, /, *, out=None)[source]#
Round the result of dividing each element x1_i of the input array x1 by the respective element x2_i of the input array x2 to 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 wereint
s. 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_i
orx2_i
isNaN
, the result isNaN
.If
x1_i
is either+infinity
or-infinity
andx2_i
is either+infinity
or-infinity
, the result isNaN
.If
x1_i
is either+0
or-0
andx2_i
is either+0
or-0
, the result isNaN
.If
x1_i
is+0
andx2_i
is greater than0
, the result is+0
.If
x1_i
is-0
andx2_i
is greater than0
, the result is-0
.If
x1_i
is+0
andx2_i
is less than0
, the result is-0
.If
x1_i
is-0
andx2_i
is less than0
, the result is+0
.If
x1_i
is greater than0
andx2_i
is+0
, the result is+infinity
.If
x1_i
is greater than0
andx2_i
is-0
, the result is-infinity
.If
x1_i
is less than0
andx2_i
is+0
, the result is-infinity
.If
x1_i
is less than0
andx2_i
is-0
, the result is+infinity
.If
x1_i
is+infinity
andx2_i
is a positive (i.e., greater than0
) finite number, the result is+infinity
. (note: libraries may returnNaN
to match Python behavior.)If
x1_i
is+infinity
andx2_i
is a negative (i.e., less than0
) finite number, the result is-infinity
. (note: libraries may returnNaN
to match Python behavior.)If
x1_i
is-infinity
andx2_i
is a positive (i.e., greater than0
) finite number, the result is-infinity
. (note: libraries may returnNaN
to match Python behavior.)If
x1_i
is-infinity
andx2_i
is a negative (i.e., less than0
) finite number, the result is+infinity
. (note: libraries may returnNaN
to match Python behavior.)If
x1_i
is a positive (i.e., greater than0
) finite number andx2_i
is+infinity
, the result is+0
.If
x1_i
is a positive (i.e., greater than0
) finite number andx2_i
is-infinity
, the result is-0
. (note: libraries may return-1.0
to match Python behavior.)If
x1_i
is a negative (i.e., less than0
) finite number andx2_i
is+infinity
, the result is-0
. (note: libraries may return-1.0
to match Python behavior.)If
x1_i
is a negative (i.e., less than0
) finite number andx2_i
is-infinity
, the result is+0
.If
x1_i
andx2_i
have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.If
x1_i
andx2_i
have 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
, norNaN
is 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 aninfinity
of 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 (
Union
[float
,Array
,NativeArray
]) – first input array. Must have a numeric data type.x2 (
Union
[float
,Array
,NativeArray
]) – second input array. Must be compatible with x1 (with Broadcasting). Must have a numeric data type.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – an array containing the element-wise results. The returned array must have a numeric data type.
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the argumentsExamples
With
ivy.Array
inputs:>>> x1 = ivy.array([13., 7., 8.]) >>> x2 = ivy.array([3., 2., 7.]) >>> y = ivy.floor_divide(x1, x2) >>> print(y) ivy.array([4., 3., 1.])
>>> x1 = ivy.array([13., 7., 8.]) >>> x2 = ivy.array([3., 2., 7.]) >>> y = ivy.zeros((2, 3)) >>> ivy.floor_divide(x1, x2, out=y) >>> print(y) ivy.array([4., 3., 1.])
>>> x1 = ivy.array([13., 7., 8.]) >>> x2 = ivy.array([3., 2., 7.]) >>> ivy.floor_divide(x1, x2, out=x1) >>> print(x1) ivy.array([4., 3., 1.])
With a mix of
ivy.Array
andivy.NativeArray
inputs:>>> x1 = ivy.array([3., 4., 5.]) >>> x2 = ivy.native_array([5., 2., 1.]) >>> y = ivy.floor_divide(x1, x2) >>> print(y) ivy.array([0., 2., 5.])
With
ivy.Container
inputs:>>> x1 = ivy.Container(a=ivy.array([4., 5., 6.]), b=ivy.array([7., 8., 9.])) >>> x2 = ivy.Container(a=ivy.array([5., 4., 2.5]), b=ivy.array([2.3, 3.7, 5])) >>> y = ivy.floor_divide(x1, x2) >>> print(y) { a: ivy.array([0., 1., 2.]), b: ivy.array([3., 2., 1.]) }
With mixed
ivy.Container
andivy.Array
inputs:>>> x1 = ivy.Container(a=ivy.array([4., 5., 6.]), b=ivy.array([7., 8., 9.])) >>> x2 = ivy.array([2., 2., 2.]) >>> y = ivy.floor_divide(x1, x2) >>> print(y) { a: ivy.array([2., 2., 3.]), b: ivy.array([3., 4., 4.]) }
- Array.floor_divide(self, x2, /, *, out=None)[source]#
ivy.Array instance method variant of ivy.floor_divide. This method simply wraps the function, and so the docstring for ivy.floor_divide also applies to this method with minimal changes.
- Parameters:
self (
Array
) – dividend input array. Should have a real-valued data type.x2 (
Union
[Array
,NativeArray
]) – divisor input array. Must be compatible withself
(see broadcasting). Should have a real-valued data type.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the element-wise results. The returned array must have a data type determined by type-promotion.
Examples
With
ivy.Array
inputs:>>> x1 = ivy.array([13., 7., 8.]) >>> x2 = ivy.array([3., 2., 7.]) >>> y = x1.floor_divide(x2) >>> print(y) ivy.array([4., 3., 1.])
With mixed
ivy.Array
andivy.NativeArray
inputs:>>> x1 = ivy.array([13., 7., 8.]) >>> x2 = ivy.native_array([3., 2., 7.]) >>> y = x1.floor_divide(x2) >>> print(y) ivy.array([4., 3., 1.])
- Container.floor_divide(self, x2, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.floor_divide. This method simply wraps the function, and so the docstring for ivy.floor_divide also applies to this method with minimal changes.
- Parameters:
self (
Container
) – dividend input array or container. Should have a real-valued data type.x2 (
Union
[Container
,Array
,NativeArray
]) – divisor input array or container. Must be compatible withx1
(see broadcasting). Should have a real-valued data type.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.out (
Optional
[Container
], default:None
) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – a container containing the element-wise results. The returned container must have a data type determined by type-promotion.
Examples
With
ivy.Container
inputs:>>> x1 = ivy.Container(a=ivy.array([4., 5., 6.]), b=ivy.array([7., 8., 9.])) >>> x2 = ivy.Container(a=ivy.array([5., 4., 2.5]), b=ivy.array([2.3, 3.7, 5])) >>> y = x1.floor_divide(x2) >>> print(y) { a: ivy.array([0., 1., 2.]), b: ivy.array([3., 2., 1.]) }
With mixed
ivy.Container
andivy.Array
inputs:>>> x1 = ivy.Container(a=ivy.array([4., 5., 6.]), b=ivy.array([7., 8., 9.])) >>> x2 = ivy.array([2, 3, 4]) >>> y = x1.floor_divide(x2) >>> print(y) { a: ivy.array([2., 1., 1.]), b: ivy.array([3., 2., 2.]) }