News for hackers. Stuff that matters.
Re:Integers in C quiz by jart on Sunday September 04, 2022 @08:48AM (#32712463)
Another interesting thing about C integers that the quiz doesn't cover is that remainder is not modulus. For example, in Python:

    >>: 2 % -5
    -3
But in C:

    2 % -5 == 2
If you want to use modulus in C rather than Euclidean remainder, then you have to use a function like this, which does what Python does:

    long mod(long x, long y) {
      if (y == -1) return 0;
      return x - y * (x / y - (x % y && (x ^ y) < 0));
    }
Re:Integers in C quiz by qsort on Sunday September 04, 2022 @09:08AM (#32712626)
Mathematically speaking, the mod is usually taken to be positive. Any reasonable definition would involve taking the quotient of Z over the congruence relation, so I can see both -3 and 2 being reasonable conventions to represent partitions.

OTOH, the largest "wat" of C-like languages is the following:

  > -8 % 5
  -3
why

Python here is once again correct:

  >>> -8 % 5
  2
Re:Integers in C quiz by veltas on Sunday September 04, 2022 @09:11AM (#32712666)
Mathematically speaking if you ask for the modulus of -5 you may as well get a negative number, and I think people may validly have their own interpretations of what's "correct" at this point.
Re:Integers in C quiz by qsort on Sunday September 04, 2022 @09:18AM (#32712727)
I didn't say it's wrong.

You may as well use {white, blue, black, red, green} to represent congruences mod 5, mathematically speaking it's not wrong, as long as they respect the axioms of a field.

What's "wat" (in the same sense as the js "wat" talk) is that the answer changes if the argument becomes negative. By all reasonable definitions of modular arithmetic, -8 and 7 are in the same class mod 5. Why is then:

  > (-8 % 5) == (7 % 5)
  false
in C-like languages? I'm pretty sure it's perfectly consistent with all the relevant language standards, but it's a "wat" nonetheless.
Re:Integers in C quiz by masklinn on Sunday September 04, 2022 @09:28AM (#32712816)
> What's "wat" (in the same sense as the js "wat" talk) is that the answer changes if the argument becomes negative.

It's the same in Python though? you just prefer the way it behaves (though to be fair it's generally more useful and less troublesome): Python's remainder follows the sign of the divisor (because it uses floored division), while C's follows the sign of the dividend (because it uses truncated division).

Strictly speaking, neither is an euclidian modulo.

Re:Integers in C quiz by owl57 on Sunday September 04, 2022 @10:56AM (#32713738)
It's not symmetric. I believe most people with strong mathematical background (like Guido:)) expect lambda x: x % m to be some "computer approximation" to the standard mapping from Z to Z/mZ, while not having deep expectations about lambda m: x % m.
Re:Integers in C quiz by veltas on Sunday September 04, 2022 @09:14AM (#32712694)
The really useful fact of C's remainder operator is that: x == x / y * y + x % y, provided nothing overflowed. This is usually what you want when doing accurate calculations with integer division.
Re:Integers in C quiz by xigoi on Sunday September 04, 2022 @02:06PM (#32715845)
The modulo operation together with floor division also has this property. And unlike C's operators, it also has the useful property that (x + y) % y == x % y.
Re:Integers in C quiz by kccqzy on Monday September 05, 2022 @06:26PM (#32730235)
That's an inherent property of both the modulus interpretation and the remainder interpretation.

Haskell does it right. Instead of having operators like %, it gives the programmer choice to choose either the modulus or the remainder. Using GP's example:

    ghci> 2 `div` (-5)
    -1
    ghci> 2 `mod` (-5)
    -3

    ghci> 2 `quot` (-5)
    0
    ghci> 2 `rem` (-5)
    2
And we have both (x `quot` y)*y + (x `rem` y) == x and (x `div` y)*y + (x `mod` y) == x
Re:Integers in C quiz by cozzyd on Sunday September 04, 2022 @12:52PM (#32715031)
Re:Integers in C quiz by marshallward on Sunday September 04, 2022 @09:52AM (#32713061)
Fortran supports both of these, with `mod` as the C-like truncated modulo and `modulo` as the floored modulo. Having both is convenient, but you do get errors from people who don't realize the difference
Re:Integers in C quiz by mtreis86 on Sunday September 04, 2022 @03:34PM (#32716846)
Common Lisp as well, Rem and Mod