5 ms·
I doubt it, but even if you're right, 1-based indexing wouldn't eliminate off-by-one errors. They're easy to make regardless. Ask ten people to implement binar
by solipsism 10y ago
I doubt it, but even if you're right, 1-based indexing wouldn't eliminate off-by-one errors. They're easy to make regardless.
Ask ten people to implement binary search with 1-based indexing, for example. Watch the off-by-ones roll in.
In fact, from https://en.m.wikipedia.org/wiki/Zero-based_numbering https://en.m.wikipedia.org/wiki/Zero-based_numbering
With zero-based numbering, a range can be expressed as the half-open interval, [0,n), as opposed to the closed interval, [1,n]. Empty ranges, which often occur in algorithms, are tricky to express with a closed interval without resorting to obtuse conventions like [1,0]. Because of this property, zero-based indexing potentially reduces off-by-one and fencepost errors.
- Sean1708 10y agoI find that statement very suspect, and I highly doubt there is any data to back it up. In my opinion [1,0] is far more obviously an empty range than [1,1) but that's just what it is, my opinion. And I suspect what we are seeing in that Wikipedia article is the author's opinion, not anything backed up by facts or data. Edit: Lol, the Wikipedia article cites Dijkstra's article.
- JadeNB 10y ago> In my opinion [1,0] is far more obviously an empty range than [1,1) but that's just what it is, my opinion. On its own, I think that's a reasonable contention, but it doesn't work when combing intervals: [1, 1) combined with [1, 2) is still [1, 2), but [1, 0] combined with [0, 2] isn't [1, 2].