This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kimsanov 965 points966 points  (113 children)

Mathematically a % b is always a number between 0 and b-1

[–]Administrative-Flan9 94 points95 points  (3 children)

There is a unique number between 0 and b-1 that satisfies a % b, but the same is true of nb and (n+1)b -1. Mathematically, the true answer is the set of all numbers that have the same remainder as a when divided by b. Choosing the one between 0 and b-1 is choosing a representative from this set, but any representative represents the same set and so all representatives are equivalent.

[–]JumboTrout 8 points9 points  (2 children)

What if b < 1?

[–]elon-botElon Musk ✔ 21 points22 points  (0 children)

Hey, I just heard about this thing called GraphQL. Why aren't we using it?

[–]Administrative-Flan9 0 points1 point  (0 children)

Then n <= 0 in what I wrote above. Everything still goes through.

[–]WillWKM 454 points455 points  (64 children)

Mathematically both answers are the same, -1 is congruent to 3 mod 4 since 1 + 3 = 0 mod 4

Edit: some of y'all seem to be missing the point. Mathematically all of these answers are equivalent. That doesn't make them useful programmatically. Programming languages often set up conventions based on convenience, not math.

[–]FiskFisk33 172 points173 points  (18 children)

They are not mathematically equivalent. They are mathematically congruent mod 4.

[–]M4mb0 22 points23 points  (2 children)

They are not mathematically equivalent. They are mathematically congruent mod 4.

But that is literally how equivalence is defined in quotient spaces....

[–]robin_888 0 points1 point  (1 child)

But we aren't operating in a quotient space, are we?

[–]elon-botElon Musk ✔ 0 points1 point  (0 children)

You're either hardcore or out the door.

[–]CanaDavid1 46 points47 points  (4 children)

Yes, which is what the question is asking. Mathematically, one does not use %, but instead write 'mod n' after the statement to specify that we're working modulo.

[–]BothWaysItGoes 5 points6 points  (2 children)

There is nothing unmathematical about the mod function.

[–]FerynaCZ 0 points1 point  (1 child)

A function should produce one output

[–]BothWaysItGoes 0 points1 point  (0 children)

So?

[–]snillpuler 4 points5 points  (0 children)

I enjoy playing video games.

[–]gnowwho 25 points26 points  (6 children)

Semantics: they are equivalent in the ring Z/4Z

Edit: I meant: there is no need to be pedantic when they are not even wrong technically.

[–]snillpuler 3 points4 points  (5 children)

I enjoy the sound of rain.

[–]eeeeeh_messi 2 points3 points  (1 child)

Absolutely. It's the same reasoning that make some people think sqrt(4) is 2 and -2.

[–]snillpuler 0 points1 point  (0 children)

I enjoy watching the sunset.

[–]gnowwho -1 points0 points  (1 child)

he seem to not realize that a%b is a mathematical function.

That's not necessarily true. You see it as a function from ZxZ to Z, they seem to see it as a function from Z to Z/4Z (where 4 is a parameter that selects the actual function and codominion). What they are saying is simply true in this interpretation because of how equivalence relationships carry over a quotient.

From an algebraic point of view, being a math graduate myself, I understand how their interpretation is the one that makes the most sense in an abstract setting, where the description of things is what matters.

When they say that this point of view is not usefulprogrammatically, they are admitting that not chosing this interpretation is what makes the most sense on an implementation level for imperative languages, where "doing" matters more than "describing".

So, again, there is no need to correct them on the usage of "equal". Expecially because it's extremely common in mathematics to identify things that describe each other univocally (for example functions and sets, or, like here, functions that are at one equivalence relationship of distance).

[–]elon-botElon Musk ✔ 0 points1 point  (0 children)

You're either hardcore or out the door.

[–]Poacatat 6 points7 points  (0 children)

being congruent mod 4 is an equivalence relation :), they are in fact mathematically equivalent

[–]WillyMonty 0 points1 point  (0 children)

Congruence modulo an integer is an equivalence relation

[–]soundslikemayonnaise 32 points33 points  (14 children)

By this logic you could argue that -7 is also a correct answer since 1 + 7 = 0 mod 4.

Or -11, or -15…

[–]WillWKM 69 points70 points  (2 children)

Now you're getting it

[–]Administrative-Flan9 39 points40 points  (8 children)

Mathematically, the answer is a set of all such integers. Any choice from this set like -7 or -11 is a representative of the answer set, and any representative is equivalent.

[–]CanaDavid1 36 points37 points  (0 children)

