I always found array index confusing. For example, given a array with size N in Python, we can iterate it from 0 to N-1 (onwards) and from -1 to -N (backwards), which is not consistent at all. Programming language is meant for human eyes. It would be better if array index being 1 to N, and let the compiler substract that 1 for us.
Numbering Should Start at Zero
41–50 of 58 posts
Re: Numbering Should Start at Zero
#42I don't know about arrays, but numbering floors starting from 1 is absurd. In most of Europe ground floor is 0, underground floors are negative, floors above the ground are positive numbers. Integers were invented for a reason, use them :)
Counting things (measuring how many you have) starts at 1 out of convenience. It makes no sense to talk about all the 0 things (that you don't have ) to start. Communication tends to be as succinct as possible.
In software, we often are tracking multiple things, so being explicit about how many offset or what an int holds, causes 0 to appear more frequently and "feels better" because the explicit communication is comprehensive.
Re: Numbering Should Start at Zero
#43Dijkstra have a certain way of writing as if his argument follows by logical necessity. But when you dig into the chain of reasoning, it hinges on the assertion that starting with 0 is "nicer". Which is of course a valid opinion, but starting with 1 also have nice properties, for example that the numbering of elements corresponds to the ordinal numbers. Having the first element be element 1 is pretty nice IMHO. He en…
Yes, I also had found that paper to be surprisingly and disappointedly hand wavey. "Don't meet your heroes" type of thing. Math does not care about pointer arithmetic and i in summation and induction starts at 1. Same in Julia. It's an higher level accommodation to machine code primitives, which are indeed less gate expensive if indexes start at zero.
Re: Numbering Should Start at Zero
#44Re: Numbering Should Start at Zero
#45Earlier quoted context omitted.
Yes, I also had found that paper to be surprisingly and disappointedly hand wavey. "Don't meet your heroes" type of thing. Math does not care about pointer arithmetic and i in summation and induction starts at 1. Same in Julia. It's an higher level accommodation to machine code primitives, which are indeed less gate expensive if indexes start at zero.
In addition to code, I think 0-indexing is also a nice convention for floor numbering. That way the Nth floor is N floors up from the ground level. This is already how basement levels work.
Re: Numbering Should Start at Zero
#46Earlier quoted context omitted.
In fact the vast majority of algorithms which involve doing computation on indexes beyond increment/decrement are naturally implemented with zero-based indexing. When using ones based indexing you end up having to convert to zero based, do the computation, then convert back to one based. Another common example of this is circular buffers. Computing the bounded index from an arbitrary index is simply i % c with zero b…
> Computing the bounded index from an arbitrary index is simply i % c with zero based indexing, but becomes (i-1) % c + 1 with one based indexing. Arguably, this is because the common definition of modular arithmetic is itself zero-based: “modulo N” maps all integers into the numbers [0,N-1]. It is fully possible to define a “%” operator that instead mapped the integers into the range [1,N], which might be more natur…
Re: Numbering Should Start at Zero
#47I always found array index confusing. For example, given a array with size N in Python, we can iterate it from 0 to N-1 (onwards) and from -1 to -N (backwards), which is not consistent at all. Programming language is meant for human eyes. It would be better if array index being 1 to N, and let the compiler substract that 1 for us.
One idea is a programming language that allows both, with different syntax. Say A[i] for 0 based and A{i} for 1 based.
Re: Numbering Should Start at Zero
#48Earlier quoted context omitted.
> Computing the bounded index from an arbitrary index is simply i % c with zero based indexing, but becomes (i-1) % c + 1 with one based indexing. Arguably, this is because the common definition of modular arithmetic is itself zero-based: “modulo N” maps all integers into the numbers [0,N-1]. It is fully possible to define a “%” operator that instead mapped the integers into the range [1,N], which might be more natur…
Great, now you've messed up all the other math that uses the modulo operator. The mathematical operators behave the way they do for well established reasons that long predate the invention of computers. It's going to be a tough sell to get everyone to adopt a wholesale refactoring of modulo arithmetic (and likely number theory in general) just for the "convenience" of one based indexing.
Re: Numbering Should Start at Zero
#49Earlier quoted context omitted.
Yes, I also had found that paper to be surprisingly and disappointedly hand wavey. "Don't meet your heroes" type of thing. Math does not care about pointer arithmetic and i in summation and induction starts at 1. Same in Julia. It's an higher level accommodation to machine code primitives, which are indeed less gate expensive if indexes start at zero.
In addition to code, I think 0-indexing is also a nice convention for floor numbering. That way the Nth floor is N floors up from the ground level. This is already how basement levels work.
Re: Numbering Should Start at Zero
#50Earlier quoted context omitted.
One idea is a programming language that allows both, with different syntax. Say A[i] for 0 based and A{i} for 1 based.
This sounds like a really good idea... if you want to hide subtle backdoors in innocuous-looking code.