Hacker News new | ask | show | jobs
by dr_zoidberg 1250 days ago
> The 0 indexing in python does really and truly suck sometimes though.

Now that's something you usually don't hear. At my university I heard a bit of "urban legend" about how back in the day (think early 1990's) a couple of professors got a peer-reviewed article published because they found out that Matlabs 1-indexed implementation of some algorithm resulted in numerical errors, which they measured and corrected. Don't really remember the details, and most of those involved have already retired.

2 comments

What you've said isn't surprising.

The problem isn't whether 0 or 1 are "right" or not, it's the inconsistency. It makes transcribing something from a textbook harder, because the indexing logic in a textbook algorithm can get quite intricate. It's even worse if they use slices like M[i:j,m:n].

Indexing from 1 is the standard in many areas, going back many decades. SWEs have adopted a different convention.

(Obligatory) There are only 2 hard problems in computer science: cache invalidation, naming things, and off-by-one errors
may You concurrency forgotten have.
It just printed out in your comment a few hours after I thought I invoked it.
but this depends a lot on the particular formula. For example, if you use discrete Furier transforms, all formulas are natural with 0-indexing, but weird with 1-indexing. In general, when the indices of an array are to be interpreted as modulo N, you want them to be 0, 1, ..., N-1

It is inevitable to have to use both conventions if you do varied math stuff. Thus, you will never be happy.

The pragmatic solution for transcribing is probably to make some functions `array_index` and `array_slice` that automatically convert between the two conventions. I think Julia offers another way: It lets you choose between them.
I prefer OPTION BASE {0, 1} :)
I just never find myself working out formulae/series/proofs on paper using zero-indexing. When you look up derivations/solutions in books and references they're one indexed. A recent example was a formula that had a (n-1)!*n! term that made sense from understanding the derivation. But I forgot to code it as (n-2)!*(n-1)! at first.

So you end up having to translate from one-indexed derivations to zero-indexed code and it leads to different bugs. Ultimately zero-indexing just trades one layer of bugs for another.

IMHO zero-indexing makes sense to people thinking about the machine, not necessarily the work people are trying to use the machine to do.

To be fair sometimes the opposite is true and zero-indexing gives less complex expressions.

There are good reasons why indexing from zero with exclusive upper bounds should be preferred. I'll defer to Dijkstra for the explanation: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
Because it's intuitive that an array contains an element before the first element?
I have a ruler. What number does the first centimeter start at?
It's a bit like all those people claiming that the new millennium shouldn't have been celebrated until 2001. They didn't stop to think how many years pass between the epoch and the time of interest. You celebrate a baby's first birthday after one year of time has passed. Until that point they are zero (and a bit) years old.
Do you typically measure sets with a ruler?

Furthermore it depends on what you're doing with that first centimeter. In many instances the first centimeter is correctly considered to be 0.5

Is this a pithy response? The first element is indexed by zero.
Isn't dropping Dijkstra links as an appeal to authority about 0-indexing considered a pithy response?

How about I drop a link about Einstein Notation with a curt observation that there are good reasons it uses both zero and one indexing simultaneously?

As I said it depends on how you are thinking about things. Indexing makes sense to people who are focusing only on the machine.

We're discussing programming language design. Referencing a prominent computer scientist to explain something on that subject better than I can seems like a reasonable thing to do. If you want to point me to somewhere where it can be argued that indexing from one is useful (not just a matter of aesthetics), please do.
That's not a scientific paper.

I find it easier not to make mistakes when I index from 1.

It's a numbered manuscript by Edsger Wybe Dijkstra, one of the most influential members of computing science’s founding generation [1].

The issue stems from the word "index" in the sense of "to count" .. that the first cell is numbered 1 makes sense to some.

What is actually happening is measurement, or the offset from the origin of memory.

The first inch starts at zero on the tape measure and ends at 1, that first inch has an offset of zero.

For all manner of reasons to do with measuring memory as one might measure wood a great many people think in terms of memory offsets.

[1] https://www.cs.utexas.edu/users/EWD/

[redacted]
Maybe work on your communication skills.