0

I have this simple line of code which shows as remainder 5:

Console.WriteLine(5 % 8);

The % operator shows the remainder after first number and second number are divided. So, 5 / 8 = 0,625. Shouldn't the reminder of the code above be 6 instead of 5? Why do I see 5 when I test in Visual Studio?

Burre Ifort
  • 653
  • 3
  • 15
  • 30
  • 1
    If you divide 5 by 8 you get 0 as quotient and 5 as remainder; that's it. – mshsayem Oct 20 '17 at 17:39
  • 1
    You're confused about what "modulo" means, that's all. It doesn't mean "do floating point division, multiply by ten, and truncate". – 15ee8f99-57ff-4f92-890c-b56153 Oct 20 '17 at 17:39
  • shouldn't the remainder be 6? Please explain more what you mean by "confused". – Burre Ifort Oct 20 '17 at 17:43
  • @BurreIfort Execute this code and look at the results -- or just google the phrase "integer division remainder" -- `Enumerable.Range(1, 20).Select(n => $"{n} % 8 == {n % 8}").ToList()`. You can't make up your own novel definitions for math terms and expect the .NET framework to change to suit you. It doesn't work that way. – 15ee8f99-57ff-4f92-890c-b56153 Oct 20 '17 at 17:44
  • 5 / 8 in **integer** division is ZERO. Five minus zero is five. The result minus the left hand operand (numerator) is the remainder. In floating point division there is no remainder; nothing left over. – 15ee8f99-57ff-4f92-890c-b56153 Oct 20 '17 at 17:46

3 Answers3

2

How many times does 8 go "cleanly" into 5? Zero times.

5 / 8 = 0 (int division)

When you do 5 ÷ 8, how many items are left over? 5.

5 % 8 = 5 (modulus division)

Another way to explain this is:

You have 5 apples. Please distribute those apples among 8 groups, but you must ensure that every group has an equal amount of apples.

You can't do that. You have less apples than the number of groups. You can't give any apples to anyone.

And because you didn't give any apples away, you are left with 5 apples. That is your remainder.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • With numbers (mathematically) I can, but with apples and pears I can't. – Burre Ifort Oct 20 '17 at 17:51
  • 2
    Remember, you are dealing with integers. You can't have decimal numbers. Yes, on a calculator `5/8=0.625`, but with c# and other programming, it equals 0. – gunr2171 Oct 20 '17 at 17:52
  • thanks gunr2171. It is clear now. I thought it should show what comes after zero, but I was wrong. thanks for your help. – Burre Ifort Oct 20 '17 at 18:00
2

Everything follows from the fundamental rules of integer division. For integers x and y suppose we have:

q = x / y;
r = x % y;

The fundamental rules are:

  • q and r are both integers
  • q * y + r == x
  • Division rounds towards zero when inexact

Now that you know the fundamental rules you can work out the values of 5 / 8 and 5 % 8.

5 / 8 is an integer. The exact value is 0.625, but that's not an integer, so we round towards zero and get 0. So q is 0.

To compute r we must solve for q * 8 + r = 5, which is easy to solve: r = 5 is the solution.

(Note that there are a few more fundamental rules about dividing by zero, and so on, that you usually don't have to worry about.)

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
0

Modulo doesn't give you the first number of the quotient, but the remainder after the second number is divided by the first.

8 doesn't go into 5 at all, so the remainder is the full 5.

See this answer for several other good explanations:

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

dzl
  • 908
  • 11
  • 32