The concept of remainder after division isnot uniquely defined, and the two functions mod
and rem
eachcompute a different variation. The mod
functionproduces a result that is either zero or has the same sign as thedivisor. The rem
function produces a result thatis either zero or has the same sign as the dividend.
Another difference is the convention when the divisor is zero.The mod
function follows the convention that mod(a,0)
returns a
,whereas the rem
function follows the conventionthat rem(a,0)
returns NaN
.
Both variants have their uses. For example, in signal processing,the mod
function is useful in the context ofperiodic signals because its output is periodic (with period equalto the divisor).