Live data from Hacker News

Why We Start Indexing from 0 in Computer Science

cs.utexas.edu

21–30 of 38 posts

Re: Why We Start Indexing from 0 in Computer Science

#21
post #9

I'll repeat my comment from a different thread: "I've never really agreed that numbering should start at zero (or at one), I think people use indices way too much and it gets in the way of clarity. I much prefer whole array or list operations with no fiddling with indices and off by one errors. I like Haskell's array API, for example. You can index by whatever is natural in each case and you can always get the whole…

> I should also point out that mathematicians don't number at zero unless there is some advantage (the default is to start at one)

I disagree. While this may depend a little on which areas of mathematics you study, I've found that in situations where there is a reasonably clear / natural / non-arbtirary preference, it tends to be for zero-based natural numbers.

On the other hand, situations in which 1-based numbers are used, tend to look more like arbitrary aesthetic preferences. They tend to be situations where no particular `origin' is any better than any other, and one could (despite the ugliness) use 2- or 3-based numbering without adding any additional corner-cases.

My personal favourite `reason' that the set of natural numbers should include zero (and that numbering should start at zero) comes from set theory, where cardinal numbers are equivalence classes of sets of the same size. 0 is the smallest such number corresponding to the empty set. (For numbering sequences, 0 is also naturally the smallest ordinal number).

These kinds of reasons tend to crop up in category theory and other foundational topics too, which are some of the areas of mathematics closest to theoretical computer science.

Interested in counter-examples though; I'm sure at least some exist.

Re: Why We Start Indexing from 0 in Computer Science

#22

2..13 2:13 2->13 Again we fall in the trap of endless discussions about bitheads, numheads and charheads. The bithead will always think in zeros and ones, he is a hardcore c or c++ programmer, and his brain is damaged beyond repair. Avoid all discussions with that kind of specimen. The numhead accepts both conventions, he is a python or ruby programmer, practical and gets the job done, he may ask why that weird conve…

> I've been in the three camps luckily escaping without any

I'm not convinced that people count starting at one.

Count the number of elephants in your bedroom. Assuming that there weren't any, how can you say that you started counting at 1 when the result of said counting was 0?

Re: Why We Start Indexing from 0 in Computer Science

#23
post #15

Lua, the oh so nice "rebel" among languages, uses 1 as the first element in its indices :)

Nasty. But it's the same for substring text offsets, array indices etc in Postgres.

Nasty? In a language without pointers it makes total sense. An index is no longer an offset from an array (or string) pointer, so the first element being 1, the second being 2, just makes more sense.

Sure, it's a departure from mainstream languages, but in my opinion it's a perfectly sensible one.

Re: Why We Start Indexing from 0 in Computer Science

#24
post #22

2..13 2:13 2->13 Again we fall in the trap of endless discussions about bitheads, numheads and charheads. The bithead will always think in zeros and ones, he is a hardcore c or c++ programmer, and his brain is damaged beyond repair. Avoid all discussions with that kind of specimen. The numhead accepts both conventions, he is a python or ruby programmer, practical and gets the job done, he may ask why that weird conve…

> I've been in the three camps luckily escaping without any I'm not convinced that people count starting at one. Count the number of elephants in your bedroom. Assuming that there weren't any, how can you say that you started counting at 1 when the result of said counting was 0?

But do I call the first elephant I get into my room elephant 0 or elephant 1?

Re: Why We Start Indexing from 0 in Computer Science

#25
post #7

I suspect the real reason has more to do with the pragmatics of machine code. It's extremely common to want to refer to some dynamic offset of a fixed location in memory, like if you have an array of equal-length items stored consecutively. If you have two literal addresses called Start and Index, you'd like to be able to say something like "Start[Index]" and have it mean "Read from Index, and whatever number is ther…

Right, it makes sense when dealing with machine code and when making the compiler do extra work for you (converting one-based to zero-based indexing) would be too much work for the compiler implementer or is too slow (like on machines back in the 70s and 80s) or it would abstract too far from what is happening underneath the hood, possibly leading to errors. But like many features of programming languages and operati…

Care to explain why Dijkstra arguments for zero-indexing hold any less true today than in 1982? His arguments make a great deal of sense to me so I take exception to the assertion of cargo cultism.

Re: Why We Start Indexing from 0 in Computer Science

#26
Matlab, which is commonly used for numerical computing, uses one-based indexes. Just one of the little gotchas when switching between Matlab and "real" programming languages.

In mathematics and physics the usage varies. Zero is used when it is really the beginning of something, like t0 for the start time of an experiment. In most other cases lists count from one and up. Matrix elements too are generally denoted from (1,1) to (n,m).

Re: Why We Start Indexing from 0 in Computer Science

#27
Just to pile on: 0-based indexing is also more convenient when you want to build a multi-dimensional array from a one-dimensional array primitive using the integer division and modulo operators to map between 1D and nD indices. E.g., a matrix with M rows and N columns can be mapped to the range 0 If instead you use 1 <= i <= (NxM), 1 <= row <= M, 1 <= col <= N, then the mappings aren't nearly as clean. Sure, you could hide all this behind some kind of API, eg. Image.getPixel(x,y), but if you work with multidimensional arrays very often, eventually you'll need to marshal arrays back and forth between different containers that use different access APIs, and the simplest common representation to use for this kind of data is as a 1D array, so being able to work with nD data that is stored in a 1D array comes up fairly often.

Re: Why We Start Indexing from 0 in Computer Science

#30
post #21
post #9

I'll repeat my comment from a different thread: "I've never really agreed that numbering should start at zero (or at one), I think people use indices way too much and it gets in the way of clarity. I much prefer whole array or list operations with no fiddling with indices and off by one errors. I like Haskell's array API, for example. You can index by whatever is natural in each case and you can always get the whole…

> I should also point out that mathematicians don't number at zero unless there is some advantage (the default is to start at one) I disagree. While this may depend a little on which areas of mathematics you study, I've found that in situations where there is a reasonably clear / natural / non-arbtirary preference, it tends to be for zero-based natural numbers. On the other hand, situations in which 1-based numbers a…

Well, whole numbers come up in mathematics in different ways. Of course if you're talking about cardinality you should include zero: it simply is the cardinality of some set, you can't avoid that.

I was talking about a completely different use of numbers: numbering, that is assigning numbers as labels to things. There I think mathematicians on the whole prefer to number starting at 1 (or not to number at all and work with abstract indexing sets). Sometimes it is convenient to number starting at zero if it simplifies some formulas, but usually it doesn't matter.

Post reply on HN