News for hackers. Stuff that matters.
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by jacobolus on Wednesday June 22, 2011 @02:08AM (#2681957)
In a quite similar example of a bad convention, I find it extremely frustrating that many programming languages do not have a modulus operator (%) which behaves as modulo in mathematics. To be explicit, there are multiple choices of the sign of:

  x % y
When either x or y is negative, or both (the full details are described at Wikipedia: http://en.wikipedia.org/wiki/Modulo_operation). In common mathematical notation x mod y, the result always shares the sign of the divisor y. For example,

–14 mod 10 = 6

6 mod –10 = –4

The same is the case in python:

  >>> -14 % 10
  6
  >>> 6 % -10
  -4
Unfortunately, in JavaScript (and various other languages), a different convention is chosen. The result instead shares the sign of the dividend:

  > -14 % 10
  -4
  > 6 % -10
  6
This is really annoying because we quite frequently want to use the modulus operator to do things like normalize an angle to the range [0, 360°). To perform this normalization on an angle a in JavaScript, if a < 0, requires a computation like:

  a_prime = a % 360 + 360
I end up usually defining a function like:

  var mod = function (x, y) {
    var js_mod;
    return (js_mod = x % y) && (x > 0 ^ y > 0) ? js_mod + y : js_mod;
  }
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by chalst on Wednesday June 22, 2011 @02:55AM (#2682057)
Right.

A convenient property to have that is held by the Python version is (for nonzero n)

        x==n*floor(x/n)+x%n
This property isn't held by the Javascript variant, where there is a somewhat more complex property

        x==n*sign(x/n)*floor(abs(x/n))+x%n
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by jacobolus on Wednesday June 22, 2011 @03:12AM (#2682098)
In python, math.floor(x / n) can be written x // n, so:

  x == n * (x // n) + x % n
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by dagw on Wednesday June 22, 2011 @04:22AM (#2682249)
math.floor(x / n) can be written x // n

It's worth noting that x//n returns an int and math.floor returns a float, so the statements aren't equivalent.

Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by jacobolus on Wednesday June 22, 2011 @12:49PM (#2683939)
If you pass a float in as x or n, x // n will be a float.
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by qntm on Wednesday June 22, 2011 @07:44AM (#2682664)
The worst part is, if you are a mathematician and you expect the modulus operator to behave exactly as it does in mathematics then it can be maddeningly difficult to track down why your program isn't working correctly. You just don't expect simple mathematical operators to start malfunctioning.
Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by ScottBurson on Wednesday June 22, 2011 @02:29AM (#2682005)
Agreed. A closely related infelicity is that integer division in most languages (and indeed, on most CPUs that have an integer divide instruction) truncates toward zero. So 1 / 2 and -1 / 2 both evaluate to zero.

The `floor' operation -- that truncates toward negative infinity -- is a more useful primitive, and goes hand in hand with the modulo operation defined as you describe.

Re:Edsger Dijkstra's note on starting array indices at 0 (pd... by lelele on Wednesday June 22, 2011 @02:29PM (#2684430)
Common Lisp, of course, gets modulus right:

(mod -1 5) => 4 (mod 13 4) => 1 (mod -13 4) => 3 (mod 13 -4) => -3