Rate this Page

torch.fmod#

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

Applies C++’sstd::fmod entrywise.The result has the same sign as the dividendinput and its absolute valueis less than that ofother.

This function may be defined in terms oftorch.div() as

torch.fmod(a,b)==a-a.div(b,rounding_mode="trunc")*b

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

Note

When the divisor is zero, returnsNaN for floating point dtypeson both CPU and GPU; raisesRuntimeError for integer division byzero on CPU; Integer division by zero on GPU may return any value.

Note

Complex inputs are not supported. In some cases, it is not mathematicallypossible to satisfy the definition of a modulo operation with complex numbers.

See also

torch.remainder() which implements Python’s modulus operator.This one is defined using division rounding down the result.

Parameters
  • input (Tensor) – the dividend

  • other (Tensor orScalar) – the divisor

Keyword Arguments

out (Tensor,optional) – the output tensor.

Example:

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