6 ms·
FWIW there's an entire paper on the subject of these operations: https://core.ac.uk/download/pdf/187613369.pdf https://core.ac.uk/download/pdf/187613369.pdf (w
by masklinn 5y ago
FWIW there's an entire paper on the subject of these operations: https://core.ac.uk/download/pdf/187613369.pdf https://core.ac.uk/download/pdf/187613369.pdf
(well a report in TOPLAS really, but that's pretty close)
There are technically 5 different versions of this operation, though only 2 (possibly 3) are in common use. Common Lisp seems to implement all of them except, somewhat oddly, the euclidean version (the "actual modulo"), its mod function is instead a flooring division.
edit: it looks like Python also follows flooring, not euclidean, meaning the quotient is rounded downwards, and per "a = nq + r" the remainder has the sign of the divisor:
>>> 5 % -2
-1
>>> divmod(5, -2)
(-3, -1)
The C version is the "truncating" division, meaning the quotient is truncated (rounded towards 0).
- aix1 5y agoAlso this TR: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf https://www.microsoft.com/en-us/research/wp-content/uploads/...
- cormacrelf 5y agoThis one is very concise, great as a reference, you can remind yourself of which negatives go where in an instant.
- saurik 5y agoI kind of get what you are saying syntactically, but the semantics are weird to me as the C version is only defined for integers in the first place so saying that the operator is truncating something is an awkward mental model.
- masklinn 5y ago> I kind of get what you are saying syntactically, but the semantics are weird to me as the C version is only defined for integers Why? Mathematically, dividing two integers yields a real. If you want the result to be an integer, you need to define some rounding operation on the real result to produce an integer. That is the source of the divergence between C and Python here, C rounds by truncation (towards 0), Python rounds by flooring (towards -inf).
- saurik 5y agoOh, I'm dumb. Sorry. (I got the terms for quotient, dividend, and divisor mixed up.)
- masklinn 5y ago> Oh, I'm dumb. We both know that's not true. > Sorry. (I got the terms for quotient, dividend, and divisor mixed up.) No big, brainfarts happen.
- DonHopkins 5y agoJust don't divide by zero. That's how you get black holes!
- dragonwriter 5y ago> Mathematically, dividing two integers yields a real Rational, more specifically.
- desio 5y ago> Mathematically, dividing two integers yields a real not true. if you define 'divide' as inverse multiplication over the reals then that is closer to being true (zero is an integer, dividing an integer by zero doesn't yield a real). in that case every real can be expressed as n = a/b where a and b are integers and b != 0 then yes n is a real. But 'divide' could just as well be defined along the lines of divisibility in number theory (the study of integers) where every integer can be expressed as n = q*b + r, where n,q,b,r are all integers and r >= 0 > b. then you could say that n divide b = q, with r as remainder, and n mod b = r. These are my preferred definition of mod and integer division.
- glandium 5y agoIf it's not truncating, how do you describe its behavior when e.g. an odd number is divided by 2?
- saurik 5y ago(Read the rest of the thread and look for my username.)
- deleted 5y ago[deleted]
- StefanKarpinski 5y agoGood paper—it was the primary source we looked at when implementing these operations in Julia. Specifically, Julia implements all versions and overloads `div` and `rem` to take a rounding mode argument. It also gives special names to a few of them and they come in pairs: `div` & `rem`, `fld` & `mod` (`fld` stands for “floored divide” as opposed to `div` which rounds towards zero). The `%` operator does remainder like C, which was a tough call, but we figured being compatible with C was worthwhile for people porting code. It’s also less useful in Julia due to default 1-based indexing of arrays; for that there is a `mod1` function that returns a value between 1 and n.
- thaumasiotes 5y ago> The `%` operator does remainder like C, which was a tough call, but we figured being compatible with C was worthwhile for people porting code. It’s also less useful in Julia due to default 1-based indexing of arrays; for that there is a `mod1` function that returns a value between 1 and n. After thinking about this, what I really want is a way to access an array with an unadjusted number and tell the array to do whatever modulizing is necessary. e.g. instead of a[small] a[mod1(big)] I'd like to have something more like a{small} a[big]
- bee_rider 5y agoHmm. This means we'd have to do mod every time we accessed an array which seems not great, right? But probably it could be optimized away for the common/easy cases.
- dmurray 5y agoJulia already has bounds checking at runtime on array access, so you could do the mod operation only if the bounds check fails. But I don't get how this would practically work. Would each array have a preferred n that is used for the mod operation? For most situations where that makes sense, using a 2d array seems more logical.
- 5y ago
- bee_rider 5y agoSomewhere out there in the universe, there's an nightmare that starts with "Well, zero is kind of an unusual number, so for safety sake we've defined our division to always round away from zero. This resulted in an interesting mod operation..."
- Supermancho 5y agoPonylang.io - There is a similar operation (toward zero) https://stdlib.ponylang.io/builtin-Integer/#div_unsafe https://stdlib.ponylang.io/builtin-Integer/#div_unsafe and, in Ponylang, i/0 usually results in 0.
- staticassertion 5y agoI think this is a bit different. Pony's div is total, and in order to be total div by 0 is 0, which is not a particularly absurd idea. That said, I do feel that Pony should have added a 'panic' concept and then forced actors to handle panics, but that's me.
- deleted 5y ago[deleted]
- kibwen 5y ago> which is not a particularly absurd idea It's not completely untenable, but it's still kinda janky. I've never heard of any mathematical argument that justifies division by zero yielding 0; contrast 0^0, which at least has an argument for yielding 1: http://mathscitech.org/articles/zero-to-zero-power http://mathscitech.org/articles/zero-to-zero-power
- duskwuff 5y ago> I've never heard of any mathematical argument that justifies division by zero yielding 0 0/x = 0 for all x except zero; defining 0/0=0 removes that exception.
- deleted 5y ago[deleted]