フィルターのクリア

diffrence between rem and mod

12 ビュー (過去 30 日間)
hanish h
hanish h 2015 年 5 月 29 日
コメント済み: Walter Roberson 2022 年 1 月 9 日
mod(4,-10)
ans =
-6
>> rem(4,-10)
ans =
4
guys could you tell me in simple language whats is diffrence between two huh i know mod take the second number symbol but i didnt get the real math out of it

採用された回答

Star Strider
Star Strider 2015 年 5 月 29 日
The discussion in the documentation is here.
  2 件のコメント
hanish h
hanish h 2015 年 5 月 29 日
mod(4,-10)= -6 how do u get six man? this is the onli point im being stuck
Star Strider
Star Strider 2015 年 5 月 29 日
The difference is between the fix function (that rounds toward 0) and the floor function (that rounds toward -Inf).
For mod:
%MOD Modulus after division.
% MOD(x,y) is x - n.*y where n = floor(x./y) if y ~= 0. If y is not an
% integer and the quotient x./y is within roundoff error of an integer,
% then n is that integer. The inputs x and y must be real arrays of the
% same size, or real scalars.
%
% The statement "x and y are congruent mod m" means mod(x,m) == mod(y,m).
%
% By convention:
% MOD(x,0) is x.
% MOD(x,x) is 0.
% MOD(x,y), for x~=y and y~=0, has the same sign as y.
%
% 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.
%
% See also REM.
% Copyright 1984-2005 The MathWorks, Inc.
% Built-in function.
For rem:
%REM Remainder after division.
% REM(x,y) is x - n.*y where n = fix(x./y) if y ~= 0. If y is not an
% integer and the quotient x./y is within roundoff error of an integer,
% then n is that integer. The inputs x and y must be real arrays of the
% same size, or real scalars.
%
% By convention:
% REM(x,0) is NaN.
% REM(x,x), for x~=0, is 0.
% REM(x,y), for x~=y and y~=0, has the same sign as x.
%
% Note: MOD(x,y), for x~=y and y~=0, has the same sign as y.
% REM(x,y) and MOD(x,y) are equal if x and y have the same sign, but
% differ by y if x and y have different signs.
%
% See also MOD.
% Copyright 1984-2005 The MathWorks, Inc.
% Built-in function.

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

その他の回答 (1 件)

Samiu Haque
Samiu Haque 2020 年 9 月 7 日
When mod(4,-10) is used, it works as -10*1=-10 and the remainder becomes 4-10=-6
But when rem(4,-10) is used, it works as -10*0=0 and the remainder becomes 4-0=4
If the dividend and divisor both are positive integers, then rem() and mod() function returns the same result. But if either of them is negative, then mod() function avoid the multiple of zero and return the remainder considering the quotient as 1. This is because the mod() function's output is periodic.
  2 件のコメント
Moon Light
Moon Light 2022 年 1 月 8 日
mod(4,-3) ans= - 2
Why??
Walter Roberson
Walter Roberson 2022 年 1 月 9 日
(-3*-2) + (- 2) = 4 (-3*-1) + ( 1) = 4
However, when you use mod() and the remainder is not 0 then it will be the same sign as the modulus (second parameter)

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

カテゴリ

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