Zulip Chat Archive
Stream: new members
Topic: Negative Modular Arithmetic, question about conventions
Ian Allen (Apr 18 2024 at 07:10):
It seems very unintuitive to me that "the least positive residue" convention is chosen. Take 5mod3 and (-5)mod3 for example. Having (q,r) = (1,2) in the first strongly implies (to me at least) that the second case should have the negative operator applied to it resulting in (q,r) = (-1,-2), instead of (-2,1). This preserves the symmetry as well as the intuitive nature: the absolute value of the divisor multiplied with the quotient is less than the absolute value of the dividend, and the remainder is added, always moving the real answer away from 0. As all numbers and operations are built first from the naturals starting with just 0 and 1 (thank you, lean number game), it seems also correct to anchor further definitions based on the behavior of working with strictly positives...
Is there a strong reason the least positive residue was chosen, from the context of number theory? Or is it largely resting on practical reasons emerging from computer science? Or am I just completely out of the ballpark here?
Is it even important (beyond needing to correct your work accordingly?)
Kevin Buzzard (Apr 18 2024 at 07:36):
It's very convenient to know that the remainder is always in a certain range because this now gives you a natural and well-behaved map from the integers to the ring Z/rZ thought of as the numbers from 0 to r-1
Ian Allen (Apr 18 2024 at 07:47):
Ok, but doesn't the other way also produce a well defined range? It's just got different bounds.. no? Maybe I should learn what rings do, finally...
Kevin Buzzard (Apr 18 2024 at 08:06):
Right, so your system gives us two well-defined ranges which is less convenient than one well-defined range
Ian Allen (Apr 18 2024 at 08:29):
Ahh ok. Thank you, Kevin.
Eric Wieser (Apr 18 2024 at 09:20):
I think the distinction here is docs#Int.div vs docs#Int.ediv
Eric Wieser (Apr 18 2024 at 09:22):
Though the docstrings are a little confusing because until very recently the meaning of /
swapped depending on what you import
Ian Allen (Apr 18 2024 at 09:33):
Yes exactly, what I was looking for was the reason for the propensity for one choice above the others. It is apparently just a choice of convenience, one that emerges I can imagine from having to write algorithms and efficiently predict behavior, which is satisfying enough. I just hate doing something that feels wrong, without knowing the reason for it..
Mauricio Collares (Apr 18 2024 at 09:47):
https://blog.vero.site/post/modulo is a good read on why C's modulo operation is the way it is. I think a big reason why people find negative remainders natural is familiarity with C.
Mauricio Collares (Apr 18 2024 at 09:48):
One big footgun in C is checking if a number is odd by doing n % 2 == 1
. This is obviously wrong given the context we have here, but it's very easy to forget about it while writing code.
Ian Allen (Apr 18 2024 at 10:00):
Thanks for the link. Pretty sure I am firmly in the truncate towards zero camp. Fun to see this is such a big topic after all, I came in thinking maybe there was just something I didn't understand.
Last updated: May 02 2025 at 03:31 UTC