I confess I have grown increasingly in favor of how common lisp does this. The basic `/` creates rationals. And "floor" is a multiple value return where the first value is the floor and the second is the remainder. Granted, getting used to multiple value calls takes getting used to. I think I like it, but I also think I emphatically did not like it initially.
To be fair, Python's `/` creates floats, and this article is about a second operator `//`.
Why Python's integer division floors (2010)
91–100 of 102 posts
Re: Why Python's integer division floors (2010)
#92Why Python's Integer Division Floors - https://news.ycombinator.com/item?id=1630394 - Aug 2010 (2 comments)
Re: Why Python's integer division floors (2010)
#93Earlier quoted context omitted.
However, the oposite shift is undefined if a 1 goes into the sign bit. More precisely, regarding E1 "If E1 has a signed type and nonnegative value, and E1 × 2ᴱ² is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined." Thus if E1 is negative, or if the result overflows, UB.
Didn't they specify two's complement signed integers somewhat recently? What's the rationale for leaving these behaviours undefined?
Re: Why Python's integer division floors (2010)
#94Earlier quoted context omitted.
To be fair, Python's `/` creates floats, and this article is about a second operator `//`.
In python 2, '/' creates integers, and the article was written in 2010 when python 2 was the most used by far.
> PS. Note that I am using // instead of / -- this is Python 3 syntax
Re: Why Python's integer division floors (2010)
#95Re: Why Python's integer division floors (2010)
#96Re: Why Python's integer division floors (2010)
#97Earlier quoted context omitted.
I've always considered it a mistake to refer to "%" as a "modulo" operator in C because of its ability to return negative numbers. That's not how modulo arithmetic works. C's "%" is a remainder operator.
That's not any clearer. Both "modulo" and "remainder" can be defined in ways (yeah, more than one) that include or exclude negative numbers. You have to memorize the full definition. There's no mnemonic shortcut.
Modulus doesn’t. I believe that algebraically equality module N can exist without division
Re: Why Python's integer division floors (2010)
#98Earlier quoted context omitted.
I've always considered it a mistake to refer to "%" as a "modulo" operator in C because of its ability to return negative numbers. That's not how modulo arithmetic works. C's "%" is a remainder operator.
> That's not how modulo arithmetic works. We should be a bit careful with what one means by "modulo arithmetic" here. If we are talking about arithmetic in Z/nZ (read "integers modulo n"), then the objects being acted upon are no longer integers, but equivalence classes of integers. That is, the set of all integers that satisfy the equivalence relation "~" where "a ~ b" means a - b = k*n for some integer k. For examp…
Re: Why Python's integer division floors (2010)
#99Earlier quoted context omitted.
> That's not how modulo arithmetic works. We should be a bit careful with what one means by "modulo arithmetic" here. If we are talking about arithmetic in Z/nZ (read "integers modulo n"), then the objects being acted upon are no longer integers, but equivalence classes of integers. That is, the set of all integers that satisfy the equivalence relation "~" where "a ~ b" means a - b = k*n for some integer k. For examp…
But 2 mod 3 should still give the same as -1 mod 3. If the representative for the same equivalence class is allowed to be different depending on the input, you could just as well use the number itself.
On a more serious note, there are quite reasonable properties that are satisfied by keeping the sign of the first argument as the chosen representative. In particular, for any two nonzero integers a and b, we have that:
a = (a / b) * b + (a mod b)
Where division should be interpreted as integer division that rounds towards zero. If a=-1 and b=3, then (a/b) would be zero which would require (a mod b) to be -1 if we want the above to hold. Also note that other rounding choices (e.g., rounding towards negative infinity) could impose (a mod b) = 2.
So choosing a particular representative comes down to choosing what properties you want your function to have in relation to other arithmetic.
Re: Why Python's integer division floors (2010)
#100Earlier quoted context omitted.
But 2 mod 3 should still give the same as -1 mod 3. If the representative for the same equivalence class is allowed to be different depending on the input, you could just as well use the number itself.
At the risk of being snarky, I should certainly hope the output depends on the input. On a more serious note, there are quite reasonable properties that are satisfied by keeping the sign of the first argument as the chosen representative. In particular, for any two nonzero integers a and b, we have that: a = (a / b) * b + (a mod b) Where division should be interpreted as integer division that rounds towards zero. If…
... and that's exactly why the C modulo operator sucks.