← Back to context

Comment by umanwizard

1 year ago

I have a really hard time understanding why people like 1-based indexes! 0 is the smallest unsigned integer in every programming language I know of that supports the concept of unsigned integer. Why shouldn’t an array at the smallest possible index correspond to the beginning of the array?

It’s also very natural to think of arr[i] as “i steps past the beginning of arr”. With one-based indexing arr[i] has no natural interpretation that I know of. It’s “i-1 (for some reason) steps past the beginning of arr”. The only reason I can think of to prefer that extra -1 in your formula is just because human languages (at least the ones I know of) work this way — the 42nd element of a sequence, in normal colloquial English, means the one 41 steps past the beginning. But I’m not sure if there is any logical justification for that.

I also, despite being American, find the convention used in many countries of numbering building floors starting with zero to be more logical. I’m on the third floor, how many stories up did I travel to get here? Three.

> Why shouldn’t an array at the smallest possible index correspond to the beginning of the array?

Because then there is no good way to refer to the index before that point: You are stuck using -1 (which means you can't use it to refer to the end of the array), or null (which isn't great either).

> every programming language I know of that supports the concept of unsigned integer

Surely you know Python which uses a signed integer as an index into their arrays: list[-1] is the last element of a list. If they only used one-based indexing then list[1] would be the first and that would be nicely symmetrical. It would also mean that list[i-1] would NEVER refer to a value after ‹i› eliminating a whole class of bugs.

> It’s also very natural to think of arr[i] as “i steps past the beginning of arr.”

I think it's more natural to think of arr[i] as “the ‹i›th element of arr” because it doesn't require explaining what a step is or what the beginning is.

The exact value of ‹i› matters very little until you try to manipulate it: Starting array indexes at one and using signed indexes instead of unsigned means less manipulation overall.

> find the convention used in many countries of numbering building floors starting with zero to be more logical

In Europe, we typically mark the ground-floor as floor-zero, but there are often floors below it just as there are often floors above it, so the floors might be numbered "from" -2 for example in a building with two below-ground floors. None of this has anything to do with arrays, it's just using things like "LG" or "B" for "lower ground" or "basement" don't translate very well to the many different languages used in Europe.

The software in the elevator absolutely doesn't "start" its array of sense-switches in the middle (at zero).

  • > In Europe, we typically mark the ground-floor as floor-zero,

    _Western_ Europe. Eastern Europe prefers 1-based numbering. The reason, typically assumed, is that thermal isolation, required due to colder winters, causes at least one stair segment between entrance and the sequentially first floor.

  • Python might have used array[~0] instead, where ~ is required, to indicate end-of-list 0-based indexing.

    But I guess they wanted to iterate from the end back [-1] to the start [0], making it easy to implement a rotating buffer.

    • > Python might have used array[~0] instead

      This is what was once added to C#: arr[^idx], when this ^idx is mapped to a special object, typically optimized then out. arr[^0] means the last element.

      1 reply →

  • > I think it's more natural to think of arr[i] as “the ‹i›th element of arr” because it doesn't require explaining what a step is or what the beginning is.

    Yes, but if you will eventually need to do steps on your array, you better opt for the framework that handles them better. I agree, that if your only task is to name them, then 1 based indexing makes more sense: you do that since diapers, and you do that with less errors.

In India too, the floor at the ground level is called the ground floor (probably that is where the name came from), the one above it is called the first floor, and so on. The convention is probably from British colonial times.

Also LED floor numbers in lifts (elevators) in India start from 0 for the ground floor, as do the buttons that you press to go to specific floors.

Also, Ground Zero.

https://en.m.wikipedia.org/wiki/World_Trade_Center_site

https://en.m.wikipedia.org/wiki/Hypocenter#

> I also, despite being American, find the convention used in many countries of numbering building floors starting with zero to be more logical. I’m on the third floor, how many stories up did I travel to get here? Three.

Alternatively the ground floor is the first floor because it’s the first floor you arrived at when you entered the building.

The same point of view applies to 1-based indexing.

That said I prefer 0-based in programming and 1-based in buildings.

  • I never understood why they didn't picture the building, with the buttons and the room/apartment numbers at each floor... That would make all conventions clear. Going negative would be obvious, and just indicate which floor the elevator is at with LED's of backlighting.

    They never heard of making a UI, and just slapped buttons.

    • > They never heard of making a UI, and just slapped buttons.

      I suspect that floor numbering predates lifts (elevators) by centuries.

      Stairs are ancient.

      I mean, zero itself is a non-obvious concept. Its invention is a matter of historical record:

      https://www.open.ac.uk/blogs/MathEd/index.php/2022/08/25/the...

      ... and we still use counting systems which predate the invention of zero, such as Roman numerals.

> find the convention used in many countries of numbering building floors starting with zero to be more logical.

Ukrainian here. Multi-floor buildings always have at least one stair section to first floor due to need of thermal basement isolation. (I guess this is not pertaining to Western Europe due to more clement winters.) And, yep, it is called "first" floor. Using zero number is rare but possible (in this case it is called "tsokolny" floor) if a real "basement floor" is present, but in this case still 1-based numbering is preferred.

I'd argue that 1-based indexing is the "natural interpretation". Mathematics is inherently 1-based, and it isn't surprising that languages designed to do mathematics like R, Matlab, Mathematica, Julia all do 1-based arrays because that makes modeling paper mathematics in programs easier.

  • Sequences in math start with 1 by convention, not for any fundamental logical reason. It’s a reach to say that math is “inherently 1-based”.