r/math • • Dec 15 '16

E.W. Dijkstra: Why numbering should start at zero (EWD 831)

https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
38 Upvotes

29 comments sorted by

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.

9

u/thegenius2000 Dec 15 '16

I think the argument is more compelling for applied mathematicians and computer scientists than for pure mathematicians.

9

u/skullturf Dec 15 '16 edited Dec 15 '16

Yeah, I've always interpreted this as an argument primarily about programming languages, in which case I think Dijkstra has an excellent point.

Even in pure mathematics, there are many situations where numbering starting at 0 is best. (Admittedly, I'm sure there are many other natural situations where starting at 1 is best.)

In my thesis, I studied n-tuples whose entries are +1 and -1. I also studied their generating functions, which are polynomials. Those polynomials have a constant term, so I started numbering at 0.

For me, an n-tuple was (a*0, a1, ..., an-1*) and the corresponding polynomial was

a*0* + a*1z + a2z2 + ... + an-1* zn-1

which I called a polynomial of "length" n.

4

u/mmmmmmmike PDE Dec 15 '16

IIRC some people say that it makes more sense in the context of infinite ordinals. Something like how when you get to (|||...)|, you have to call the last object ω+1 if you're being consistent with starting 1,2, ... , since there are ω+1 objects. Nothing gets called object number ω, and similarly no other limit ordinal is assigned to an actual object. Whereas starting out 0,1,2, ... you can say the last object of (|||...)| is number ω, etc., and there's a correspondence between ordinal numbers of lesser cardinality than your set and objects of that set. You just have to remember that to state the cardinality of the set, you take the successor or limit operation.

3

u/SpeakKindly Combinatorics Dec 15 '16

For mathematicians, inconsistent notation is not a problem. I can start at 0 in one paper and start at 1 in the next. When deciding how a programming language should work, you're making a decision for everyone that uses it.

It still matters in pure mathematics which notation you use: some notation will be easier to understand than others. But the preferences can be more nuanced.

12

u/Bromskloss Dec 15 '16

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 p1e1...pkek, with the indexing starting at 1.

Is it? I'm not sure I think so.

I don't see any over-arching advantage to starting numbering at 0 as some kind of default convention.

How about seeing the act of numbering as creating a function from the set of natural numbers to the set of objects you're numbering? What you start on then comes down to how you define the set of natural numbers.

1

u/chebushka Dec 15 '16 edited Dec 15 '16

Yes, it is natural to write prime factorizations the way I did. Why would you not think so? Have you found number theory books (or any math books) that write prime factorizations starting with p*0^(e0*) for the first factor?

9

u/Bromskloss Dec 15 '16

Yes, it is natural to write prime factorizations the way I did. Why would you not think so?

To me, it's just like numbering any other things, which I think is suitably done starting on zero. What's special about prime factors?

Have you found number theory books (or any math books) that write prime factorizations starting with p0e*0 for the first factor?

I don't remember, but what does that have to do with it?

3

u/[deleted] Dec 15 '16 edited Dec 15 '16

Using 0 as index implies that that factor is somehow different to the others. It makes more sense that a number's n*th* smallest unique prime factor is the factor p*_n_en*. It makes sense that a number with n unique prime factors has largest factor p*_n_en*.

Also what do you mean by "but what does that have to do with it?". It seems like you're playing dumb when he provided evidence that starting with 1 is the preferred convention.

5

u/Bromskloss Dec 15 '16 edited Dec 15 '16

If that's the argument, couldn't the same thing be said about all numbering?

3

u/chebushka Dec 15 '16

Yes, exactly! There are common conventions in different areas, and just because programmers find it useful to start counting at 0 doesn't mean this should be done throughout math.

I already pointed out in my original answer that there are situations in math where a count starting at 0 is very convenient (sums for a polynomial starting in degree 0 and homogeneous coordinates in projective n-space start with x*0* so that the last coordinate in this n-dim. space literally has index n), but for most purposes in math there is not a compelling reason to count starting from 0 and people are used to the other way. If you have a finite group G with n elements, it is standard to list the elements as g*1, ..., gn* unless the index has a special meaning that would suggest counting in a different way (e.g., maybe it is useful to regard the indices as elements of Z/(n), so starting from the identity would suggest starting with g*0*).

1

u/[deleted] Dec 15 '16

Could I say that it's wrong to imply that an element is different to others by giving it the only non-positive index? No, because in some cases the element is actually different. My 2nd point is pretty universal but in some cases it is trumped by more important advantages of using 0.

8

u/SpeakKindly Combinatorics Dec 15 '16 edited Mar 13 '17

If you're making a list by writing down A*min, Amin+1, ..., Amax*, then you're forced to use the interval form min ≤ i ≤ max, and then you should probably start at 1, yes.

If you're using fancier notation that has an index variable ranging over some range - for example, if you're writing down a prime factorization as [; \prod_{ ?? } p_i^{e_i} ;] - then we have options. We want a variable k to tell us how many prime numbers we have, so we should use either 1≤i≤k or 0≤i<k to bound i.

In this case, it probably doesn't matter, because we won't need to work much with this product after writing it down. But there are plenty of reasons to prefer 0≤i<k and a≤i<b when dealing with sums and products:

  • As Dijkstra mentions, using a≤i<b makes it easy to tell you're working with b-a elements. (Or, more precisely, with (b-a)+ = max{b-a,0} elements.)

  • Also as Dijkstra mentions, it's easy to write down an empty sum: a≤i<a. The other method requires you to use a≤i≤a-1, which is awkward. Empty sums are important things when looking at base cases, we want them to be nice.

  • It's easy to break a sum over a≤i<b into two sums over a≤i<c and over c≤i<b.

  • This makes the summation operator the inverse of the forward difference operator in a natural way: if g(x) = f(x+1)-f(x), then the sum of g(i) over a≤i<b is f(b)-f(a).

  • If you start doing anything more complicated with finite difference methods, the a≤i<b interval is the only thing that will save you from drowning in +1s and -1s.

Also, you explicitly use the a≤i<b approach when your upper bound is infinity.

5

u/ice109 Dec 15 '16

Being able to subtract the lower bound from the upper bound and get the length of the sequence is very useful. Everything follows from that.

3

u/chebushka Dec 15 '16 edited Dec 15 '16

Most of the time when I write down a sequence of n terms, it's easy to count because I list the terms as x*1,...,xn*.

Even if you did start the indexing at 0 in a list x*0, x1,...,xn, if you need to know the number of terms in the list from xi* through x*j, you still need to use the formula j-(i-1) = j-i+1, so the "magic" of starting at 0 doesn't help. Perhaps you would say this intermediate list of terms should be described as running over xk* for i <= k < j+1 (hence the count is j+1-i = j-i+1), but I do not find this way of describing lists to be useful. I like saying where I actually start and where I actually end (not one term past where I end -- and how would that work anyway for a finite list when you reach the end?).

2

u/ice109 Dec 15 '16

you have it backwards because you think listing entire ranges if primary when it's actually secondary. the intermediate list is [i,j) and the length of the list is j-i - counting from zero has nothing to do with this (only open upperbound). counting from zero comes from abiding by [i,j) and not wanting a trailing +1 to include all of the elements in a list.

1

u/chebushka Dec 15 '16

I already said that I understood that intermediate lists with a strict inequality for the upper index was what you had in mind, but these conventions are each useful for a particular range of applications. For me personally, listing entire ranges is primary; not for you, but for me.

1

u/ice109 Dec 15 '16

are you a mathematician or a programmer?

1

u/chebushka Dec 15 '16

I think it's clear. :)

1

u/LeepySham Dec 16 '16

To be honest, I don't think Dijkstra's statement even applies to this case. Numbering x*1,...,xn* and x*0,...,xn-1* are both examples of closed intervals, [1,n] and [0,n-1], which were dismissed at the beginning. An example with a right open interval would be like:

The sum from i = 0 to <n of x*i*

as opposed to

The sum from i = 0 to n-1 of x*i*.

In other words, it's for iterating over a sequence, rather than listing an entire sequence explicitly. This is why the paper is more intended for programming languages, where the former is more common.

Dijkstra's only reason for dismissing this:

The sum from i = 1 to <n+1 of x*i*

is the ugly "n+1". We can apply that exact same argument to prefer x*1,...,xn* over x*0,...,xn-1*.

0

u/[deleted] Dec 16 '16

[deleted]

3

u/chebushka Dec 16 '16

Of course there is no logical problem with doing that. And we could start the indexing in the prime factorization at -2 or call a 4-tuple of numbers (a,x,ю,y*9*). But people who work with prime factorizations will find it strange. When in Rome...

8

u/[deleted] Dec 15 '16

[removed] — view removed comment

4

u/jacobolus Dec 15 '16 edited Dec 15 '16

Exactly right.

Elsewhere in this thread: mathematicians who lack relevant experience – i.e. don’t ever have to work with systems that are strict about bounds checking or fail catastrophically when they hit an off-by-one error, and haven’t ever bothered trying to index all their summations from 0.

It’s like listening to physicists complain that mathematicians won’t accept sin x = x as a valid simplification even when we know x < 0.5 radians or won’t take “it’s true for every integer up to 10 million” as a “proof”.

13

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

u/[deleted] Dec 15 '16

I know it's been discussed before but his handwriting is a work of art.

5

u/Bromskloss Dec 15 '16 edited Dec 15 '16

As a font. (Click on "Fonts".)

2

u/[deleted] Dec 15 '16

Awesome!

0

u/[deleted] 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 fft gives 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=1

Had 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