Numbering Should Start at Zero
31–40 of 58 posts
Re: Numbering Should Start at Zero
#32If 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.
Think of a basket of apples. If you take the zeroth apple (empty basket), you have no apples. If you take the first apple, you have 1 apple and now the basket is empty again (0).
One also wouldn't have to do [length - 1] to access the last index; it would naturally be [length].
Re: Numbering Should Start at Zero
#33Dijkstra have a certain way of writing as if his argument follows by logical necessity. But when you dig into the chain of reasoning, it hinges on the assertion that starting with 0 is "nicer". Which is of course a valid opinion, but starting with 1 also have nice properties, for example that the numbering of elements corresponds to the ordinal numbers. Having the first element be element 1 is pretty nice IMHO. He en…
Re: Numbering Should Start at Zero
#34> Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i "A nicer range." This is just Dijkstra's preference.
Interestingly, he links ordinals (the subscript) to cardinals (how many numbers there are before the element).
The off by one is just as likely in both schemes with half open intervals.
The nice property of the length of the sequence falling "for free" from the indices is not essential if you track the length separately. While it was important in the age of machines with severely constrained memories, the downsides outweigh the merits.
Re: Numbering Should Start at Zero
#35I 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.
Re: Numbering Should Start at Zero
#36C uses pointer offsets. Base pointer + offset = destination address. C "array" syntax is sugar for this addition. There are no array indices in C, just pointer offsets. Thus, they start at 0.
Some languages have arrays that aren't just a pointer to the first element. In those, indices are a better option. There, starting at 1 usually makes more sense.
The first number used should match the use of that number.
Re: Numbering Should Start at Zero
#37Once 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…
Another common example of this is circular buffers. Computing the bounded index from an arbitrary index is simply i % c with zero based indexing, but becomes (i-1) % c + 1 with one based indexing.
Re: Numbering Should Start at Zero
#38Earlier quoted context omitted.
first, second and so on
So the 'first' item in the list is actually the second? The problem is you're trying to redefine English (and probably most other languages). The first item on a menu is, well the first item, like first place in a marathon, the first day of the month. Surely a definition of the zeroth item would be something like an item that does not exist, the item that's left when you take away all the other items, etc. Ordinals a…
For example christmas is the zeroth day after christmas.
A common advice with regards to user inputs is that if you do not do ordering or arithmetic on a piece of data (eg a phone numbers) then it should be a string even if it is numeric.
Similarly n-indexed conventions should be considered in terms of practical pros and cons.
Linguistic similarity is not a convincing argument to me.
Re: Numbering Should Start at Zero
#39Earlier quoted context omitted.
So the 'first' item in the list is actually the second? The problem is you're trying to redefine English (and probably most other languages). The first item on a menu is, well the first item, like first place in a marathon, the first day of the month. Surely a definition of the zeroth item would be something like an item that does not exist, the item that's left when you take away all the other items, etc. Ordinals a…
zeroth is the ordinal associated with the cardinal zero. For example christmas is the zeroth day after christmas. A common advice with regards to user inputs is that if you do not do ordering or arithmetic on a piece of data (eg a phone numbers) then it should be a string even if it is numeric. Similarly n-indexed conventions should be considered in terms of practical pros and cons. Linguistic similarity is not a con…
I agree indexing is a trade-off, sometimes 0 is best, sometimes 1 makes more sense.
But it's not linguistic 'similarity'. You have to name things. If your names are off-by-one (the element named 'first' is actually the second) you're just sowing confusion.
Re: Numbering Should Start at Zero
#40Once 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…
In fact the vast majority of algorithms which involve doing computation on indexes beyond increment/decrement are naturally implemented with zero-based indexing. When using ones based indexing you end up having to convert to zero based, do the computation, then convert back to one based. Another common example of this is circular buffers. Computing the bounded index from an arbitrary index is simply i % c with zero b…
Arguably, this is because the common definition of modular arithmetic is itself zero-based: “modulo N” maps all integers into the numbers [0,N-1]. It is fully possible to define a “%” operator that instead mapped the integers into the range [1,N], which might be more natural in 1-based languages?