Live data from Hacker News

Why numbering should start at zero (1982)

cs.utexas.edu

41–50 of 72 posts

Re: Why numbering should start at zero (1982)

#41
post #2

There's one important use case for Dijkstra's alternative (c), that is, an interval closed at both ends. That is when your set has a maximum element and you want to be able to express an interval including the maximum. This is, of course, important in any programming language whose basic integer types have a bounded range. For instance, the following loop in C never halts: for(uint8_t i = 0; i What's even worse, the…

In fact, there's no way to make a C for loop run 256 times using only an 8-bit counter, because there aren't enough states. Looping N times requires N+1 evaluations of the termination condition, and with only 256 states of the index variable you can't have more than 255 iterations.

You can do it with a do { } while loop, which only needs N evaluations of the termination condition for N iterations.

Re: Why numbering should start at zero (1982)

#42
post #7

Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops being an advantage. - Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me. I commit less off-by-one errors with 1-based, and I don't have to double-che…

> Starting at 1 is more intuitive

Only when counting. Why should 'numbering' be the same as 'counting'? I don't see why that would lead to a generally more intuitive when most of what you calculate with 'numbering' is offsets and indexes. Offsets begin at zero. Indexes could be by ordinal and not by offset, I suppose, but I don't see how that could be a benefit in typical index calculations--mostly they're just offsets in my world, distances between indexes.

I rarely ever refer to specific, literal indexes, so I can't say that I have much of an opinion on them, but that does seem to be the area where indexing by ordinal would make the most sense. Perhaps this is the main operation that dominates numerical calculations? Rails even adds english ordinal methods (e.g. first, second, third, fourth, fifth) for this purpose.

Re: Why numbering should start at zero (1982)

#43

I always like to point to the Julia package TwoBasedIndexing ( https://github.com/simonster/TwoBasedIndexing.jl/ ) which is IMHO provides the very best way to index an array. Yes, Julia defaults to 1-based indexing, and allows zero-based indexing ( https://github.com/JuliaArrays/OffsetArrays.jl ) or indeed whatever sort of indexing you want, yet I think the world is going to eventually come around to THE ONE TRUE ind…

[deleted]

Re: Why numbering should start at zero (1982)

#45
post #38

I always like to point to the Julia package TwoBasedIndexing ( https://github.com/simonster/TwoBasedIndexing.jl/ ) which is IMHO provides the very best way to index an array. Yes, Julia defaults to 1-based indexing, and allows zero-based indexing ( https://github.com/JuliaArrays/OffsetArrays.jl ) or indeed whatever sort of indexing you want, yet I think the world is going to eventually come around to THE ONE TRUE ind…

I remember reading a funny quote that the obvious compromise between 1- and 0-based indexing was 0.5, and that it was dismissed without proper consideration. Unfortunately, I do not remember where I read that or who wrote/said it. :(

Stan Kelly-Bootle: https://www.azquotes.com/author/27328-Stan_Kelly_Bootle

Re: Why numbering should start at zero (1982)

#46
post #4

As usual, let me point out that Dijkstra's handwriting is a treat if you haven't seen it before. Here's the handwritten version of this note: https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

Is that his real handwriting??? That's not a fancy font?

Wow, it really IS a treat. I've always heard people say how much a person's handwriting can tell you about that person. Mine is a mangled mess, and I'm pretty disorganized in most areas of my life.

Re: Why numbering should start at zero (1982)

#47
post #23

When I was learning QBasic some billion years ago I recall that I was happy with arrays generally starting at 1 (even though this is user specified in Basic). When I learned C I initially wasn't thrilled about re-learning arrays, but eventually grew to only like 0-based arrays. I'm thinking this is like people who drink coffee will cite studies saying it is good for you, and people who hate it will cite opposite stud…

I had been comfortably programming in C and python for years and found 0-based indexing to be perfectly sensible. For the last couple of years I've been programming a lot in Julia. Initially I was horrified by the idea of indexing from 1 but I got the hang of it remarkably fast. Now I feel it's totally natural either way, depending what language I'm in.

I think it's only ever been a matter of preference as you say. And Julia was otherwise such a pleasure to write code in that it was easy for me to pay the small price to learn that.

Re: Why numbering should start at zero (1982)

#48
post #38

Earlier quoted context omitted.

I remember reading a funny quote that the obvious compromise between 1- and 0-based indexing was 0.5, and that it was dismissed without proper consideration. Unfortunately, I do not remember where I read that or who wrote/said it. :(

Stan Kelly-Bootle: https://www.azquotes.com/author/27328-Stan_Kelly_Bootle

Thank you! Sad to see he passed away, though.

Re: Why numbering should start at zero (1982)

#49
post #4

As usual, let me point out that Dijkstra's handwriting is a treat if you haven't seen it before. Here's the handwritten version of this note: https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

Is that his real handwriting??? That's not a fancy font? Wow, it really IS a treat. I've always heard people say how much a person's handwriting can tell you about that person. Mine is a mangled mess, and I'm pretty disorganized in most areas of my life.

It's real, but if you'd like to emulate it, there's a TTF font floating around out there:

http://lucacardelli.name/indexartifacts.html (go to Artifacts > Fonts tab).

Or, https://www.google.com/search?q=dijkstra+ttf+font

Re: Why numbering should start at zero (1982)

#50

Earlier quoted context omitted.

That notation is ambiguous. It doesn't cause a problem for humans since we can infer the correct meaning from the context, but it would for computers. For example, does 3, 5, ... 11 mean the odd numbers between 3 and 11 inclusive, or the primes between 3 and 11 inclusive?

No, it is ambiguous for humans too as evidenced by the gazillions of problems of the form "N1, N2, N3, what number comes next?". Just decide on some rule for how decide and let people make their own standard if they don't like it.

Of course the answer is N1 - 3 N2 + 3 N3, since that naturally completes the cubic interpolating polynomial.
Post reply on HN