News for hackers. Stuff that matters.
Re:Again on 0-based vs. 1-based indexing by segfaultbuserr on Wednesday January 20, 2021 @04:20AM (#25844310)
> It works mathematically better with some of the most common operations on array offsets

The problem is that for every formula or calculation best suited for 1-based indexing, there is another which is better implemented using 0-based indexing. Regardless of which indexing mode you are using, peppering your code with +1 or -1 is eventually inevitable. It's why I think the choice is a matter of preference and has little to do with technical reasons.

Overall I still prefer 0-based indexing - a few bytes of RAM is cheap nowadays, when the calculation can be simplified using 1-based indexing, deliberately not using [0] and pretending that the array begins at [1] is often an acceptable option (the trick was originally invented for porting Fortran code...)

Re:Again on 0-based vs. 1-based indexing by user-the-name on Wednesday January 20, 2021 @05:18AM (#25844650)
Not convinced that is the case. I do not pepper my code with +1 and -1 while using 0-based indexing.

I can think of a lot of cases where I would do it for 1-based indexing, but very few for 0-based indexing. If you want to make the claim that there similarly many, you're going to have to come up with some substantial examples.

Re:Again on 0-based vs. 1-based indexing by tsegers on Wednesday January 20, 2021 @06:59AM (#25845359)
The -1 with 0-based indexing occurs very frequently when using the amount of elements in some collection to derive boundaries of the indexes, which will range from 0 up to and including n-1. Grepping for "- 1" in my code folder returns mostly expressions that look like 'length/len/size - 1'.
Re:Again on 0-based vs. 1-based indexing by user-the-name on Wednesday January 20, 2021 @07:06AM (#25845426)
This can happen, but it is fairly rare, especially if you favour handling ranges as either (start, length) tuples, or as half-open intervals, with the end index being one past the highest index. This representation simplifies many things, many of them unrelated to 0- or 1-based indexing.
Re:Again on 0-based vs. 1-based indexing by tsegers on Wednesday January 20, 2021 @07:17AM (#25845536)
While that is true, ranges and intervals are not native constructs in quite a few languages.
Re:Again on 0-based vs. 1-based indexing by ufo on Wednesday January 20, 2021 @01:02PM (#25849622)
One of the more common examples is if you need to iterate backwards. It's awkward because the [0,N) interval becomes (N,0].

for(i=N-1; i>=0; i--)

Re:Again on 0-based vs. 1-based indexing by naniwaduni on Wednesday January 20, 2021 @02:46PM (#25851028)
Notably, this requires you to use a signed index type, which is absurd.

The alternative is to use the so-called goes-to operator: for (i=N; i --> 0;). But this actually relies on the postincrement semantics, which is a huge wart.