3 ms·
I could imagine 2 use cases divMod is for when you want Z_n, you actually care about the equivalence classes and their "canonical" representative. quotRem is
by nh23423fefe 4y ago
I could imagine 2 use cases
divMod is for when you want Z_n, you actually care about the equivalence classes and their "canonical" representative.
quotRem is for symmetry. The values of the remainders are the same, but you get some sign flip for free if you want it.
So for example. Suppose you had some number of hours and you wanted to "convert to days", if you want symmetric outputs then quotRem will do that and divMod won't.
36 hours divMod 24(hours/day) = 1d + 12h
-36 hours divMod 24(hours/day) = -2d + 12h
36 hours quotRem 24(hours/day) = 1d + 12h
-36 hours quotRem 24(hours/day) = -1d - 12h
obviously the information is the same. 12 = -12 (mod 24) but the form is different.
- Chinjut 4y agoWhen do you ever want that symmetry?
- nh23423fefe 4y agoWhen your presentation layer should be symmetric about zero.
- thaumasiotes 4y agoYour terminology is a little weird. The idea that 0 <= remainder < divisor is part of the Division Algorithm, not modular arithmetic. And modular arithmetic, unlike remainders, is completely fine with negative numbers. But you're calling the function that gives you a correct remainder "divMod" and the one that only holds itself to modular equivalence "quotRem". If you divide -13 by 3, the "quotient" is -5 and the "remainder" is 2.
- danbruc 4y agoIf you divide -13 by 3, the "quotient" is -5 and the "remainder" is 2. You have this exactly backwards. If 13 divided by 3 answers the question how often does 3 fit into 13 and the answer is 4, then -13 / -3 should obviously also be 4. And -13 / 3 and 13 / -3 should be -4 as it makes no sense to say that 3 fits into 13 4 times but into -13 -5 times. Changing the sign of the argument changes the sign of the result, not the magnitude, or equivalently integer division rounds towards zero. The remainders follow from this and are all +1 or -1. Modular arithmetic on the other hand partitions the integers into equivalence classes and represents each equivalence class with a canonical member, usually the smallest non-negative one. So the integers modulo 3 - and of course also modulo -3 - form the following three equivalence. [0] => ..., -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, ... [1] => ..., -17, -14, -11, -8, -5, -2, 1, 4, 7, 10, 13, 16, ... [2] => ..., -16, -13, -10, -7, -4, -1, 2, 5, 8, 11, 14, 17, ... Therefore 13 belongs to the equivalence class with canonical representation 1 modulo ±3 and -13 to the one represented by 2.
- thaumasiotes 4y ago> You have this exactly backwards. You see how Division Algorithm is capitalized in my comment? I wasn't just having a seizure. The Division Algorithm is a famous theorem (the name is very old) that is generally taken to define the concepts of "quotient" and "remainder". By the standard approach, there is no such thing as a negative remainder. > Modular arithmetic on the other hand partitions the integers into equivalence classes and represents each equivalence class with a canonical member This is just false. Modular arithmetic doesn't represent each equivalence class with a canonical member. You either work with raw numbers ("1"), or you represent the equivalence classes directly ("[-2]").
- danbruc 4y agoYou see how Division Algorithm is capitalized in my comment? Missed that. But this just shows that Euclidean division is a bad choice if you are dealing with dividing signed quantities, that sign changes cause magnitude changes makes no sense in that case. This is just false. Modular arithmetic doesn't represent each equivalence class with a canonical member. You either work with raw numbers ("1"), or you represent the equivalence classes directly ("[-2]"). I don't really understand what you mean. If you say [13] - [24] = [-11] mod 3 that is certainly true, but what's the point then? Wouldn't you at least want [13] - [24] = [1] mod 3 even if you skip reducing [13] - [24] to [1] - [0] explicitly?
- nh23423fefe 4y ago[-11] == [1], even more so, they are the same object, because they are sets. Sure you can designate the answer however you want. 4/6 = 2/3. I'm not sure what you mean by "whats the point"
- nh23423fefe 4y agoYeah I agree, I'm just using the Haskell names of these functions. https://hackage.haskell.org/package/base-4.17.0.0/docs/src/GHC.Real.html#Integral https://hackage.haskell.org/package/base-4.17.0.0/docs/src/G...