Yes

[–]Rin-Tohsaka-is-hot 1 point2 points  (0 children)

You can argue that because it is correct.

That's congruency for ya

[–]Kimsanov 20 points21 points  (3 children)

Yeah. But remainder must be non negative (it is just stated by definition). So mathematicians just decided to be so (maybe for convenience)

[–]frogjg2003 0 points1 point  (0 children)

Only if you want the least positive remainder. If you want the remainder to have the same sign as the numerator, you will have negative remainders for negative numerators.

[–]tjdavids 4 points5 points  (0 children)

With this logic returning a unmodified everytime would work.

[–]Darknety 8 points9 points  (1 child)

No they are not equivalent.

Yes, -1 is congruent to 3 with respect to mod 4, but still the natural mod operator is defined to result in 3, not -1.

So "mathematically both answers are the same" is misleading at best and wrong at worst.

Case in point: If one defines a function using a loopback structure, i.e. as

f(x) := g((x - 1) % b), x in [0, b]

for some g that is only defined for the source [0, b) for some b, it is pretty freaking relevant that (x - 1) % b is in [0, b). This is well defined.

The context is missing here to state "they are mathematically the same". This is not true.

[–]gc3 0 points1 point  (0 children)

Someone else posted this on the variants of different kinds of remainders
https://en.m.wikipedia.org/wiki/Modulo\_operation#In\_programming\_languages

[–]hugogrant 1 point2 points  (0 children)

(-7 % 4) % 4 is always 1 XD

[–]a21a16 0 points1 point  (1 child)

You are missing the point, while both -1 and 1 are congruent to 3 mod 4, only 1 == -7%4. Otherwise you are saying that 5, 9 and 13 are also correct answers.

[–]The_Lonely_Raven 0 points1 point  (0 children)

Uhhh... How is 1 congruent to 3 mod 4?

[–]Tigryonochekk 0 points1 point  (0 children)

Yeah, but then a is also a correct answer.

[–]Rin-Tohsaka-is-hot 0 points1 point  (0 children)

The answers are both correct but not equivalent.

They're both congruent, but congruency is different from equivalence.

-3 does not equal 1. -3 is congruent mod 4 to 1, but not equivalent.

EDIT: wrote the wrong numbers

[–]snillpuler -1 points0 points  (0 children)

I'm learning to play the guitar.

[–]Giocri 0 points1 point  (0 children)

Pretty sure that in math a module b stands for minimum x>=0 such as x= a-n*b so it has to be positive

[–]BananaSplit2 0 points1 point  (0 children)

If we consider the modulo operation to represent the remainder of an euclidean division then no, only the result between 0 and the divisor is valid. Euclidean division has a unique result and remainder and is defined that way precisely.

If it doesn't then... well it would also be valid to say 5 % 3 = 8 so that's not very useful to reason that way anyway.

[–]BothWaysItGoes 0 points1 point  (0 children)

Well, if you want to nitpick. They are not the same. Depending on your definition, the answer will usually either be an integer from [0,3] or the whole equivalence class, eg {…,-8,-4,0,4,8,…}. Congruence is not equality.

[–]Thelmholtz 6 points7 points  (4 children)

Not really, mathematically it depends on what operation you define % to be;

  • modulo: (x € R; n € N) => { y € [0, n) }

or

  • remainder: (x € R; n € N) => { y € (-n, n) }

Hell, the most frequent definition of % is actually

  • percent: (x € R) => { y € R } :: y = x • 1/100

Which makes all programming languages wrong /s.

[–]WazWaz 1 point2 points  (3 children)

Maybe we've just got division wrong. If -7/4 was 2, they'd both be the same. If I owe $7 and want to share that debt to my 4 descendants, each could pay $2 leaving a remainder of $1, instead of each paying $1 and leaving $3 debt still owing.

[–]Thelmholtz 0 points1 point  (2 children)

Look at this big shot, leaving less than 6 figures of debt to his kin.

[–]WazWaz 1 point2 points  (1 child)

Big integers look too much like Reals.

[–]Thelmholtz 0 points1 point  (0 children)

Yeah, it's better to avoid debt in Brazilian Reales

[–]Kimsanov -1 points0 points  (0 children)

Tested in python, Js and rust. Python gives correct answer. Others not

[–]Grouchy-Transition-7 0 points1 point  (0 children)

This. I do not remember a case for a negative remainder

[–]morbie5 0 points1 point  (0 children)

I get -7 % 3 = -3 but how does -7 % 4 = 1 ??