Live data from Hacker News

Why do arrays start at 0?

buttondown.email

41–50 of 702 posts

Re: Why do arrays start at 0?

#42
The general term of arithmetic and geometric sequences seem simpler when indexing from 0 rather than 1. I do not think that '1' is more human focused for anything than '0'.

Re: Why do arrays start at 0?

#43

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…

> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.

It's fundamentally a distinction between end-index and start-index.

Most human counting uses end-index. I.e. "1" is after 1-thing (has passed, is physically obtained, etc.).

Most computer counting uses start-index + length, for efficiency and to better generalize. I.e. "1" is at the memory address immediately before the "1"st item.

Which ultimately creates the "0 index is 1st thing" linguistic confusion.

PS: Also, language predates computers by a few years, and the concept of zero is hard.

Re: Why do arrays start at 0?

#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+widthy]`. With 1 based indexing it is at `array[x+width(y-1)]`. You might argue that programming languages should support multi-dimensional arrays, but you still need operations like resizing, views, etc.

Re: Why do arrays start at 0?

#46

Earlier quoted context omitted.

No, we don't count from 0. That's like saying we start counting a baker's cup from 0 because you can have half a cup.

If we (speaking as a North American) count from 1, does that mean other cultures (e.g. Korean) count from 2?

No, it just means we have a different notion of what a year is in regards to age. Similar to how different cultures can use different units of measurement for length, mass, etc.

Re: Why do arrays start at 0?

#47
post #26

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…

If you think of the index as an offset you would start with 0. BTW on which level is the 1st floor?

You've got ...P3, P2, P1, G, M, 1, 2, 3...

Re: Why do arrays start at 0?

#49

Earlier quoted context omitted.

No, birthdays are 1 indexed. Your Nth birthday is the day you turn N years old. When you're 1 year old, you've been alive for 1 year, not 2 years.

The moment you are born you are 0 years old (or perhaps 0.75 years old, but we don't usually recognize that). We count from zero in this case, at least implicitly. In some cultures you are considered 1 the moment you are born, so the zero indexing isn't universal here, but typical in North America as noted earlier.

Typical counting of things starts from 0. If you count apples you implicitly start at zero and add 1 for each apple. If you count age, you start at birth (0) and count years; one for each birthday. That isn't zero based. The difference is the index of the item between the starting point and the next item. In zero based this item is number 0, in one based, this item is number 1. The first year of life is generally considered the year following birth.

Re: Why do arrays start at 0?

#50

Earlier quoted context omitted.

If we (speaking as a North American) count from 1, does that mean other cultures (e.g. Korean) count from 2?

No, it just means we have a different notion of what a year is in regards to age. Similar to how different cultures can use different units of measurement for length, mass, etc.

Yes, exactly. One of which carries an implicit zero indexing. The date of birth doesn't disappear just because you decided to use a different measuring device.
Post reply on HN