Live data from Hacker News

Why do arrays start at 0?

buttondown.email

411–420 of 702 posts

Re: Why do arrays start at 0?

#412
post #166
post #95

Earlier quoted context omitted.

That's why I prefer the Superior(TM) Korean age counting. You are one year old when you're born (it's your first year!). You are two year old on the next New Year's day. (Congratulations, it's your second your now!) So, if you're born on December 31st, you're two years old the next day. (I see no problem, but apparently some people are hung up on such minor details. I can't fathom why.)

> You are one year old when you're born (it's your first year!) That makes no sense to me. It's also your first century. Does that make you one century old? Of course not! The moment you're born, you're not even one hour old, let alone one day.

Except that we actually do use this system for years. Thus 1 BC (the first year BC) was followed by 1 AD (the first year AD). Also for centuries, as in 'the 20th century' being the years 1901 to 2000.

Re: Why do arrays start at 0?

#413
post #95

Earlier quoted context omitted.

That's why I prefer the Superior(TM) Korean age counting. You are one year old when you're born (it's your first year!). You are two year old on the next New Year's day. (Congratulations, it's your second your now!) So, if you're born on December 31st, you're two years old the next day. (I see no problem, but apparently some people are hung up on such minor details. I can't fathom why.)

A more sensible English translation from Korean would be to use the phrase "in year X" rather than the phrase "X years old": a newborn is in year 1; after 12 months they are in year 2; etc. In fact, this whole discussion is more about a choice of phrasing rather than the numbers. When indexing arrays, sometimes we're talking about an offset from the first element (starting with "0"), and sometimes we're talking about…

Right. I try to refer to a[n] as 'element number n' rather than 'the nth element'.

Re: Why do arrays start at 0?

#414

Earlier quoted context omitted.

The "language" that has a 0-based indexing scheme is assembly. An HLL with 1-based counting that compiles to assembly will introduce additional computational overhead for the translation of the index. If 1-based indexing was used in assembly, then "mov 1(%ebx),%eax" would be the equivalent of "mov (%ebx),%eax".

Not sure if assembly/machine code is that relevant. If one based indexing was more prevalent, the LEA instruction on x86 would just subtract one during execution

u/blutomcat's point clearly (I think) was that instruction sets didn't do what you suggest, so zero-based index is (was and still is) what you'd do if you wrote in assembly (which was not uncommon!), and on any given platform, assembly was the main language 50-60 years ago. It follows that it's easier to carry that over to higher level programming languages.

Whether that's what actually happened in the cases of HLLs that adopted zero-based arrays or not, I don't know. But today, to me, zero-based array indexing feels very natural, and the idea that zero-based arrays being simpler in assembly carrying over to HLLs seems at the very least plausible.

Re: Why do arrays start at 0?

#415
post #201

Earlier quoted context omitted.

Interestingly, that Dijkstra article was originally published/circulated in handwritten form. It helps showcase the author's handwriting skills. https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

The handwriting skills were common back in the day... By the way, are you sure that it was Dijkstra himself who hand printed these texts (rather than have, say, his secretary do that)? Because this is not exactly the (beautiful) handwriting as it was taught back then.

He handwrote them, and occasionally typed them (especially early on, I gave up trying to find the transition point). It was something he was known for. If you read the EWD's over the years you'll see the same (or very similar) handwriting throughout, which would not be the case if he had a secretary writing them for him (who wouldn't have been the same person as he moved between countries).

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra#EWD_manuscr...

https://www.cs.utexas.edu/~EWD/ - Pick random ones from different decades and you'll see very similar handwriting.

(Didn't read these, picked two at random):

2001 - https://www.cs.utexas.edu/~EWD/ewd13xx/EWD1307.PDF

1984 - https://www.cs.utexas.edu/~EWD/ewd09xx/EWD901.PDF

Re: Why do arrays start at 0?

#416
post #45

Other advantages of zero based indexing, beyond being 'closer to the machine': It works better with the modulo operator: `array[i%length]` vs `array[(i+length-1)%length+1]`. Or you would have to define a modulo-like operator that maps ℕ to [1..n]. It works better if you have a multi-dimensional index, for example the pixels in an image. With 0 based indexing, pixel `(x,y)` is at `array[x+width y]`. With 1 based index…

I think the 1-indexing folks would have to argue that a%b should return a value from 1 to b inclusive. This does make the same sort of intuitive sense as 1-indexing. For example we number clocks from 1 to 12.

12 is zero, in 12-hour clocks.

Re: Why do arrays start at 0?

#417
post #378

Earlier quoted context omitted.

Are you sure it doesn't just look that way because you are used to monospaced fonts? Ask a kid to show you their zeroth finger.

As a kid, I saw these things often on multiple-choice test sheets. The tens digit at the end of each row not matching that at the beginning always seemed awkward, and I wondered why they didn't just start at zero. This was long before I did any programming. The first year CE being "1" resulting in the new millennium starting at 2001 instead of 2000 also seemed idiotic, and the 1900s being referred to as "the 20th cen…

Well, given that year numbering system's start in Europe, zero wasn't as broadly accepted as other digits at the time.

By AD 150, Ptolemy, influenced by Hipparchus and the Babylonians, was using a symbol for zero.

https://en.wikipedia.org/wiki/0#History

To keep it confusing: the traditional proleptic Gregorian calendar (like the Julian calendar) does not have a year 0 and instead uses the ordinal numbers 1, 2, ... both for years AD and BC. Thus the traditional time line is 2 BC, 1 BC, AD 1, and AD 2. ISO 8601 uses astronomical year numbering which includes a year 0 and negative numbers before it. Thus the ISO 8601 time line is −0001, 0000, 0001, and 0002.

https://en.wikipedia.org/wiki/Gregorian_calendar#Dual_dating

Re: Why do arrays start at 0?

#419

It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…

In Western music theory, intervals are one based. No pitch change is "unison"; one diatonic step is a "major second" and so on. As a result of this silly state of affairs, an octave occurs every 7 notes, even though the root "oct" means eight. Furthermore, a "rule of nines" is needed to invert an interval: e.g. inversion of minor 3rd is a major 6th (exchange major/minor, subtract from 9).

And addition also gets broken. Like a third plus a fourth is a sixth.

Re: Why do arrays start at 0?

#420

Earlier quoted context omitted.

A rare case where 1-based indexing is more convenient is complete binary trees laid out breadth-first (as in a standard binary heap): parent is i div 2 and children are 2i and 2i+1 when starting at one and who knows what when starting at zero. But that’s the only one I know.

Except 1-based indexing is what we use in normal language. We don't use "zeroeth" or "player (number) zero" etc. And the word "first" is shortened to 1st etc. Personally I think we'd be better off if programming languages stuck to the same convention - off-by-1 errors aren't the hardest problems to deal with but they're still annoying.

Well: https://www.merriam-webster.com/dictionary/zeroth
Post reply on HN