# megengine.functional.mod¶

mod(x, y)[源代码]

Element-wise $$\operatorname{mod}(x, y)$$ function.

Returns the remainder of division for each element $$x_i$$ of the input tensor $$x$$ and the respective element $$y_i$$ of the input tensor $$y$$.

• In general, similar to Python’s % operator, this function is not recommended for floating-point operands as semantics do not follow IEEE 754. That this function is specified to accept floating-point operands is primarily for reasons of backward compatibility.

• mod 是 NumPy 中 remainder 的别名。

Tensor

Special cases

For floating-point operands,

• If either $$x_i$$ or $$y_i$$ is NaN, the result is NaN.

• If $$x_i$$ is either +infinity or -infinity and $$y_i$$ is either +infinity or -infinity, the result is NaN.

• If $$x_i$$ is either +0 or -0 and $$y_i$$ is either +0 or -0, the result is NaN.

• If $$x_i$$ is +0 and $$y_i$$ is greater than 0, the result is +0.

• If $$x_i$$ is -0 and $$y_i$$ is greater than 0, the result is +0.

• If $$x_i$$ is +0 and $$y_i$$ is less than 0, the result is -0.

• If $$x_i$$ is -0 and $$y_i$$ is less than 0, the result is -0.

• If $$x_i$$ is greater than 0 and $$y_i$$ is +0, the result is NaN.

• If $$x_i$$ is greater than 0 and $$y_i$$ is -0, the result is NaN.

• If $$x_i$$ is less than 0 and $$y_i$$ is +0, the result is NaN.

• If $$x_i$$ is less than 0 and $$y_i$$ is -0, the result is NaN.

• If $$x_i$$ is +infinity and $$y_i$$ is a positive (i.e., greater than 0) finite number, the result is NaN.

• If $$x_i$$ is +infinity and $$y_i$$ is a negative (i.e., less than 0) finite number, the result is NaN.

• If $$x_i$$ is -infinity and $$y_i$$ is a positive (i.e., greater than 0) finite number, the result is NaN.

• If $$x_i$$ is -infinity and $$y_i$$ is a negative (i.e., less than 0) finite number, the result is NaN.

• If $$x_i$$ is a positive (i.e., greater than 0) finite number and $$y_i$$ is +infinity, the result is $$x_i$$. (note: this result matches Python behavior.)

• If $$x_i$$ is a positive (i.e., greater than 0) finite number and $$y_i$$ is -infinity, the result is $$y_i$$. (note: this result matches Python behavior.)

• If $$x_i$$ is a negative (i.e., less than 0) finite number and $$y_i$$ is +infinity, the result is $$y_i$$. (note: this results matches Python behavior.)

• If $$x_i$$ is a negative (i.e., less than 0) finite number and $$y_i$$ is -infinity, the result is $$x_i$$. (note: this result matches Python behavior.)

• In the remaining cases, the result must match that of the Python % operator.

>>> F.mod(8, 3)
Tensor(2, dtype=int32, device=xpux:0)


Element-wise mod:

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


>>> x = Tensor([1, 2, 3, 4, 5])