News for hackers. Stuff that matters.
Re:Numbering should start at zero: E.W. Dijkstra by roenxi on Friday March 21, 2025 @07:52AM (#43434542)
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 finger as 0 and I expect they're mocked mercilessly for it by all their neighbours. I've been programming for too long to appreciate it myself, 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: E.W. Dijkstra by sham1 on Friday March 21, 2025 @08:17AM (#43434737)
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 sense for negative indexing.

Re:Numbering should start at zero: E.W. Dijkstra by tmtvl on Friday March 21, 2025 @08:38AM (#43434919)
But on the other hand,

  last := vector[vector-length(vector)];
is nicer than

  last := vector[vector-length(vector) - 1];
so in the end I'd say 'de gustibus non est disputandum' and people who prefer 0-based indexing can use most languages and I can dream of my own.
Re:Numbering should start at zero: E.W. Dijkstra by naasking on Friday March 21, 2025 @12:49PM (#43437992)
That's arguably one of the only downsides of zero-based, and can be handled easily with negative indexing. Basically all indexing arithmetic is easier with zero-based.
Re:Numbering should start at zero: E.W. Dijkstra by tmtvl on Friday March 21, 2025 @01:10PM (#43438291)
Using an array as a heap is also easier with 1-based indexing:

  base-element := some-vector[i];
  left-child := some-vector[i * 2];
  right-child := some-vector[i * 2 + 1];
where the root element is `some-vector[1]'.
Re:Numbering should start at zero: E.W. Dijkstra by Quekid5 on Friday March 21, 2025 @08:23AM (#43434782)
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.
Re:Numbering should start at zero: E.W. Dijkstra by pansa2 on Friday March 21, 2025 @08:50AM (#43435022)
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: E.W. Dijkstra by UncleEntity on Friday March 21, 2025 @09:21AM (#43435347)
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: E.W. Dijkstra by pansa2 on Friday March 21, 2025 @09:27AM (#43435403)
No - negative indexing is fine as it is. You just need to be careful about the special case of negative zero.
Re:Numbering should start at zero: E.W. Dijkstra by Ukv on Friday March 21, 2025 @09:31AM (#43435467)
> 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: E.W. Dijkstra by tetha on Friday March 21, 2025 @08:34AM (#43434869)
I've heard the statement "Let's just see if starting with 0 or 1 makes the equations and explanations prettier" quite a few times. For example, a sequence <x, f(x), f(f(x)), ...> is easier to look at if a_0 has f applied 0 times, a_1 has f applied 1 time, and so on.
Re:Numbering should start at zero: E.W. Dijkstra by xandrius on Friday March 21, 2025 @08:50AM (#43435016)
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: E.W. Dijkstra by branko_d on Friday March 21, 2025 @08:50AM (#43435025)
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: E.W. Dijkstra by hn_throwaway_99 on Friday March 21, 2025 @09:35AM (#43435518)
The comment you are replying to essentially said exactly that:

> 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: E.W. Dijkstra by goatlover on Friday March 21, 2025 @01:02PM (#43438203)
But then again Fortran proceeded C, is known for being very performant, and is 1-based by default.
Re:Numbering should start at zero: E.W. Dijkstra by Aardwolf on Friday March 21, 2025 @09:11AM (#43435246)
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: E.W. Dijkstra by venusenvy47 on Friday March 21, 2025 @09:55AM (#43435752)
Just speaking anecdotally, I had the impression that math people prefer 1-based indexing. I've heard that Matlab is 1-based because it was written by math majors, rather than CS majors.
Re:Numbering should start at zero: E.W. Dijkstra by Aardwolf on Friday March 21, 2025 @10:25AM (#43436085)
Yes but I think it might be just habit, and it's exactly in matlab that dealing with for loops over sub matrices is so annoying due to this
Re:Numbering should start at zero: E.W. Dijkstra by BeetleB on Friday March 21, 2025 @01:05PM (#43438234)
Indeed. I was going to point out that mathematicians choose the index based on whatever is convenient for their problem. It could begin at -3, 2, or whatever. I've never heard a mathematician complain that another mathematician is using the "wrong" index. That's something only programmers seem to do.
Re:Numbering should start at zero: E.W. Dijkstra by shiandow on Friday March 21, 2025 @09:27AM (#43435414)
In mathematics, if it matters what index your matrix starts on then you're likely doing something wrong.

Besides, in the rare cases where it does matter you're free to pick whichever is convenient.

Re:Numbering should start at zero: E.W. Dijkstra by personalaccount on Friday March 21, 2025 @11:10AM (#43436632)
> 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.

Zero based indexing had nothing to do with Dijkstra's opinion but the practical realities of hardware, memory addressing and assembly programming.

> I'm sure there is a culture that counts their first finger as 0

Not a one because zero as a concept was discovered many millenia after humans began counting.

Re:Numbering should start at zero: E.W. Dijkstra by pwdisswordfishz on Friday March 21, 2025 @11:34AM (#43436879)
> Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor

So you claim this is just an appeal to authority and as a rebuttal you give appeal to emotion without being an authority at all?

> 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 finger as 0 and I expect they're mocked mercilessly for it by all their neighbours

> 0-counting sequences represents a crazy choice

5G chess move.