News for hackers. Stuff that matters.
Re:Moving from Ruby to Rust by xiphias2 on Thursday February 14, 2019 @06:52PM (#19167234)
Actually what's funny here is that (index - 1) % N doesn't work in C, as it can be negative. You have to use (index+N-1) % N.

I still prefer 0 based indexing (for example for calculating the size of an array), but the worst thing is inconsistency between languages. 0 won, that's it.

Re:Moving from Ruby to Rust by bsder on Thursday February 14, 2019 @07:16PM (#19167383)
Oh, that's hilarious. I tested this in Python where it worked fine and forgot that C has a "broken" modulo operator:

Python:

  >>> -1 % 20
  19
C:

  printf("%d\n", (-1 % 20));
  -1
Even worse, it still fails even on unsigned:

  printf("%d\n", ((unsigned int)0 - (unsigned int)1) % 20);
  15
So you still need to add N:

  printf("%d\n", ((unsigned int)0 - (unsigned int)1 + 20) % 20);
  19
Thanks for the reminder of humility.

(Side Note: For those reading this, the C operator isn't "broken", per se. There are three properties that modulo can adhere to but two of the three are mutually exclusive.)

Re:Moving from Ruby to Rust by m-ou-se on Friday February 15, 2019 @02:19AM (#19169094)
C does not have a modulo operator. It has a remainder operator, which works perfectly fine. Calling C's `%` a 'broken modulo operator' is like calling `+` a 'broken minus operator'. Quite a few languages have separate operators, keywords, or functions for 'modulo' and 'remainder'.