5 ms·
The one that returns a negative value for negative first argument has never done anything useful for me, and always made things that are actually useful harder
by Aardwolf 4y ago
The one that returns a negative value for negative first argument has never done anything useful for me, and always made things that are actually useful harder to do.
The only useful behavior is that crossing 0 does not change the pattern. Change my mind.
- ajuc 4y agoSame. Imagine we defined substraction in a way that requires almost all the users to check if arguments are positive. It's insane.
- plasticeagle 4y agoI know this is a typo, but I'm imagining that substraction is a form of abstraction where you take things away to make them simpler to understand.
- jjgreen 4y agoLikewise, but I was thinking of extraction of something from beneath something else, probably a mining term ...
- nh23423fefe 4y agoI 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
- croes 4y agoShouldn't be (x \ y) * y + (x mod y) = x? With \ as integer division
- deleted 4y ago[deleted]
- Skeime 4y agoBut it’s faster! Don’t you prefer a solution that is correct half the time over a slow solution that is correct all the time?
- thaumasiotes 4y agoAll of them are correct. There isn't a language in the table that will give you false information.
- Aardwolf 4y agoIs it really faster? If you want to modulo with powers of 2, you can use bit-and with a power of two minus 1, that has the good behavior and is definitely faster than what the one outputting negative values does. Or do you mean if based on integer division? I don't know what is fastest to implement in hardware for that one to be honest.
- Skeime 4y agoAt least that’s the reason I’ve heard for preferring C-style mod. Or rather, round-to-zero integer division it faster than round-to-negative-infinity integer division and arguably, you should choose / and % such that a = (a / b) * b + (a % b) always holds. This then forces the strange convention for %.
- LegionMammal978 4y ago> round-to-zero integer division it faster than round-to-negative-infinity integer division Do you have a source for this? Obviously, the big instruction sets natively implement round-to-zero division because that's what everyone already uses, but I've always attributed that to historical accident rather than first principles.
- Aardwolf 4y agoI'd even argue that flooring division is also more useful than round to zero and is also what binary shifting does. At least more useful for my use-cases (things like dealing with coordinates in game worlds, ...), but unlike for modulo, at least here I can imagine use cases where the other option, round to zero, is also useful (e.g. financial, ...)