Live data from Hacker News

Numbering should start at zero (1982)

cs.utexas.edu

21–30 of 309 posts

Re: Numbering should start at zero (1982)

#21
post #3

The 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…

What culture would that be? Because I was under the impression that counting "nothing" generally makes little sense in most of the practical world.

Re: Numbering should start at zero (1982)

#22
post #8
post #6

Earlier 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.

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`.

Re: Numbering should start at zero (1982)

#23
post #3

The 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 - 1)
  a[3] == *(a + 3 - 1)
  ...
  a[i] == *(a + i - 1)
This extra "-1" costs some performance (through it can be optimized-away in some cases).

Re: Numbering should start at zero (1982)

#24
post #11

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…

I always thought:

- Offset: 0-based

- Index: 1-based

Re: Numbering should start at zero (1982)

#25
post #11

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…

AFAIK "offset" (i.e. from the beginning of the array/file/etc) is commonly used to indicate a zero-based index.

Re: Numbering should start at zero (1982)

#26

1 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 don't use Lua as much anymore, but there were a few years where I used Lua and C++ both daily and very quickly you can easily handle both zero and one-indexing, even while switching between languages frequently. As with most things it's just practice.

Re: Numbering should start at zero (1982)

#27

1 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.

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.

Re: Numbering should start at zero (1982)

#28
post #3

The 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 distance away from the origin at the start.

Re: Numbering should start at zero (1982)

#29

1 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.

> decades of programming languages choosing 0-based index is hard to ignore

Yes - many file formats also work with zero-based indices, not to mention the hardware itself.

One-based indexing is particularly problematic in Lua, because the language is designed to interoperate closely with C - so you're frequently switching between indexing schemes.

Re: Numbering should start at zero (1982)

#30
post #22
post #8

Earlier quoted context omitted.

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.

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.

Post reply on HN