News for hackers. Stuff that matters.
Re:Mod and remainder are not the same by dmurray on Thursday August 23, 2018 @06:21AM (#17826023)
In algebra, mod is not an operator. We say 17 mod 4 is 1, but what we write is something like

17 = 1 (mod 4)

To say that 17 and 1 have the same remainder when divided by 4. If you do all of your operations and comparisons mod 4, it doesn't matter if we call the number 1 or -3.

In real life, we often need the remainder function, and we usually need it with positive numbers (how many eggs will I have left if I put these into boxes of 12). We sometimes call this the modulo function, which is a slight abuse of notation from the algebraic meaning of modulo. For positive numbers, it works completely intuitively. There are a couple of natural ways to extend this to negative numbers - an extension which would be meaningless or irrelevant in algebra - and computer language authors have (usually) called one rem and one mod.

Re:Mod and remainder are not the same by nmyk on Thursday August 23, 2018 @11:32AM (#17828065)
Strictly speaking, it's not the case that

17 = 1 (mod 4)

but rather

17 ≅ 1 (mod 4)

i.e. "17 is congruent to 1 mod 4", not "equal", though congruency mod n is an equivalence relation.

Re:Mod and remainder are not the same by gizmo686 on Thursday August 23, 2018 @06:25PM (#17831142)
What do you mean by "17".

The standard way of constructing modular arithmetic in "upper level" [0] is to define it over equivalence classes. For instance, let a ~ b mod 4 be the expected equivalence relationship for mod-4 arithmetic.

Then, define [x] = {a | a~x} to be the set of all numbers which are congruent to x mod 4.

At this point, we define [x]+[y] = [x+y] (along with the other arithmetic operations which are defined similarly). Then, there is just a bit of work to prove that the above definition is actually well defined (since the representative element we pick from both of the [x] and [y] sets is arbitrary, we must show that [x+y] is the same set regardless of which x and y we choose).

This means that, when we write 17 = 1 (mod 4), we are really saying [17]_4 = [1]_4; where [x]_n is the mod-n equivalence class containing x. The equal sign is literal, the numbers are not.

This type of implicit injection is actually fairly common in algebra. It is how we can view, for example, 17 as a natural number, real number, complex number, and polynomial without explicitly mapping it each time.

[0] Eg. an introductory undergrad course in abstract algebra.

Re:Mod and remainder are not the same by antoineMoPa on Thursday August 23, 2018 @12:10PM (#17828429)
Yeah I remember this notation completely messing up with my brain back in my discrete math class.
Re:Mod and remainder are not the same by 0x7f800000 on Thursday August 23, 2018 @12:20PM (#17828515)
Clock arithmetic.

Modulo 4: a clock with only four evenly-spaced marks on it.

17 is congruent to 1 (modulo 4) means:

Starting at the top of the clock, go clockwise one mark.

Starting at the top of the clock, go clockwise seventeen marks.

You'll end up at the same position.

That's congruence.

Re:Mod and remainder are not the same by alexlarsson on Thursday August 23, 2018 @06:44AM (#17826099)
I don't think that is quite true. Yes, for normal intergers 17 and 4, you can say the above. But mod can also be used as an operation on (and into) the equivalence classes of Z mod 4.
Re:Mod and remainder are not the same by edflsafoiewq on Thursday August 23, 2018 @09:21AM (#17827009)
For this reason, x % 0 should be x.
Re:Mod and remainder are not the same by abakker on Thursday August 23, 2018 @09:41AM (#17827143)
It's funny, I posted this same thing on the 1/0 = 1 thread the other day. It seemed to me that the logic was linked.

https://news.ycombinator.com/item?id=17737175

Re:Mod and remainder are not the same by gowld on Thursday August 23, 2018 @08:03AM (#17826481)
For polyglots, the problem is that % sometimes mean mod, sometimes means rem, or sometimes means something else:

-7 % 2

7 % -2

Admit 4 possible sets of answers.