Earlier quoted context omitted.
At which number starts the first centimeter on a ruler ?
At which number starts the zeroth centimeter on a ruler?
Why do arrays start at 0?
301–310 of 702 posts
Re: Why do arrays start at 0?
#302Earlier quoted context omitted.
> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.
It's fundamentally a distinction between end-index and start-index. Most human counting uses end-index. I.e. "1" is after 1-thing (has passed, is physically obtained, etc.). Most computer counting uses start-index + length, for efficiency and to better generalize. I.e. "1" is at the memory address immediately before the "1"st item. Which ultimately creates the "0 index is 1st thing" linguistic confusion. PS: Also, la…
i think a more accurate and complete idea is that humans refer to a thing in its entirety, with things being lined up and scanned in order as only a potential convenience
if you ask someone to identify an object, they'll point to the middle (or center of the most important component) of the object, not to the 'start' or 'end' of it in their field of vision..
that said, the human perspective is subtle and convenient in completely different ways to how a computer manages memory, and this 'end-index' idea seems like a useful way to map the human whole-object perspective to a linear memory-index perspective
Re: Why do arrays start at 0?
#303Earlier quoted context omitted.
No. 1. Optimizing compilers exist. 2. Addressing at fixed offsets is cheap (a single instruction): https://en.wikipedia.org/wiki/Addressing_mode 0-based indexing is superior (as a default) because it simplifies a lot of common math, as discussed in TFA.
TFA = Thanks For Asking?
Re: Why do arrays start at 0?
#304Edsger Dijkstra wrote an interesting article titled 'why numbering should start at 0'. Perhaps not answering the question directly but an interesting read nonetheless. https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
Re: Why do arrays start at 0?
#305 fruits = ['apple'] \\ Hello I have fruits
x = len(fruits) \\ Only one kind tho lol
fruits[x] \\ OK here is what I have
SUBSCRIPT OUT OF RANGE
I have always hated zero indexing for this reason (other than thinking in assembler where it is beautiful). It is of course useful in many contexts, albeit with tradeoffs; more importantly everyone is used to it and it's predictable. But I was absolutely thrilled to discover Julia defaults to indexing from 1 like a normal person.Essentially what we have here is a mismatch between the tool (computer that does everything in binary) and the task (mathematical abstraction of quantities). Now it's completely understandable that people work within the limitations of the tool when there is no other choice, but that's the same sort of path dependency that creates technical debt.
If zero indexing were so great, mathematicians would have made it the default centuries if not millennia ago; but mathematicians don't want to do off-by-1 adjustments on all sorts of common operations because it makes things unnecessarily complicated. To be honest, I think this has become a moat to keep people out of programming even though it's a shallow one.
It could be interesting to test this, say by taking two classes of schoolchildren and teaching one Julia and the other Python (or...). By not having to take on the idea of zero-indexing at the same time as the concept of an array, the Julia group can get into collections using their intuitive understanding of the natural numbers. I expect that picking up language elements quickly will have a compounding effect and that at the end of the evaluation period the Julia group will be able to make significantly more complex programs than a control group.
Edit: in your heart you know I'm right
Re: Why do arrays start at 0?
#306Starting at one led to the whole "2000 is not the millennium" thing, as there is no year zero.
Re: Why do arrays start at 0?
#307I 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.
Re: Why do arrays start at 0?
#308Earlier quoted context omitted.
> Birthdays are anniversaries. Indeed they are, which is why I literally said so in the previous comment. Did you forget to finish reading it? > They do not include the event itself. That is true because the event itself is implied information. There is no value in speaking of it. If you stand before us, we can be certain that you had a day of birth (your 0th anniversary). We don't necessarily know how many times you…
If I care about apples (for my lunch, or my store), I care about the difference between having 0 of them and not having bothered to count yet. 0!=null
Re: Why do arrays start at 0?
#309I've always considered the index and the power to which a base has been raised to be equivalent in many circumstances.
Thus starting at 0 makes sense.
Re: Why do arrays start at 0?
#310Earlier 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.
Then the delta of both bounds (N – 1) wouldn’t equal the length of the range (N). The inclusive-exclusive convention is used in order for `end = start + length` to hold.
1
You only calculate the length when dealing with other than 0- or 1-based ranges. There, the inclusive-exclusive convention is very handy as you point out. So if we're fixing the initial offset at either 0 or 1, then use the appropriate convention for that offset. If we let the initial offset float, then the inclusive-exclusive makes sense.