The concept of remainder after division is not uniquely defined, and the two functionsmod
andrem
each compute a different variation. Themod
function produces a result that is either zero or has the same sign as the divisor. Therem
function produces a result that is either zero or has the same sign as the dividend.
Another difference is the convention when the divisor is zero. Themod
函数遵循convention thatmod(a,0)
returnsa
, whereas therem
函数遵循convention thatrem(a,0)
returnsNaN
.
Both variants have their uses. For example, in signal processing, themod
function is useful in the context of periodic signals because its output is periodic (with period equal to the divisor).