0

I get that the modulo operator (%) is used to get the remainder of a division, so for example:

7 % 3 = 1 

But, why does 1 % 60 = 1 and not 0.084? (1 - 0.016)

Why does -1 % 60 = 59?

There must be something I don't understand about how this operator works.

2
  • 2
    Does this answer your question? The modulo operation on negative numbers in Python Commented May 27, 2020 at 11:38
  • In addition to the above, n % m = n for 0 < n < m. In your example, the grade school representation of 1/60 would be 0R1 (zero remainder 1). Commented May 27, 2020 at 11:41

2 Answers 2

0

Indeed.

1 divided by 60 is 0, the remainder is 1. Remainders are always Integers.

And the 59 is correct, because -1 // 60 is -1, and it must be true, that n == (n // divisor)*divisor + remainder, so -1 * 60 + 59 == -1

Sign up to request clarification or add additional context in comments.

Comments

0

Let's look at the more general function divmod, which gives you both the quotient and the remainder:

divmod(n, m) == n // m, n % m 

divmod obeys the following law (see help(divmod)) for all integers n and m:

q, r = divmod(n, m) <==> m*q + r == n 

Further, m and r always have the same sign, and 0 <= abs(r) < abs(m). Neither of these is explicitly stated, but each follows from the definition of //.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.