Skip to content

Instantly share code, notes, and snippets.

@wmayner
Created July 31, 2014 17:19
Show Gist options
  • Save wmayner/66ba5bbd5fd9c7d49c26 to your computer and use it in GitHub Desktop.
Save wmayner/66ba5bbd5fd9c7d49c26 to your computer and use it in GitHub Desktop.
Another reason why numbering should start at zero

Why numbering should start at zero

Elaborating on the reasons given in Dijkstra's memo on the topic, we observe the following:

1-based indexing

  • Ranges are of the form 1 ≤ i < N + 1.
  • The last element of a sequence has index N.
  • How do we represent the index of the last element if N is not known? We need a new symbol!
  • There is no obvious choice for this symbol, so we choose something like end (Matlab). Ugly.

0-based indexing

  • Ranges are of the form 0 ≤ i < N.
  • The last element of a sequence has index N – 1.
  • How do we represent the index of the last element if N is not known? We need a new symbol!
  • But now there is an obvious choice: -1. It comes directly from the expression for the index of the last element, N-1.
  • And, we can now count backwards using only the usual numerical symbols, so that the index of the second-to-last element is -2, etc., instead of something ugly like end-1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment