Live data from Hacker News

Why do arrays start at 0?

buttondown.email

621–630 of 702 posts

Re: Why do arrays start at 0?

#621
post #610
post #536

Earlier quoted context omitted.

Which floor is the first floor in your building? Depends which country your building is in, and more! My apartment building is 1,2… but my mall is G,M,1,2… (same country different architects). For many years I lived in Europe where it’s usually G,1,2… (where G can also be E or Fsz or whatever) and when I was in the US I had to remember that 1 is G, though L,2,3… is also common. City people live with index ambiguity a…

Not only that but which floor is the 13th (in the West) or 14th (in China)? Often those numbers are skipped because "luck", "feng shui", or other superstitious nonsense. Do you count the machine floors in high rise buildings? In AU, the ground floor is effectively "zero", the first floor is the one above ground. Depending on the design of the building, there might be a "mezzanine" floor between ground and 1st. Baseme…

In one department store in Finland there are two separate numbering schemes, see this photo from the elevator: https://hs.mediadelivery.fi/img/some/default/c244c43c3008429...

Re: Why do arrays start at 0?

#622
post #440

Earlier quoted context omitted.

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.

The index is the distance away from the first element. So index 0 is the first element. Index 3 is 3 away from the first element, so the fourth element. It's the difference between ordinal and cardinal numbers. In other words, counting and indexing.

Well well well...

https://news.ycombinator.com/item?id=5888459 (quoted here for convenience)

—————

Formally, ordinals start at zero too :-) [0]. We owe their latest definition to Von Neumann, but AFAIK, the former definitions were similar. [...snip...]

--

[0] This is meta-meta-contrarianism.

- The layman counts from 1

- The uptight programmer counts from zero, because Dijkstra said so (or so he thinks).

- The meta-contrarian (I used to be one) says fuck it, ordinals start at one.

- The meta-meta-contrarian reads Wikipedia[1], realizes he was formally wrong, and goes one step further in pedanticity, back to zero [0].

That being said, my brain prefers 1-indexing programming languages like Lua, Julia, R and Matlab...

[1] http://en.wikipedia.org/wiki/Ordinal_number

[0] Help! I'm stuck in a Boolean algebra[1][0]!

[1] http://en.wikipedia.org/wiki/Boolean_algebras_canonically_de...

[0] ... where xor is an addition that wraps around ...

Re: Why do arrays start at 0?

#623
post #448

Earlier quoted context omitted.

No. Your age is 1-indexed. It's a 'birthday' in English and German ('geburstag'); in French it's 'anniversaire', and so on. But pretty much everyone indexes age from 1. The fact of your birth is the transition from (legal) non-existence to existence, the equivalent of a dimensionless point.

Age is definitely 0-indexed - a newborn and exactly 1 year old differ by a year. People also count months during the first year, which is still 0 years old. As in 00:xx is the first hour, and 01:xx is the second. If you're 30 years old, it's your 31st year of life now. But gregorian epoch itself is 1-based. 1AD (0001-01-01) goes right after 1BC (-0001-12-31). There was no 0000-mm-dd. That's why 3rd "millenium" and 21…

Age is not indexed.

And the birthdays are definitely 1 indexed: you denote your first birthday with 1, and not with 0, and so on. You don't denote any birthdays of yours with 0. You may denote something else with it, but birthdays[0] gives an out of range exception. (Especially true in French, where birthdays are called anniversaire, but in English too.)

Re: Why do arrays start at 0?

#624

Earlier quoted context omitted.

> for arr[i] = 2, you'll still need to subtract 1 You can, but don't need to. Just have compiler store array pointer constant as (arr-1) instead of arr, et voila, zero runtime overhead. Or, for modern(-ish) ISAs, often you can add/substract a small constant at runtime, with no extra cycles taken. For example, for x86_64: # rbp contains "true" pointer to arr # rax contains 1-based array index mov rax, QWORD PTR [rbp-8…

It's basically only x86 among modern ISAs that lets you do base + literal + register * literal, aarch64 only gives you base + register shifted by a literal, and I believe RISC-V is similar: https://gcc.godbolt.org/z/dz768768z

But it will still execute with likely no extra time at all due to OOE and how fast arithmetics are.

Re: Why do arrays start at 0?

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

Another advantage is with ranges: 0-based indexing and exclusive ranges work well. This is apparent with cursor position in text selection Consider: Characters h e l l o Cursor index 0 1 2 3 4 5 Char index 0 1 2 3 4 Range [0,3) [0,1,2] Range [2,5) [2,3,4] Range [1,1) [] If we used 1-based indexing and exclusive ranges, it leads to ranges where the end index is greater than the string's length... Characters h e l l o…

Yeah, offsets are just easier to mathematically manipulate than ordinals. It's not just pointer arithmetic where it matters that item i corresponds to start+i*step. Any time you want to convert between integer indices and general linearly-spaced values, 0-based indexing is more convenient.

Re: Why do arrays start at 0?

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

I always assumed it started at zero otherwise you don’t use the full range of an unsigned integer and you can only have an odd number as your capacity (for whatever you use indexing for, not just RAM addressing).

Re: Why do arrays start at 0?

#627

Unfortunate that the Julia folks weren't exposed to this when they designed the language.

https://juliaarrays.github.io/OffsetArrays.jl/stable/ 0-based or any arbitrary offset of your choice so that your data model can more closely map to what you need it to. And use eachindex instead of making assumptions and you can work with arrays using 1-based, 0-based, or arbitrary-based indexing.

For this particular problem, having a choice even worse than 1-based indexing. Code become a giant PITA to read because you have to switch back and forth.

Re: Why do arrays start at 0?

#628

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…

Someone linked to your johnny decimal system earlier today. https://johnnydecimal.com/ I liked the idea but I noticed that it didn’t seem to be 0 indexed. If I adopt this i’m definitely going for zero indexing.

Zero is reserved for meta stuff: https://johnnydecimal.com/concepts/what-about-00-09/

Re: Why do arrays start at 0?

#629

Earlier quoted context omitted.

It could just as easily be 1 ≤ i ≤ N and then I wouldn't have to remember that the lower bound is inclusive and the upper exclusive.

Closed ranges (with both ends inclusive) are super annoying to work with. You can represent the empty range (unless you are willing to do [i : i-1]), and they don't compose like half open ones: [a : b) + [b : c) = [a : c).

For some reason I am thinking in the terms of closed ranges if not specified otherwise/doing it for myself. [i:i-1] is how i think of empty ranges, and [i:i] if I want to capture the element i with a range. Also [a:b] + [b+1:c] corresponds more what I want, than [a:b) + [b:c).

I guess the majority of the people are not like this, but arguments like "just look at it how strange it looks" don't do it for me, because it looks natural, and the other way looks complicated.

Re: Why do arrays start at 0?

#630
post #596

Earlier quoted context omitted.

Since as you say, C doesn't carry size information, you would actually need to do it each time a pointer is created from an object. In C you can have arrays of arrays (not pointers) or arrays in structs, where there is no pointer on creation: // 11 arrays are created here int foo[10][10]; It doesn't make sense to create pointers for the 10 inner arrays, so the subtraction would presumably happen when referring to eac…

when I count apples I happily start with one. it's "natural" to label the first thing "1". or how would you count, say, 3 Apples? or three legionaries?

[0, 1, 2] would be how I would refer to my 3 apples. Each apple is identified by the number of apples that precede it.

I don't think it's inherently more natural to start from 1, just conventional. Disregarding history/convention, I think it would be more natural to use the lowest available natural number.

Back in Roman times, the lowest natural number that people were aware of was "1", so obviously they started counting from that number.

Our understanding of and use of mathematics has evolved since then, and accordingly there are fields such as computer science and combinatorics where there are clear advantages to starting from the smallest number (zero). In virtually all other cases, the reason "1" seems more natural is because that's the way it's been done historically.

It seems that when labelling those apples using a 1-based count, the logic is basically: each apple is identified by the number of apples that precede it, plus 1. The reason for the "plus 1" is that that was your starting number, but it could have easily been 2 or 3. If you instead start from 0, you can omit the "plus X" logic, just as I omitted the "+ 1" and "- 1" logic in my year/century formulae when moving from 1-based to 0-based counting.

Post reply on HN