It should be noted that in C89, both behaviors of division and modulo are allowed and it's implementation-defined whether division results are truncated or rounded towards negative infinity. This has changed in C99 which only allows truncation towards zero.
They made the correct decision here!<p>Too bad they made the wrong decision in not supporting the expected floating point behavior of division through zero (python throwing errors, rather than return inf or nan)
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.<p>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.
Note the top comment by “ark” - there’s really no perfect solution here.<p>In the floating-point case, you have to choose between negative remainders or potentially inexact results. And you definitely want integer division to work the same as float division.
Forth-83 beat C99 by 16 years!<p>>Python, unlike C, has the mod operator always return a positive number (twitter.com/id_aa_carmack):<p><a href="https://news.ycombinator.com/item?id=29729890">https://news.ycombinator.com/item?id=29729890</a><p><a href="https://twitter.com/ID_AA_Carmack/status/1476294133975240712" rel="nofollow">https://twitter.com/ID_AA_Carmack/status/1476294133975240712</a><p>tzs on Dec 30, 2021 | next [–]<p>>The submission is a tweet which doesn't really have a title, so the submitter was forced to make up one. Unfortunately the assertion in the chosen title is not correct. In Python the mod operator returns a number with the same sign as the second argument:<p><pre><code> >>> 10%3
1
>>> (-10)%3
2
>>> 10%(-3)
-2
>>> (-10)%(-3)
-1
</code></pre>
<a href="https://news.ycombinator.com/item?id=29732335">https://news.ycombinator.com/item?id=29732335</a><p>DonHopkins on Dec 30, 2021 | parent | context | favorite | on: Python, unlike C, has the mod operator always retu...<p>The FORTH-83 standard adopted floored division.<p>Signed Integer Division, by Robert L. Smith. Originally appearing in Dr. Dobb's Journal September 1983:<p><a href="https://wiki.forth-ev.de/doku.php/projects:signed_integer_division" rel="nofollow">https://wiki.forth-ev.de/doku.php/projects:signed_integer_di...</a><p>Lots of other languages got it wrong:<p><a href="https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages" rel="nofollow">https://en.wikipedia.org/wiki/Modulo_operation#In_programmin...</a><p>Symmetric division is the kind of thing that causes rockets ships to explode unexpectedly.<p>Symmetric division considered harmful:<p><a href="https://www.nimblemachines.com/symmetric-division-considered-harmful/" rel="nofollow">https://www.nimblemachines.com/symmetric-division-considered...</a><p>>Since its 1983 standard (Forth-83), Forth has implemented floored division as standard. Interestingly, almost all processor architectures natively implement symmetric division.<p>>What is the difference between the two types? In floored division, the quotient is truncated toward minus infinity (remember, this is integer division we’re talking about). In symmetric division, the quotient is truncated toward zero, which means that depending on the sign of the dividend, the quotient can be truncated in different directions. This is the source of its evil.<p>>I’ve thought about this a lot and have come to the conclusion that symmetric division should be considered harmful.<p>>There are two reasons that I think this: symmetric division yields results different from arithmetic right shifts (which floor), and both the quotient and remainder have singularities around zero.<p>>If you’re interested in the (gory) details, read on. [...]
Changing well known behavior for something no one is really going to need. The justification makes sense, but it breaks convention and the relationship with modulo doesn't need to hold for negative numbers.