Live data from Hacker News

Why do arrays start at 0?

buttondown.email

381–390 of 702 posts

Re: Why do arrays start at 0?

#381
post #219
post #168

If you ask people which floor of building they're on, it's going to depend on which country they're in. In North America, at least, the first floor you walk into (in a sane city: I understand there are some which do not qualify in this respect due to hills or historic disaster recovery) is the first floor. On other continents, you enter the ground floor and need to take stairs or an elevating device to get to the fir…

The ground floor is 0. The floor above 0 is 1. The floor below 0 is -1. Anything else is crazy TBH, the whole point of integer numbers is to count things that start at a defined point and go opposite ways. Why shift it and then reinvent weird pseudo-negative numbers like S1? But then again people measure distance in feet and write dates as month day year :)

Counterpoint: because floors are sum types.

Floors 1 through 10 are "regular" floors. L is the lobby. P1 through P3 are the parking (basement) levels.

So you have three types: regular, lobby, and parking. And the elevator labels are of type regular | lobby | parking.

(Now you can't disagree with me because sum types are popular on HN, and I have framed this in terms of sum types.)

Re: Why do arrays start at 0?

#382

Earlier quoted context omitted.

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

Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.

> Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.

Why not? "Every cardinality, except of the empty set, is a natural number" isn't very convincing to me, if we're making cardinal-based arguments.

Re: Why do arrays start at 0?

#383

If it starts at zero we should call it an "offset" That would fix so many of these discussions.

Maybe, but then they'd just become discussions about whether `y` in the `x[y]` syntax "should" be an offset or an index. Which one gets the language priority?

...Of course, the answer is offset, because offsets are beautiful, composable, elegant things that harmonize with bitmasks, modulo, array slices, and everything else, while (one-based) indexes are nothing but "offset plus one", no more useful than "offset plus two".

Re: Why do arrays start at 0?

#384

Earlier quoted context omitted.

The counting activity doesn't begin when the first item is registered; it begins when the counter is initialized to zero. A decision is made to begin counting, along with the realization that nothing has been counting yet. That's when counting has started. When the first item is seen, the counting is then continuing. Suppose your job is to count some events. You check in for work at 8:00 a.m., but the first event has…

What counter? You're saying a counter exists before people start counting? Is this a form of mathematical Platonism?

A counter can be lazily instantiated just before the first item is counted.

Re: Why do arrays start at 0?

#385

Earlier quoted context omitted.

As Jens Gustedt points out[1], the following intentional unsigned overflow works perfectly for downwards iteration (even when length is 0 or SIZE_MAX), though it looks a bit confusing at first: for (size_t i = length - 1; i You are also free to start at any other (not necessarily in-bounds) index, just like with ascending iteration. [1] https://gustedt.wordpress.com/2013/07/15/a-praise-of-size_t-...

Principle of least surprise violated. Also, that behavior is not guaranteed. The programmer would need to be aware of how the particular machine in question actually handles that. Then again, that's C.

Unsigned integer underflow and overflow are both guaranteed to wrap by the C standard.

Re: Why do arrays start at 0?

#386

Earlier quoted context omitted.

What do you do for a living, and why are they paying you not to understand this?

Do you know the definition of countable? A set S is countable if there is a one-to-one mapping from S to N where N is the natural numbers. Do you know that 0 is not a member of the natural numbers? We literally start counting at 1 by definition of countable.

An empty set is countable; it has an empty mapping to the natural numbers. Its cardinality is zero.

Re: Why do arrays start at 0?

#387
post #353

Earlier quoted context omitted.

I'd agree so why did languages that allow specifying the base die (other than maybe VBA).

Because changing the base is a great source of bugs. That said, not all languages have given up on this. For example Julia allows it. https://docs.julialang.org/en/v1/devdocs/offset-arrays/ Ironically I learned this from a discussion of Julia bugs. Apparently changing offsetting of arrays has proven to be a source of bugs in Julia. So maybe someday they will come to the same conclusion as languages like Perl and stop…

I'd argue 0-base is a source of bugs too! Ideally we'd be able to catch more array indexing bugs at compile time - there are definitely cases where it should be possible to determine that arrays are being incorrectly indexed via static analysis.

Re: Why do arrays start at 0?

#388

Earlier quoted context omitted.

> when we count, we start at 1 That is false; counting begins by initializing an accumulator to zero. When you register the first item, the count jumps from 0 to 1.

Who sets an accumulator to zero and then adds one in everyday language?

For instance, someone who makes a fist with zero extended fingers before counting.

Or someone who simply becomes motivated to count something, without making any utterances or gestures to that effect. The motivation is followed by the persistent awareness that nothing has been counted yet, which then changes to 1.

Re: Why do arrays start at 0?

#389
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.

Re: Why do arrays start at 0?

#390

Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing. To write 4 numbers (including zero) you only need two bits 0: 00 1: 01 2: 10 3: 11 To write 4 numbers if you avoid using the number zero, you need three bits 1: 001 2: 010 3: 011 4: 100 If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit…

I like how the article summarizes this idea:

> These all point to one reason why 0-indexing might be preferred: it matches machine semantics more

Post reply on HN