megengine.functional.floor_div#

floor_div(x, y)[source]#

Element-wise floor division.

Rounds the result of dividing each element \(x_i\) of the input tensor \(x\) by the respective element \(y_i\) of the input tensor \(y\) to the greatest (i.e., closest to +infinity) integer-value number that is not greater than the division result.

Parameters:
Return type:

Tensor

Returns:

A tensor containing the element-wise results. The returned tensor must have a data type determined by Type promotion rules.

Special cases

For floating-point operands,

  • If either \(x\) or \(y\) is NaN, the result is NaN.

  • If \(x\) is either +infinity or -infinity and \(y\) is either +infinity or -infinity, the result is NaN.

  • If \(x\) is either +0 or -0 and \(y\) is either +0 or -0, the result is NaN.

  • If \(x\) is +0 and \(y\) is greater than 0, the result is +0.

  • If \(x\) is -0 and \(y\) is greater than 0, the result is -0.

  • If \(x\) is +0 and \(y\) is less than 0, the result is -0.

  • If \(x\) is -0 and \(y\) is less than 0, the result is +0.

  • If \(x\) is greater than 0 and \(y\) is +0, the result is +infinity.

  • If \(x\) is greater than 0 and \(y\) is -0, the result is -infinity.

  • If \(x\) is less than 0 and \(y\) is +0, the result is -infinity.

  • If \(x\) is less than 0 and \(y\) is -0, the result is +infinity.

  • If \(x\) is +infinity and \(y\) is a positive (i.e., greater than 0) finite number, the result is +infinity.

  • If \(x\) is +infinity and \(y\) is a negative (i.e., less than 0) finite number, the result is -infinity.

  • If \(x\) is -infinity and \(y\) is a positive (i.e., greater than 0) finite number, the result is -infinity.

  • If \(x\) is -infinity and \(y\) is a negative (i.e., less than 0) finite number, the result is +infinity.

  • If \(x\) is a positive (i.e., greater than 0) finite number and \(y\) is +infinity, the result is +0.

  • If \(x\) is a positive (i.e., greater than 0) finite number and \(y\) is -infinity, the result is -0.

  • If \(x\) is a negative (i.e., less than 0) finite number and \(y\) is +infinity, the result is -0.

  • If \(x\) is a negative (i.e., less than 0) finite number and \(y\) is -infinity, the result is +0.

  • If \(x\) and \(y\) have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.

  • If \(x\) and \(y\) 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, nor NaN is involved, the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too large to represent, the operation overflows and the result is an infinity 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.

Note

The // operator can be used as a shorthand for floor_div on tensors.

See also

In Python, // is the floor division operator and / is the true division operator. See div

Examples

>>> F.floor_div(5.0, 4.0)
Tensor(1.0, device=xpux:0)

Element-wise floor division:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> y = Tensor([[1, 1, 1], [2, 2, 2]])
>>> F.floor_div(x, y)
Tensor([[1 2 3]
 [2 2 3]], dtype=int32, device=xpux:0)

Broadcasting:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> F.floor_div(x, 2)
Tensor([[0 1 1]
 [2 2 3]], dtype=int32, device=xpux:0)