这concept of remainder after division is not uniquely defined, and the two functionsmod
和rem
each compute a different variation. Themod
函数产生的结果为零或具有与除数相同的符号。这rem
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
,而rem
函数遵循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).