-4

5%5 is 0

5%4 is 1

5%3 is 2

5%2 is 1

5%1 is 0

Why is that? From my understanding modulus just prints out whether or not it has a remainder but I'm apparently wrong here.

also 5%10 prints out 5, and "%10" seems to consistently print out the last number of any value. 123%10 is 3 for some reason.

What's the deal here?

x = 5%2;
y = 123%10;
  • 2
    What do you think the result should be in each case? What is the remainder when you divide 5 by 5? It divides once and the remainder is `0` because it divides exactly. – Weather Vane May 18 '19 at 16:34
  • There is no remainder for that but 5/2 is 2.5. Where does C get a 1 from in that case? – qwertyuiop123456789 May 18 '19 at 16:36
  • 2
    Have you tried to google it, before asking in SO? – Hamza Ince May 18 '19 at 16:36
  • 1
    The `%` operator is doing **integer division**. Divide 5 by 2, you get 2 remainder 1. This is primary school stuff. – Weather Vane May 18 '19 at 16:36
  • @qwertyuiop123456789 In primary school, before you learn about the decimal point and stuff, 5/2 is 2, but 1 remains. – glglgl May 18 '19 at 16:39
  • 1
    It is no accident that `% 10` always gives the last digit, when you use decimal numbers. – Weather Vane May 18 '19 at 16:40
  • I have never heard of remainders before and I don't understand the concept. All math you use in highschool involves decimals, what purpose does this completely random number even serve? – qwertyuiop123456789 May 18 '19 at 16:40
  • As you have discovered, you can use it to extract the last digit of a decimal number, with a high degree of reliablilty that the same answer results each time. You might need the individual digits to, say, put on a display device which handles single digits. – Weather Vane May 18 '19 at 16:42
  • @glglgl is it possible you can explain the purpose of this? Google says it's the amount left over after division but it literally isn't considering decimals exist. I do not understand this concept in the slightest. – qwertyuiop123456789 May 18 '19 at 16:48
  • There's integer division, and division that results in decimals. 10 / 3 is either 3.333..., or 3 remainder 1 depending on what type of division you're doing. Surely you learned to do long division on paper when you were in school? That doesn't use decimals; that gives you a remainder. – Carcigenicate May 18 '19 at 16:50
  • 1
    Please read the duplicate question, which has at least a dozen answers. I hope you can get a satisfactory understanding from it. – Weather Vane May 18 '19 at 16:53
  • @qwertyuiop123456789 *I do not understand this concept in the slightest.* – Let's assume it's 22:00 ... what hour will the clock show in 4 hours from now? `(22 + 4) % 24 = 2` ... in 28 hours? `(22 + 28) % 24 = 2` ... – Swordfish May 18 '19 at 16:54
  • @qwertyuiop123456789 If you have 7 apples and 3 children, and you divide the apples among the children, each gets 2, and you keep (remain) 1. So 7/3, in integer division, is 2, with a remainder of 1. If you divide the one apple as well, eachh gets another ⅓ apple, so the "non-integer" result (which involves cutting an apple) is 2⅓. – glglgl May 20 '19 at 05:36

1 Answers1

0

From my understanding modulus just prints out whether or not it has a remainder but I'm apparently wrong here.

You are right, but only in so far as you recognize that you are wrong.

You think too complicated: modulus is not the decision if there is a remainder or not, but it just is the remainder. 0 thus means "no reminder", and so the modulo operation can be used in a boolean context.

This obviously leaves opwn what is meant by "division remainder".

Division per se is the question "If I have X objects and distribute it to Y targets, how many will each recipient get?"

The result differs whenther or nit I can divide a single object.

Assume I have 25 € (or $ or whatever) and want to distribute them to 4 people. Then we all assume that each is entitled to 6,25 €. But what happens if I oby have 1 € coins? Then I can distribute only 6 € to each, and I will have 1 € left which cannot be distributed in a fair way.

So this shows the two possible results of 25 / 4:

  • In floating point division, I get 6.25, and the reverse operation 6.25 * 4 gets 25 again, the original value.
  • In integer division, I get 6. But the reverse operation 6 * 4 doesn't get 25, but 24, so I have a remainder of 1.

On other words: The remainder of x / y is the result of x - (x / y) * y.

If any doubts are still open, Wikipedia has a lemma about the modulo operation as well.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • The problem here is that there is apparently a concept of "remainder" that I never learned in school(seemingly in elementary) and is literally never used again in middle or highschool. I don't even see the purpose of this and now trying to figure out how to program with it makes it seem even more confusing. – qwertyuiop123456789 May 18 '19 at 16:43