r/math • u/thegenius2000 • Dec 15 '16
E.W. Dijkstra: Why numbering should start at zero (EWD 831)
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html13
u/Bromskloss Dec 15 '16
Note how, in the PDF version, Dijkstra has numbered (in handwriting) the pages starting on zero, but someone else has added numbers (printed) starting on one.
5
0
Dec 16 '16
[deleted]
5
u/Bromskloss Dec 16 '16 edited Dec 16 '16
trying to prove that one is better than the other is probably misplaced effort.
Still, one has to choose, and therefore wants to figure out which option is slightly better, even if it means bringing out the microscope to find a difference.
I do think it matters in programming, though. Choosing every N:th element from a zero-indexed array (starting on the first one) means choosing all indices divisible by N (0, N, 2_N_, …), whereas with one-indexing (1, _N_+1, 2_N_+1, …), that nice relationship isn't there.
As a slightly more complicated example, which describes something one often has to do to find the memory address to some value in a data structure, imagine having N arrays with M elements each. These arrays are concatenated to form one big, "collective" array. If everything (the individual arrays, their elements, and the elements of the collective array) are indexed from zero, it is easy to find the position in the collective array where element m from array n ends up: n__M_+_m. Indexing from one makes it more complicated: (n-1)(M-1)+_m_ (and expanding the expression as n__M-M-n_+1+_m does not help).
PS: Another example. In Matlab, typing
help fftgives you this formula for the discrete Fourier transform and its inverse:N X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N n=1 N x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N k=1Had it instead used zero-based indexing and Dijkstras notation for intervals (including for the summation limits), it would have looked like this, which I think more clearly shows what the transform means, as the argument to the exponential functions become simply a product of the different quantities, with no shifting by one:
N X(k) = sum x(n)*exp(-j*2*pi*k*n/N), 0 <= k < N n=0 N x(n) = (1/N) sum X(k)*exp( j*2*pi*k*n/N), 0 <= n < N k=0
21
u/chebushka Dec 15 '16
The "reasoning" that a list should end with a strict inequality < is not persuasive to me, so the rest of his argument is not compelling either. If you want to list the first N positive integers, then it is natural to start with 1. If you want to write down a prime factorization it is natural to write p*1^(e1)...pk^(ek), with the indexing starting at 1. If you want to write down a power series, it is natural to start the summation with the constant term indexed by 0. If you want to write down homogeneous coordinates of a point in projective n-space, then it is natural to start with 0 (that is, [x0,x1,...,xn*]). Everything flows from the actual context you are in; I don't see any over-arching advantage to starting numbering at 0 as some kind of default convention.