Live data from Hacker News

Numbering Should Start at Zero

cs.utexas.edu

11–20 of 58 posts

Re: Numbering Should Start at Zero

#11

He’s not wrong, but ask any normal person “What’s the first thing on this list” and see what happens…

This reminds me of when I used zeroeth, oneth, twoth, and threeth, instead of first, second, and third, for referring to indices 0, 1, 2, and 3 as a TA. It's less verbose than “at index zero”, but feels a little clumsy.

Re: Numbering Should Start at Zero

#12

Once you flatten multidimensional arrays the folly of starting at 1 becomes apparent (much like mathematics got so much simpler when we switched to logarithms with the "obvious" log a + log b = log ab which was a breakthrough at the time). If you have a two dimensional array A (1..N X 1..M), then flattening it becomes A[i][j] = flat[i + (j-1) * N] Continuing to higher dimensions it only gets uglier. 0-based arrays ar…

> If you have a two dimensional array A (1..N X 1..M), then flattening it becomes A[i][j] = flat[i + (j-1) * N]

> When flattening A (0..N-1 X 0..M-1) A[i][j] becomes flat[i + N * j].

To play the devil’s advocate a bit: Both cases above has the same number of “-1” offsets; the question is whether it is in the indexing or in the bounds. In the 0-indexed case, i goes from 0 to N-1 instead of from 1 to N, and this happens for every array dimension.

Re: Numbering Should Start at Zero

#14

If I have some apples on a table and pick single apple up, I don’t have zero apples in my hand. I think that’s why numbering doesn’t start at zero.

Hmm but this example only makes me think about how 0 is useful. Otherwise I wouldn't be able to describe your hand prior to picking up the apple.

Re: Numbering Should Start at Zero

#15

If I have some apples on a table and pick single apple up, I don’t have zero apples in my hand. I think that’s why numbering doesn’t start at zero.

You're describing counting, not numbering. Like how an array with a count of 1 has one item that is numbered by it's index, 0.

Re: Numbering Should Start at Zero

#16

Once you flatten multidimensional arrays the folly of starting at 1 becomes apparent (much like mathematics got so much simpler when we switched to logarithms with the "obvious" log a + log b = log ab which was a breakthrough at the time). If you have a two dimensional array A (1..N X 1..M), then flattening it becomes A[i][j] = flat[i + (j-1) * N] Continuing to higher dimensions it only gets uglier. 0-based arrays ar…

Or we can make programming languages that do not require flattening by hand. Off-by-ones are easy to make in both systems. Also it’s annoying that standard libraries do not include functions like n_ints_inclusive(from, to), range_of_last_n_or_less(arr_size, n), n_elems_before(range, n), index_after_range(range), flatten2(i, j, stride) and so on. As if PL designers wanted us to draw ranges on paper and visually check our reasoning every time we have to deal with indices.

Re: Numbering Should Start at Zero

#17
post #6

I always found array index confusing. For example, given a array with size N in Python, we can iterate it from 0 to N-1 (onwards) and from -1 to -N (backwards), which is not consistent at all. Programming language is meant for human eyes. It would be better if array index being 1 to N, and let the compiler substract that 1 for us.

This is surely an inconsistency with Python, not with array indexing at large.

Re: Numbering Should Start at Zero

#18
post #6

I always found array index confusing. For example, given a array with size N in Python, we can iterate it from 0 to N-1 (onwards) and from -1 to -N (backwards), which is not consistent at all. Programming language is meant for human eyes. It would be better if array index being 1 to N, and let the compiler substract that 1 for us.

0 = -N mod N and N-1 = -1 mod N

The consistency is that if i < 0 and l[i] exists then l[i] = l[i+N]

Re: Numbering Should Start at Zero

#19

He’s not wrong, but ask any normal person “What’s the first thing on this list” and see what happens…

This reminds me of when I used zeroeth, oneth, twoth, and threeth, instead of first, second, and third, for referring to indices 0, 1, 2, and 3 as a TA. It's less verbose than “at index zero”, but feels a little clumsy.

Zeroth is an actual word, I heard it many times in conversation between methematicians.

Re: Numbering Should Start at Zero

#20
So the first item in my array of 13 items is at position '0'. The 3rd item is at position '2'.

And if I want to count the items in my array I start at '0' and keep going until I get to '12'. Even though there are 13 items in my array?

I see.

zero-indexing is good for system-level stuff, for dealing with actual memory and so on, but I'm still not convinced it's the right way. It's certainly not the one true way, 1-based indexing is completely valid for some use-cases like maths.

Either way, there will be off-by-one difficulties, either in ordinals or count or whatever, so it's a trade-off.

Post reply on HN