4 ms·
One big advantage of zero-based indexing is that it's consistent with division with remainder. This is especially nice when working with multidimensional arrays
by fdej 11y ago
One big advantage of zero-based indexing is that it's consistent with division with remainder. This is especially nice when working with multidimensional arrays.
- TheLoneWolfling 11y agoI'd argue that in the presence of one-based indexing, remainders being defined as 1..n, inclusive, makes more sense anyways.
- shasta 11y agoSo 8 divided by 4 is 1 with a remainder of 4?
- TheLoneWolfling 11y agoNo. Two with a "remainder" of 4. Ideally I'd call it by another name to avoid confusion (and the implication that x == x//y * y + x % y - although this is often incorrect in the case of negative values anyways), but I do not know what other "better" name to call it. Think about it. If you have a series of bins with 4 items each, and you ask someone to grab the 8th item, where do they go? To the 4th item of the second bin.
- shasta 11y agoLooking at division and mod as a pair finding group and subgroup indices is interesting. For zero based indexing, we want division to round down and the usual zero-based mod. So Group(8,4) = 8/4 = 2, Index(8,4) = 8 % 4 = 0. The invariant is x == x/y * y + x % y. With one based indexing, we want division to round up and want the modulo operator you're describing. So Group(8,4) = 8/'4 = 2, Index(8,4) = 8 %' 4 = 4. The invariant is x == (x/'y - 1) * y + x %' y.
- jfarmer 11y agoAlthough it might make sense to define the modulus operator in that way, it doesn't really make sense to define remainders that way: what is the remainder of n/n?
- TheLoneWolfling 11y agoAs I said in another comment: It ideally shouldn't be called a remainder. I just don't know what else to call it. In this system, n % n is n. (Although note that I'm not even going to try to get into negative values here. "Standard" modulus is bad enough for this - this operator has the same potential choices as "standard" modulo w.r.t. negative values / mods) If you have 1 bin with n items, the nth item is the nth item within the bin within which it is contained. (I hope that's parsable.)
- Retra 11y agoThat's still a remainder, it's just the remainder of the zeroth bin.
- TheLoneWolfling 11y agoNo. One-based indexing everywhere. So 8 / 4 is 2 with a "remainder" of 4. Think about it. If you have a series of bins with 4 items each, and you ask someone to grab the 8th item, where do they go? To the 4th item of the second bin.
- Retra 11y agoThe fourth item of the second bin is the fourth item of the remaining items after you've looked at one bin.
- TheLoneWolfling 11y agoBut it is contained in the second bin.