On 16/10/21 18:41, David G. Johnston wrote:
> On Friday, October 15, 2021, Ron <ronljohnsonjr@gmail.com> wrote:
>
>
> Prima facie, if you were told "numbers in the range 0-10", would
> you really think, "ah, they *really* mean 0 through 9"?
>
>
> I would indeed default to both endpoints of the range being
> inclusive. I also begin counting at one, not zero. I’ve long gotten
> past being surprised when computer science and my defaults don’t
> agree. Choices are made and documented and that works for me.
>
> As for this, documentation I never really gave the wording a second
> thought before, though I can definitely understand the complaint and
> like the somewhat wordier, but less linguistically challenging,
> phrasing the OP suggested (Boundary point, especially by itself, is
> not an improvement).
>
> David J.
>
The reason arrays generally start at zero and not one, is efficiency.
When indexes are zero based then the displacement in bytes from the
start address of x[n] is simply:
startAddress + n * sizeOfElement
If the start of an array had the index of one, then you have subtract
one each time, so the displacement from the start address of x[n] now
becomes
startAddress + (n - 1) * sizeOfElement
Half open intervals make life a lot simpler so it is the natural
default, to prevent intervals from having any numbers in common.
If you have 3 intervals spanning the range [0, 30), and you are only
dealing with integers then you can split the range as:
[0, 9] 0 <= x <= 9
[10, 19] 10 <= x <= 19
[20, 29] 10 <= x <= 29
But what if you are dealing with floats? The above arrangement would not
work, as 9.78 would not be in any interval, so you need half open
intervals, such as:
[0, 10) 0 <= x < 10
[10, 20) 10 <= x < 20
[20, 30) 10 <= x < 30
So you know what number each interval starts at, and every number in the
range is covered.
-Gavin