Live data from Hacker News

Why do arrays start at 0?

buttondown.email

431–440 of 702 posts

Re: Why do arrays start at 0?

#431
Unusually for me, I read TFA first. Immediately thought "this is all wrong", then realized 400 commenters had posted the same thing already.

I have Martin Richards book on my shelf, and for a while shared an office with his coworker. I'm also one of the few folks here who has coded in BCPL.

Ok, so that said, this article is obvious nonsense. Zero indexing comes from the CPU itself. It's how you generate an indirect address by adding the offset. Many CPUs have this in hardware. They don't support non-zero offsets. The reason some languages have zero-based indexes is that their developers had the mind set of doing the same thing you do when writing machine code. They probably also wanted to directly interface between their language and libraries written in assembler (and the kernel was typically written in assembler too). Also they wanted to be efficient. Any offset other than zero is not efficient because you can't use the hardware addressing modes.

So then why do some languages _not_ have zero based indices? The obvious answer it that Mathematics by convention always used 1 as the base (or some arbitrary value). Many languages were conceived by mathematicians or people with a strong mathematical background.

Re: Why do arrays start at 0?

#432

Earlier quoted context omitted.

I asked my son to show me zero fingers(after show me 2 fingers and 5 fingers), he closed his fists.

My point exactly - there isn't an identifiable zeroth finger.

There's not a second finger either. You can convey "two" by showing any two fingers.

Re: Why do arrays start at 0?

#433
post #393

Earlier quoted context omitted.

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.

% is defined as (mostly) a remainder operator. I don't think you want to change the semantics of division itself.

Right. Ultimately the 1-indexers are wrong.

Re: Why do arrays start at 0?

#434

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

"just subtract one" would take a long time 50-60 years ago.

Re: Why do arrays start at 0?

#435
post #405

Earlier quoted context omitted.

Nats start at 0, end of discussion - it's only logical to index by the naturals.

That's really silly - the natural numbers also famously start at either 0 or 1, depending on the country, the discipline, and the individual.

I like to treat the naturals as starting at zero, but not for any mathematical reason. I just think that since it's easy to write ℤ+, it's more useful for ℕ to refer to something slightly different than it would be for ℕ to be a synonym for ℤ+ while anyone wanting to include zero in their set was forced to spell out "nonnegative integers".

A dichotomy between "ℤ+" on the one hand versus "ℕ" on the other is just plain more convenient than a dichotomy between "ℤ+" and "ℕ" on the one hand versus "ℤ\ℤ-" on the other.

Re: Why do arrays start at 0?

#436
post #404

Earlier quoted context omitted.

Sum types don't automatically define ordering between the possible values AFAIK? So in your implementation you'd have to manually define the up: (regular | lobby | parking) -> (regular | lobby | parking) down: (regular | lobby | parking) -> (regular | lobby | parking) and which_way: (regular | lobby | parking) x (regular | lobby | parking) -> direction If you use subset of integers you can just use operators inc, dec…

If I build a building in the shape of a double helix, what order are the two floors in? Hmm, are there actually an infinite number of floors? Do we need to switch to floating point?

> If I build a building in the shape of a double helix, what order are the two floors in?

Why separate the floors that are on the same level?

> Hmm, are there actually an infinite number of floors?

I assumed there isn't and that we use a subset of integers. If you have infinite number of floors - feel free to use full set of integers (if you have enough memory that is).

> Do we need to switch to floating point?

No point, if you have more than aleph0 floors - floats won't help you.

Re: Why do arrays start at 0?

#437

I always assumed it came back to math, and that t0 or time-zero is useful for the beginning state before anything happens.

What if there was no t0 for the universe, as Hawking and a few other physicists have argued? There was just the first plank second and no meaningful before.

Re: Why do arrays start at 0?

#438
post #235

I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers". Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow. The simplest possible implementation of arrays is having a start addres…

Nats start at 0, end of discussion - it's only logical to index by the naturals.

> Nats start at 0, end of discussion - it's only logical to index by the naturals.

Well, no, obviously you want to index by ordinals, and they start at "first".

Re: Why do arrays start at 0?

#440

Earlier quoted context omitted.

And? My solution? Let the kid grow up and learn.

Quoted post unavailable.

Anecdotally, I taught my children numbers starting from zero with a similar routine, based on cardinality. They immediately got it.

My kids' school also uses zero-based tables.

"Natural" is a very loaded and ambiguous term, best avoided if you hope to have sane discussions.

Post reply on HN