0

I encountered the claim that modulo is globally mathematically defined as $$a \bmod b = a - b \left\lfloor\frac{a}{b}\right\rfloor\;.$$ I asked for a source and the claimant could/did not provide one.

On top of how different programming languages handle negative numbers in the divisor or the dividend differently, suggesting that perhaps the mathematical definition is not so unambiguous, it appears that there are also weirdnesses about handling fractions, such that not all definitions will work.

Can someone please help clear this up, taking care to address the supposed mathematical/historical/universal definition of modulo (I'm not really asking about the computer definition as one can just accept each programming language's implementation)?

And, is it possible to provide a citation of some kind?

Mike Pierce
  • 18,938
ErikE
  • 222
  • 1
    See the links in this answer for various conventions. They are many prior questions on the conventions used in programming languages. – Bill Dubuque Nov 10 '16 at 04:21
  • 1
    This doesn't help me, unfortunately. I need something to counter the claim that there is only one, universal, widely-accepted definition. Just having a Wikipedia article on different possible implementations isn't enough. The other links in the answer you provided are specifically about a single programming language, not what will work for me, here. – ErikE Nov 10 '16 at 04:24
  • 2
    Same for mathematics. There is no universal convention. Number theorists don't worry about that so much because they generally prefer to use congruences, which don't force you to choose normal form reps for equivalence classes. – Bill Dubuque Nov 10 '16 at 04:28
  • It sounds like you know a little bit about the topic. Can you please elaborate and make an authoritative-sounding answer? How do congruentes work? – ErikE Nov 10 '16 at 04:30
  • 1
    See Wikipedia's article on modular arithmetic. – Bill Dubuque Nov 10 '16 at 04:32
  • In mathematics $a \bmod b$ is a little more than $a - b \lfloor a/b \rfloor$ the remainder of the Euclidean division of $a$ by $b$, but at 1st you can do as if it was the same. Now if you think to programming languages, then the modulo operator ($%$ or $\bmod$) is $a\ %\ b = a - b \lfloor a/b \rfloor$ yes – reuns Nov 10 '16 at 04:43
  • The answers are not the same, since $a \bmod b$ means the class of congruence ${ n\in \mathbb{Z}, n \ % \ b = a}$, allowing us to write $2 \equiv -1 \bmod 3$ – reuns Nov 10 '16 at 04:47
  • @user1952009 sounds like you could answer with a full discussion of the topic. Care to do that? – ErikE Nov 10 '16 at 04:47
  • No I just answered. I won't write a course on the topic, so explain what is unclear to you, we'll answer. – reuns Nov 10 '16 at 04:48
  • And in every programming language : $-1 \ % \ 3 = -1 - 3 \lfloor -1 / 3 \rfloor = -1- (-3) = 2$ – reuns Nov 10 '16 at 04:49
  • @user1952009 Not true, see the link I gave. – Bill Dubuque Nov 10 '16 at 05:08
  • @BillDubuque ok, find a programming language where $\lfloor -0.33 \rfloor \ne -1$ – reuns Nov 10 '16 at 05:10
  • @user1952009 Open Excel. Press Alt+F11. Press Ctrl+G. (Or go to the VBA Immediate pane) Type ?-1 Mod 5 and press Enter. – ErikE Nov 10 '16 at 05:17
  • If the result is $-1$, it is because it interpreted it as $-(1 \ \text{Mod} \ 5)$ – reuns Nov 10 '16 at 05:20
  • @user1952009 As I mentioned in the answer linked in my first comment, some programming languages have many variations of these functions that allow "rounding" or "truncating" in various ways, e.g. the floor, ceiling, round, truncate functions in Common Lisp – Bill Dubuque Nov 10 '16 at 14:00

1 Answers1

0

The definition of modulo in mathematics historically comes from Euclidean division. Euclidean division is finding the unique solution for a given a and b of $ a = bq + r $ where $ 0 \leq r \lt |b|$. The $r$ value is your modulus.