Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a modulo operation work when the first number is smaller?

Tags:

modulo

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is.

But what if the first number is smaller than the second?

for instance

2 % 5 the answer is 2.

How does that work?

2/5 = .4

like image 311
Pete Avatar asked Oct 08 '09 04:10

Pete


People also ask

How does modulo work with smaller numbers?

If the numerator is smaller than the denominator, then the remainder is equal to the numerator. 3 % 10 =3 //If smaller number i.e. 3 is divided by bigger number i.e. 10, then the numerator becomes the remainder.

Is modulo always 1 or 0?

The range of values for an integer modulo operation of n is 0 to n − 1 inclusive (a mod 1 is always 0; a mod 0 is undefined, possibly resulting in a division by zero error in some programming languages). See Modular arithmetic for an older and related convention applied in number theory.

Can the result of a modulo operation be larger than the divisor?

Answer. No, the result of a modulo operation can never be larger than the divisor, assuming all positive values being used in the operation. If the division is done correctly, then the divisor was fitted into the dividend as many times as possible.

Where does modulo Fit in order of operations?

Most programming languages adopt the convention that the modulo operator (denoted by % rather than mod ) occupies the same place in the order of operations as multiplication and division. Hence, it comes AFTER the operations in parentheses, but BEFORE addition and subtraction.


2 Answers

Does this help

22  % 5 = 2  17  % 5 = 2  12  % 5 = 2  7   % 5 = 2  2   % 5 = 2 

Maybe this

22 / 5 = 4 + 2/5 17 / 5 = 3 + 2/5 12 / 5 = 2 + 2/5 7  / 5 = 1 + 2/5 2  / 5 = 0 + 2/5 
like image 148
jrhicks Avatar answered Sep 18 '22 06:09

jrhicks


5 goes into 2 zero times.

5*0 = 0

2-0 = 2.

The answer is 2.

like image 39
MedicineMan Avatar answered Sep 20 '22 06:09

MedicineMan