dmfrl
dmfrl

Reputation: 337

Least Possible Value as described by Modulo Condition MATLAB

I am about to create a function in matlab which will accept multiple modulo and their corresponding remainders then it will determine the least possible value that will fit the given modulo conditions. Major trouble is that I am not allowed to use mod() and rem() built-in function in matlab. Can you help me with this?

Upvotes: 0

Views: 325

Answers (1)

nrz
nrz

Reputation: 10570

You can easily create custom my_mod and my_rem functions without using mod and rem, and you can use these as you would use mod and rem.

function modulus = my_mod(X, Y)
if isequal(Y, 0)
    modulus = X;
elseif isequal(X, Y)
    modulus = 0;
elseif (isequal(abs(X), Inf) || isequal(abs(Y), Inf))
    modulus = NaN;
else
    modulus = X - floor(X./Y) .* Y;
end
return

function remainder = my_rem(X, Y)
if isequal(Y, 0)
    remainder = NaN;
elseif isequal(X, Y)
    remainder = 0;
elseif (isequal(abs(X), Inf) || isequal(abs(Y), Inf))
    remainder = NaN;
else
    remainder = X - fix(X./Y) .* Y;
end
return

Upvotes: 1

Related Questions