フィルターのクリア

Difference between mod and rem functions

197 ビュー (過去 30 日間)
Lorenne
Lorenne 2018 年 6 月 4 日
編集済み: Stephen23 2019 年 11 月 30 日
May i know why is mod(4,-3)
ans =
-2
>> rem(4,-3)
ans =
1
these two answers different?
  1 件のコメント
Ignacy Szkudelski
Ignacy Szkudelski 2019 年 11 月 29 日
Note: REM(x,y), for x~=y and y~=0, has the same sign as x.
mod(x,y) and REM(x,y) are equal if x and y have the same sign, but
differ by y if x and y have different signs.

サインインしてコメントする。

回答 (2 件)

Stephen23
Stephen23 2018 年 6 月 4 日
編集済み: Stephen23 2018 年 6 月 4 日
The outputs of rem and mod are the same if the inputs have the same sign, otherwise it depends on how the division is interpreted. The MATLAB documentation states that "The concept of remainder after division is not uniquely defined, and the two functions mod and rem each compute a different variation. The mod function produces a result that is either zero or has the same sign as the divisor. The rem function produces a result that is either zero or has the same sign as the dividend."
The mod function's output is periodic, so this is useful where the periodicity is important (e.g. signal processing). You can also use this behavior for a simple test if a value is odd:
>> mod(-3,2)==1 % yes, -3 is odd!
ans = 1
>> rem(-3,2)==1 % does not work!
ans = 0
  2 件のコメント
Lorenne
Lorenne 2018 年 6 月 5 日
I don't really understand, does it mean that mod and rem both calculates the remainder but in a different way? How do they actually work?
Stephen23
Stephen23 2018 年 6 月 5 日
編集済み: Stephen23 2019 年 11 月 30 日
"...does it mean that mod and rem both calculates the remainder but in a different way?"
Yes. Because there are different concepts of what "remainder" means for negative values. There is no "correct" definition, because both of them are useful in different situations: rem gives the remainder that people might think of when asking themselves "divide the input Q times, what remains?", whereas mod is perfectly periodic. Neither is better than the other, they just do different things.
"How do they actually work?"
As the documentation explains (and Jan Simon already showed you):
  • mod(a,b) is defined as a-b.*fix(a./b)
  • rem(a,b) is defined as a-b.*floor(a./b)
The difference is clear when you plot their outputs:
>> X = -10:0.1:10;
>> plot(X,mod(X,4))
>> title('mod')
mod.png
Versus
>> X = -10:0.1:10;
>> plot(X,rem(X,4))
>> title('rem')
rem.png

サインインしてコメントする。


Jan
Jan 2018 年 6 月 4 日
編集済み: Jan 2018 年 6 月 4 日
See: doc rem and doc mod:
mod is: r = a - b .* floor(a ./ b)
rem is: r = a - b .* fix(a ./ b)
This is the same for positive values, but differs by b for negative values. floor is the next smaller integer, fix is removing the fractional part.
  3 件のコメント
Stephen23
Stephen23 2018 年 6 月 5 日
編集済み: Stephen23 2018 年 6 月 5 日
fix is described as "Round toward zero". Compare against floor "Round toward negative infinity":
>> -6/4
ans = -1.500
>> fix(-6/4)
ans = -1
>> floor(-6/4)
ans = -2
Jan
Jan 2018 年 6 月 5 日
@Lorenne: The complete behavior is explained exhaustively in the documentation. See:
doc rem
doc mod
doc fix
As described there (and mentioned by Stephen already), fix rounds towards zero. "6/4" is stored as 1.5 in Matlab. Then removing the fractional part means to set all digits right from the decimal point to zero. Try it:
fix(1.5)
>> 1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by