Live data from Hacker News

Why do arrays start at 0?

buttondown.email

461–470 of 702 posts

Re: Why do arrays start at 0?

#461

Earlier quoted context omitted.

I assure you, that if we counted from zero, there would be an instruction to add and multiply in the same number of cycles as a multiply.

That's not possible - the subtract and multiply need to be consecutive (adjust index before multiply by element size), so even if it was a single instruction it would still take longer than a multiply that didn't have to wait for a preceding subtraction. The only way to avoid the speed penalty would be either to have a wasted element at offset 0, or to maintain the array base address as (address - (1 * element-size))…

That goes against my intuition. Multiplication in hardware to this day relies on addition. Is one adder going to add an extra cycle? Or would that time be amortized? Take a look at slides 45-46 here. https://acg.cis.upenn.edu/milom/cis371-Spring08/lectures/04_...

Do you know the answer to that question? (I don't, but if someone does, it will settle this issue).

Re: Why do arrays start at 0?

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

It is because you mean two different things by "natural numbers", even though the same words are used for things that are not the same things.

I prefer to start by zero; I think it is more useful in general, and makes more sense mathematically for many (although not all) purposes, and systems that you will find objects and operations that have these properties too.

Re: Why do arrays start at 0?

#463

Earlier quoted context omitted.

> To get the address of a particular item, this layout naturally leads to the formula "base address + index * element size", with "index" being 0-based. If you want to expose other indexing schemes in your language, you'll have to add more logic to convert the user-visible index back to 0-based before you can obtain the address. The easy way to do that is by shifting the base address. In pseudo-C (I think that comput…

Should work 99.99999% of the time until someone tries to put an array at 0x000001 :)

I think, in theory, it would work regardless of the starting address. As long as you don't try to access the invalid address (which you wouldn't assuming that it's starting in the index 1, you would always be accessing the first valid address)

Re: Why do arrays start at 0?

#464

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.

Re: Why do arrays start at 0?

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

> To get the address of a particular item, this layout naturally leads to the formula "base address + index * element size", with "index" being 0-based. If you want to expose other indexing schemes in your language, you'll have to add more logic to convert the user-visible index back to 0-based before you can obtain the address. The easy way to do that is by shifting the base address. In pseudo-C (I think that comput…

[deleted]

Re: Why do arrays start at 0?

#466

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).

cannot?

Re: Why do arrays start at 0?

#467

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.

Well then clearly you'd have to index at -1, for the time state before the point at which there is no meaningful before. I'm not sure why this answer didn't occur to you.

Re: Why do arrays start at 0?

#468
post #252

Earlier quoted context omitted.

Nope. Is the empty set countable? (Yes.) Dictionary: nat·u·ral num·bers the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well Countable: https://en.wikipedia.org/wiki/Countable_set Set theory: https://en.wikipedia.org/wiki/Ordinal_number

From your own link on countable sets: > Equivalently, a set S is countable if there exists an injective function f : S → N from S to N; it simply means that every element in S corresponds to a different element in N. Defining N is usually done via a successor set, on which case 0 makes no sense to include.

A successor set is the set of successors of... 0 or 1, depending on what you are doing.

Re: Why do arrays start at 0?

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

I learned them in elementary school (in the US) as starting at 1. Indeed, Google's first result for natural number is from Oxford Languages, and defines natural number as:

the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well.

Re: Why do arrays start at 0?

#470
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 :)

This is how it should be, but the only country in the world, AFAIK, that does it that way is Germany.
Post reply on HN