There'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…
I don't quite understand what you're saying. If you change the code to for(uint8_t i = 0; i then it still doesn't halt.
Why numbering should start at zero (1982)
31–40 of 72 posts
Re: Why numbering should start at zero (1982)
#32Aside, Antony Jay cowrote of Yes Minister and Yes, Prime Minister.
https://en.wikipedia.org/wiki/Antony_Jay
I'm familiar with the shows but didn't recognize his name.
Re: Why numbering should start at zero (1982)
#33Earlier quoted context omitted.
These would be peculiarities of the C programming language, not issues of typed integers or of expression ranges. Other, actually-strictly-typed languages will complain that 256 doesn't fit in a byte, or if 256 is a larger sized integer, that automatic comparisons between differently sized integer types is not allowed.
Yes, but my point was that the solution is to use a closed range instead; in this case, use `i <= 255` as the loop condition. Another solution, of course, is to widen the type of your induction variable.
Re: Why numbering should start at zero (1982)
#34When I was learning QBasic some billion years ago I recall that I was happy with arrays generally starting at 1 (even though this is user specified in Basic). When I learned C I initially wasn't thrilled about re-learning arrays, but eventually grew to only like 0-based arrays. I'm thinking this is like people who drink coffee will cite studies saying it is good for you, and people who hate it will cite opposite stud…
my @arr = (1, 2, 3);
for my $i ($[...$#arr) {
...
}
It is however, advised not to change $[.Re: Why numbering should start at zero (1982)
#35Starting 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…
> Starting at 1 is more intuitive, and therefore IMO better. This is totally subjective. Starting at 0 is more intuitive to me. My intuition is very simple. `x` is the name of some memory location. `x[k]` is the location `k` spaces away from `x`.
Re: Why numbering should start at zero (1982)
#36Earlier quoted context omitted.
Matlab uses 1-based indexing. I've used it extensively and it was definitely a bad move. One particularly annoying example is splitting an array into blocks, e.g. 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... en…
I would reshape s since foo would anyway be vectorized. You could also do for i = 1:10:length(s) foo(s(i:i+9)) endfor With numpy the 9 turns into a 10 by the design choice that s[a:b] only goes to s[b-1], this is sometimes convenient, and sometimes it costs a +1.
Re: Why numbering should start at zero (1982)
#37Starting 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…
Intuition is the name we give to what we learned early. 0 and 1 are both perfectly fine starting points. They both have advantages and disadvantages. 0 is more common since a lot of languages use C as a starting point in some fashion. C chose 0 because then it can create syntactic sugar for sequential memory access via pointers. Or arrays.
Re: Why numbering should start at zero (1982)
#38I always like to point to the Julia package TwoBasedIndexing ( https://github.com/simonster/TwoBasedIndexing.jl/ ) which is IMHO provides the very best way to index an array. Yes, Julia defaults to 1-based indexing, and allows zero-based indexing ( https://github.com/JuliaArrays/OffsetArrays.jl ) or indeed whatever sort of indexing you want, yet I think the world is going to eventually come around to THE ONE TRUE ind…
Re: Why numbering should start at zero (1982)
#39Starting 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…
Having used Matlab a bunch, in practice this sucks, because “end” is treated as this weird special case in the language grammar, and many reasonable and convenient expectations of syntax that should work turn out not to. Folks writing complicated Matlab projects end up needing to work around it, and the workarounds are brittle and confusing.
Using negative integers is a whole lot easier to reason about and work with.
Everyone learns in grade school how to do arithmetic with negative integers (adopted by European mathematicians in the 17th century). Even Matlab experts don’t always understand Matlab ‘end’ arithmetic.
Re: Why numbering should start at zero (1982)
#40Earlier quoted context omitted.
> Starting at 1 is more intuitive, and therefore IMO better. This is totally subjective. Starting at 0 is more intuitive to me. My intuition is very simple. `x` is the name of some memory location. `x[k]` is the location `k` spaces away from `x`.
I can agree that's intuitive, but I'd like to point out that's not quite the same thing as numbering a sequence. If we have a sequence a_1, a_2, a3, ... we can talk about a_3 by calling it the third term. If we have a sequence a_0, a_1, a_2, ..., the third term is actually a_2. Whether or not we should index starting at 0 or 1 is probably dependent not only on intuition, but the application at hand. For most analytic…
It's still the "third" term. People commonly refer to the "zeroeth" term in a list as the "first" term, the "first" term as the "second", and so on. Admittedly, the usefulness of "zeroeth" depends on how often you think about the mechanics of array traversal, and that's probably not often if you don't program computers.