I found it devastating that there are no distinct agreed-upon words denoting zero- and one-based addressing. Initially I thought that the word "index" clearly denotes zero-base, and for one-base there is "order", "position", "rank" or some other word, but after rather painful and humiliating research I stood corrected. ("Index" is really used in both meanings, and without prior knowledge of the context, there is real…
Numbering should start at zero (1982)
31–40 of 309 posts
Re: Numbering should start at zero (1982)
#32Earlier quoted context omitted.
Not quite seamlessly, unfortunately. `l[:n]` gives you the first `n` elements of the list `l`. Ideally `l[-n:]` would give you the last `n` elements - but that doesn't work when `n` is zero. I believe this is why C# introduced a special "index from end" operator, `^`, so you can refer to the end of the array as `^0`.
So you're saying a negative index value should work like a count of elements to return and not an index? Then you couldn't do thing like l[-4:-2] to get a range of elements which seems slightly useful.
Re: Numbering should start at zero (1982)
#33The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor. It isn't usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a "1" just seems obviously superior. I'm sure there is a culture that counts their first fing…
For math too, 0-based indexing is superior. When taking sub-matrices (blocks), with 1-based indexing you have to deal with + 1 and - 1 terms for the element indices. E.g. the third size-4 block of a 16x16 matrix begins at (3-1)*4+1 in 1-based indexing, at 2*4 in 0-based indexing (where the 2 is naturally the 0-indexed block index). Also, the origin is at 0, not at 1. If you begin at 1, you've already moved some dista…
Besides, in the rare cases where it does matter you're free to pick whichever is convenient.
Re: Numbering should start at zero (1982)
#341 or 0-based index... I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try. I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.
I have a similar experience with pythons negative-indexing. In Python, you can access elements counting from the back by using negative numbers. But for this, they start with 1, not 0. Which is inconsistent, as they start for the normal forward indexing at 0. I guess it comes from reducing n.length-1 to -1, but it's still kinda annoying to have two different indexing-systems at work.
Re: Numbering should start at zero (1982)
#35Re: Numbering should start at zero (1982)
#36Earlier quoted context omitted.
Zero-based counting works better with modular arithmetic. Like arr[(i++ % arr.length)] = foo; Is certainly nicer than the equivalent in one-based subscripting arr[(i++ % arr.length) + 1] = foo; (The above is actually wrong, which helps the idea) I'll concede that it's not all that significant as a difference, but at least IMO it's nicer. Also could argue that modular arithmetic and zero-based indexing makes more sens…
Yes, negative indexing as in e.g. Python (so basically "from the end") can be incredibly convenient and works seamlessly when indexes are 0-based.
I'd claim 0-based indexing actually throws an annoying wrench in that. Consider for instance:
for n in [3, 2, 1, 0]:
start_window = arr[n: n+5]
end_window = arr[-n-5: -n]
The start_window indexing works fine, but end_window fails when n=0 because -0 is just 0, the start of the array, instead of the end. We're effectively missing one "fence-post". It'd work perfectly fine with MatLab-style (1-based, inclusive ranges) indexing.Re: Numbering should start at zero (1982)
#37The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor. It isn't usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a "1" just seems obviously superior. I'm sure there is a culture that counts their first fing…
0-based indexing aligns better with how memory actually works, and is therefore more performant, all things being equal. Assuming `a` is the address of the beginning of the array, the 0-based indexing on the left is equivalent to the memory access on the right (I'm using C syntax here): a[0] == *(a + 0) a[1] == *(a + 1) a[2] == *(a + 2) ... a[i] == *(a + i) For 1-based indexing: a[1] == *(a + 1 - 1) a[2] == *(a + 2 -…
> but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.
Re: Numbering should start at zero (1982)
#38Re: Numbering should start at zero (1982)
#39Earlier quoted context omitted.
I have a similar experience with pythons negative-indexing. In Python, you can access elements counting from the back by using negative numbers. But for this, they start with 1, not 0. Which is inconsistent, as they start for the normal forward indexing at 0. I guess it comes from reducing n.length-1 to -1, but it's still kinda annoying to have two different indexing-systems at work.
It makes more sense if you think of indices as pointing between elements: 0 1 2 3 4 ----------------- | A | B | C | D | ----------------- -4 -3 -2 -1 -0 Except, of course, -0 doesn't exist. AFAIK that's why C# chose to add a special "index from end" operator, `^`, instead of using negative indices.
Not for integers on modern hardware. If only hardware used ones’ complement (https://en.wikipedia.org/wiki/Ones'_complement)… ;-)
Meanwhile, the workaround is to use -1 through -5 to index from the end of the array.
Re: Numbering should start at zero (1982)
#40I have Ar apples for sale.
Only $A.J each!