[HN Gopher] Modulo of negative numbers (2011)
___________________________________________________________________
Modulo of negative numbers (2011)
Author : rbanffy
Score : 27 points
Date : 2023-01-25 09:12 UTC (1 days ago)
(HTM) web link (torstencurdt.com)
(TXT) w3m dump (torstencurdt.com)
| michae2 wrote:
| Scheme also distinguishes between the cases using separate
| _remainder_ and _modulo_ functions.
|
| https://www.scheme.com/tspl4/objects.html#./objects:s98
| rightbyte wrote:
| The four different integer mod types gives me a headache. I just
| can't understand them or remember the difference.
|
| I solve the problem by never using mod or division for that
| matter with negative numbers.
| alphazard wrote:
| This is mostly just an issue of calling what is really a
| remainder operator "mod".
|
| The clearest solution is to disambiguate the two as Zig does.
| Aardwolf wrote:
| 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 wrote:
| Same. Imagine we defined substraction in a way that requires
| almost all the users to check if arguments are positive. It's
| insane.
| Skeime wrote:
| But 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?
| Aardwolf wrote:
| Is 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 wrote:
| At 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 %.
| nayuki wrote:
| Rust has x.rem_euclid(y), which differs from the % operator. See
| https://doc.rust-lang.org/std/primitive.i32.html#method.rem_... .
|
| Full demo: https://play.rust-
| lang.org/?version=stable&mode=debug&editio...
|
| Haskell has separate mod and rem functions too.
|
| Above and below the heading "Even or Odd", the <pre> blocks
| suffer from "scrollbar blindness" on Windows.
| https://web.archive.org/web/20200827132812/https://svenkadak...
| rurban wrote:
| perl started this bizarre interpretation of mod, some followed.
| The rest stayed with the POSIX libc definition.
| gerikson wrote:
| According to the Wikipedia page (linked in the article), Perl
| uses the floored definition of mod, which is promoted by Knuth,
| and also used by APL, Fortran and Common Lisp.
|
| https://en.wikipedia.org/wiki/Modulo
___________________________________________________________________
(page generated 2023-01-26 23:01 UTC)