Why We Start Indexing from 0 in Computer Science
cs.utexas.edu
Why We Start Indexing from 0 in Computer Science
1–10 of 38 posts
Re: Why We Start Indexing from 0 in Computer Science
#2Re: Why We Start Indexing from 0 in Computer Science
#3Easier to read: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
Re: Why We Start Indexing from 0 in Computer Science
#4Re: Why We Start Indexing from 0 in Computer Science
#5Easier to read: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
Although this is indeed much easier to read, the handwriting in the original is beautiful. Love the 'f'.
Re: Why We Start Indexing from 0 in Computer Science
#6That is going to make the "the 2nd item is number 1" thing SO much easier to explain when teaching. Thanks! (And it's good to have the rest of the reasoning there to answer the particularly curious students...)
Definitely falls into the "wish I'd thought of that" and "wish I'd heard someone say it that way 5 years ago" camps.
Re: Why We Start Indexing from 0 in Computer Science
#7It'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 there, move that many spaces forward from Start, and give me that". It's handy that if Index is a cleared region of memory, you can say "Start[Index]" and have it mean the same thing as just "Start". More importantly, if "Start[Index]" only meant "Start" when Index was 1, that would imply that if Index were 0 that Start[Index] would refer to the spot right before Start, which is not only useless but outright dangerous.
Re: Why We Start Indexing from 0 in Computer Science
#8Re: Why We Start Indexing from 0 in Computer Science
#9"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 list of valid indices for an array x by using "indices x". I think that's much nicer, and it's also what D is doing: instead of having a(n implicitly paired) starting and ending iterator to indicate a range of positions like the C++ library does, D's library uses a range object. Basically I'm saying you can represent a range of indices as a pair of indices, one pointing to the first valid position and the other pointing past the last valid one, but that's an implementation detail you don't need to expose, make a range abstraction and you'll be happier."
"I should also point out that mathematicians don't number at zero unless there is some advantage (the default is to start at one), but more importantly, the preferred style in mathematical arguments is to avoiding fiddling with indices as much as possible (since it's so easy to mess something up working at such a low level of abstraction)."
Re: Why We Start Indexing from 0 in Computer Science
#10I 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…
But like many features of programming languages and operating systems (case-sensitivity, global menu bar, python's colon...), zero-based indexing is something that made perfect sense in the context and time in which it was originally designed and implemented, but it's really no longer necessary in most cases today, and only survives because of tradition and blind cargo cult-like copying of what others did before.