by BillGoates · 17y ago · 0 comments
He arguments that option A is the least ugly one, but forgets that computers have maximum numbers.
According to Dijkstra, you should test if a variable is a valid integer like: 0 <= var < MaxInt + 1. The best possible outcome of such a test would be a compiler error.
His argument that sequences defined as min <= i < (max + 1) because (max + 1) - min = total number of items is just silly. Maybe true for maths, not so for programmers. When reading code, you want to know if the code is valid, not how many items there are in a list. And reading i <= Max instead of i < (max + 1) is simpler.
Secondly, his article is about 0 or 1 based arrays, not random selections. And in case of 1 based arrays, the max = total number of elements.
So option C is best.
About whether 0 or 1 based arrays are better. They both have their uses. 0 based for spatial coordinates, 1 based for lists and character positions.
2) You frequently want to know the length of a subseq, and you frequently want successive subseq ranges to dovetail.
3) What in the world does rand() have to do with this?
2) Yes, but that has nothing to do with anything I wrote. Also calculating the length of a subset out of a 0 or 1 based array is the same, making your point completely irrelevant.
3) Where in the world do you read anything about rand()?
You really need to give up one of the possibilities, or use an extra bit.
The total number of items = maxint + 1
2) If you test the upper bound inclusively, then the upper bound of the previous subseq is not the lower bound of the next subseq.
3) I misinterpreted GP. Scratch that :)
2) Sorry, I don't have a clue what you mean. What's the difference between tests on subseqs from 0 or 1 based arrays?
"0 based for spatial coordinates, 1 based for lists and character positions."
Sums up my thoughts pretty well.
The real reason we do zero-based counting in computers is because of bits, speed and laziness.