Why numbering should start at zero (1982)
cs.utexas.edu
Why numbering should start at zero (1982)
1–10 of 72 posts
Re: Why numbering should start at zero (1982)
#2 for(uint8_t i = 0; i
What's even worse, the following loop has undefined behavior because signed integer overflow is not defined: for(int8_t i = 0; i Re: Why numbering should start at zero (1982)
#3There's one important use case for Dijkstra's alternative (c), that is, an interval closed at both ends. That is when your set has a maximum element and you want to be able to express an interval including the maximum. This is, of course, important in any programming language whose basic integer types have a bounded range. For instance, the following loop in C never halts: for(uint8_t i = 0; i What's even worse, the…
Re: Why numbering should start at zero (1982)
#4Re: Why numbering should start at zero (1982)
#5There's one important use case for Dijkstra's alternative (c), that is, an interval closed at both ends. That is when your set has a maximum element and you want to be able to express an interval including the maximum. This is, of course, important in any programming language whose basic integer types have a bounded range. For instance, the following loop in C never halts: for(uint8_t i = 0; i What's even worse, the…
For example, Python:
for i in range(0, 256):
Rust: for i in 0..256 {
and many other languages have similar things.Proper ranges also mean that the compiler never needs to do complicated reasoning that may depend on signed integer overflow having undefined behavior in order to prove that the loop has a finite number of iterations or that `i` doesn't wrap around.
Re: Why numbering should start at zero (1982)
#6Re: Why numbering should start at zero (1982)
#7Regarding some of the advantages of zero-based:
- Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops
being an advantage.
- Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me.
I commit less off-by-one errors with 1-based, and I don't have to double-check as much -- so on balance, I prefer it.[edit]
The amount of karma this comment is getting is undergoing something like Brownian motion.
Re: Why numbering should start at zero (1982)
#8Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops being an advantage. - Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me. I commit less off-by-one errors with 1-based, and I don't have to double-che…
Re: Why numbering should start at zero (1982)
#9Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops being an advantage. - Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me. I commit less off-by-one errors with 1-based, and I don't have to double-che…
for i = 1:numel(s)
foo(s[i])
end
That is fine, but what if you want to process in blocks of N? Now you have to do: for i = 1:(numel(s)/10)
foo(s[(i-1)*N + (1:N)])
end
Ugh no thanks. This is as simple as it gets too. For more complex array manipulation... enjoy adding and subtracting your 1's. 0-based indexing is just way more natural.Re: Why numbering should start at zero (1982)
#10There's one important use case for Dijkstra's alternative (c), that is, an interval closed at both ends. That is when your set has a maximum element and you want to be able to express an interval including the maximum. This is, of course, important in any programming language whose basic integer types have a bounded range. For instance, the following loop in C never halts: for(uint8_t i = 0; i What's even worse, the…
This is arguably a limitation of C-style for loops. For example, Python: for i in range(0, 256): Rust: for i in 0..256 { and many other languages have similar things. Proper ranges also mean that the compiler never needs to do complicated reasoning that may depend on signed integer overflow having undefined behavior in order to prove that the loop has a finite number of iterations or that `i` doesn't wrap around.