Live data from Hacker News

Why do arrays start at 0?

buttondown.email

391–400 of 702 posts

Re: Why do arrays start at 0?

#391
post #243

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…

Are you sure? We are talking about the he numbering, not the work-doing. You start waiting at 8, and you wait between events. But you only count (increment) when an event happens. The 0 comes for free when you are ready to count (hello golang and C++, as well as human intuition). When you count stars in the sky you don't say "0, 1, 2". You say "..., 1, 2", or if no stars show up, you say "there are 0" after timer exp…

> But you only count (increment) when an event happens.

Increment what?

> When you count stars in the sky you don't say "0, 1, 2".

No, you say, "I'm going to start counting stars now. Okay, 1, 2, ...".

The preparation part is the zero. You counted stars before and reached some number; you're not starting at that number.

Re: Why do arrays start at 0?

#392
So what's the actual reason you'd want to have zero based arrays and such in your language (In 2020, not 1970)? In a language from scratch that doesn't care about being like previous ones, and doesn't care about micro optimization.

I've been annoyed recently by this obnoxious neckbeard behavior when they make things zero based for no reason (other than to conform to their supposedly existent philosophy), like Ratpoison and Screen selecting windows by a zero based index, which means you have to move your hand to the far right of the keyboard to select the first window with the 0 key, the far left to select the second window, with the 1 key, and one to the right to select the third window, with the 2 key.

I so happen to have been trying to work out a tangible explanation on paper of which system is better on trips a few weeks ago, and could not come up with one (and none are given in the article nor the top comments in this thread).

On a side note, the moment you say, "zeroeth", you can no longer be sure anyone knows what you're talking about because at any moment you may count in English or this pretentious UN*Xtard dialect of English. Seems like another pedagogical stumbling block held on to boomers and boomer-wannabes who just want to have their little counter counter counter culture or whatever.

Re: Why do arrays start at 0?

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

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

Re: Why do arrays start at 0?

#394
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…

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code. Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

If this were the primary reason, it seems odd that some of the earliest popular languages used 1-based indexing in an era with much slower CPUs (<= 1 MIPS) and less available memory to store instruction code. If the authors of FORTRAN were comfortable with 1-indexing on the IBM 704 (40 KIPS) in 1957, it couldn't have been prohibitive. At the same time, as computer use cases became more interactive there may have been a greater focus on performance, but it likely that the semantics of the high-level language were at least as important.

Re: Why do arrays start at 0?

#395
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?

In Tucson I've seen some buildings where the basement is the first floor. You enter at floor two.

Re: Why do arrays start at 0?

#396
post #250
post #193

Earlier quoted context omitted.

Can't they just .. disagree? Birthdays are clearly 1 indexed, the first birthday is indexed with 1, and not with 0.

They day you're born is birthday number 0

Exactly.

Birthday[0] gives you an out of range exception, since there is no birthday called 0th. Birthdays are [first,second,..] indexed from 1: brithday[1] = first, birthday[2] = second, and so on. That's what indexing a series from 1 means.

Re: Why do arrays start at 0?

#397

Earlier quoted context omitted.

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code. Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

We're 20 years past the point where you can expect everyone in tech to trace every instruction down to machine instructions. Higher level languages abstract away the need for it, and for the most part, we can rely on the authors of those languages to make many of the decisions that impact performance. The rest of us learn about these details on posts like this. It's not a sad fact, in fact it's probably one of the mo…

If you're not raining about how your code translates to logic gates, you're not a competent programmer. /s

Abstractions are good, and while we should be careful not to completely trust a brand new abstraction, high level languages are established enough that we don't need to worry about it.

Re: Why do arrays start at 0?

#398

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.

The 2 major blunders in programming: 1) Off by one errors.

Re: Why do arrays start at 0?

#399

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

But then of course the reason why 1-indexing is better (or really, arbitrary indexing, which is even better) is that it matches human semantics better.

Re: Why do arrays start at 0?

#400

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…

No, the mathematical definition of counting (i.e. whether or not a set is countable) involves mapping to the natural numbers, which doesn’t include 0.

I don't think your definition is complete. We can count a set by mapping its elements to the natural numbers, and then identifying that number which is highest. However, we must have a provision for identifying zero as the highest when the set and mapping are empty.
Post reply on HN