Better explanation to modulo?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

```

Replace this line with your code.

<do not remove the three backticks above>

#2

To understand modulo you could use this links, that explain the concept behind the operator.
https://javaranch.com/drive/modulo.html

In general terms the modulo operator returns the remainder after a number is divided by a divisor.


#3

In division there are four components…

Dividend    => D
divisor     => d
quotient    => q
Remainder   => R

In equation form,

D / d == q + R / d

where d, q and R are integers (whole numbers). When there is no Remainder, we say that the modulo of the division is zero.

D % d == 0

as an expression will be true for the above.

Remainder is the modulo. We can use the modulo to test for divisibility, but we can also use it in other ways to create periodic functions such as finding all the Thursdays in a given month.

Rewriting the equation from above we get to this…

D / d == q + R / d   ~ * d

D == d(q + R / d)    ~ expand

D == dq + R

Let’s see this in action…

 23 / 5 == 4 + 3 / 5    ~ * 5

 23 == 5 * 4 + 3

When we subtract 5 * 4 from both sides we get,

23 - 20 == 3

which tells us that 23 % 5 == 3.


#4

Added

Recall that a remainder is an unresolved quotient, hence, R / d. The modulo is a means for us to express this quantity. It does not just apply to integer division, but is easiest to explain in that form.

 Math.PI % 1

is a valid expression using Real numbers and the modulo will be a Real number, or in our case, a float.

0.14159265358979312

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.