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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ximeng86
  • 11
  • 1
  • 3
  • 2
    Does this answer your question? [The modulo operation on negative numbers in Python](https://stackoverflow.com/questions/3883004/the-modulo-operation-on-negative-numbers-in-python) – Abhishek Bhagate May 27 '20 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). – Michael Bianconi May 27 '20 at 11:41

2 Answers2

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

U. W.
  • 414
  • 2
  • 10
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 //.

chepner
  • 497,756
  • 71
  • 530
  • 681