torch.remainder(input, other, *, out=None)Tensor

Like torch.fmod() this applies C++’s std::fmod for floating point tensors and the modulus operation for integer tensors. Unlike torch.fmod(), however, if the sign of the modulus is different than the sign of the divisor other then the divisor is added to the modulus.

Supports broadcasting to a common shape, type promotion, and integer and float inputs.


Complex inputs are not supported. In some cases, it is not mathematically possible to satisfy the definition of a modulo operation with complex numbers. See torch.fmod() for how division by zero is handled.


This op, like NumPy’s remainder, is equivalent to Python’s modulus operation, and different from Python’s math.remainder and C++’s std::remainder which implement the IEEE remainder.

  • input (Tensor or Scalar) – the dividend

  • other (Tensor or Scalar) – the divisor

Keyword Arguments

out (Tensor, optional) – the output tensor.


>>> torch.remainder(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([ 1.,  0.,  1.,  1.,  0.,  1.])
>>> torch.remainder(torch.tensor([1, 2, 3, 4, 5]), -1.5)
tensor([ -0.5000, -1.0000,  0.0000, -0.5000, -1.0000 ])

See also

torch.fmod() which just computes the modulus for integer inputs and applies C++’s std::fmod for floating point inputs.


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources