Prev: Is there any way to minimize str()/unicode() objects memory usage[Python 2.6.4] ?
Next: sched() function questions
From: Lie Ryan on 13 Aug 2010 14:43 Sorry the message gets cuts off by an accidental press of send button. On 08/14/10 04:31, Lie Ryan wrote: > On 08/10/10 06:36, Bartc wrote: >> And if the context is Python, I doubt whether the choice of 0-based over a >> 1-based makes that much difference in execution speed. > > And I doubt anyone cares about execution speed when deciding whether to > use 1-based or 0-based array. The reason why you want to choose the > alternative that use less conversion to the other system is to simplify > the source code. > > Many common mathematical/physics/economics formulas are expressed much > simply if we use 0-based counting: > > * arithmetic series: > - 1-based: s(n) = a + (n - 1) * d > - 0-based: s(n) = a + n * d > * geometric series: > - 1-based: g(n) = a * r**(n - 1) > - 0-based: g(n) = a * r**n > * equation of motion: > - 1-based: x(t) = a + 1/2 * a * (t - 1)**2 > - 0-based: x(t) = a + 1/2 * a * t**2 > * exponential growth/decay: > - 1-based: d(t) = a * e**(k * (t - 1)) > - 0-based: d(t) = a * e**(k*t) > > > In fact, most textbooks would already uses 0-based formula for some of > these formulas already. Most physics and economic textbooks would show > the base 0 variation of the formula, and refers to t=0 as the point in > time where the "event" started. > > I often used this model of thinking for 0-based array indices (and > negative indices): > > -7 -6 -5 -4 -3 -2 -1 > +---+---+---+---+---+---+---+ > | c | h | a | r | l | i | e | > +---+---+---+---+---+---+---+ > 0 1 2 3 4 5 6 (7) > > instead of: > so to repeat, I often use this model of thinking: -7 -6 -5 -4 -3 -2 -1 +---+---+---+---+---+---+---+ | c | h | a | r | l | i | e | +---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 (7) instead of: -7 -6 -5 -4 -3 -2 -1 +---+---+---+---+---+---+---+ | c | h | a | r | l | i | e | +---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 that is, the indices refers to the "gap" between the array entries. The "gap index" model highlights the naturalness of using 0-based array, negative indices, array slicing, and half-open.
From: Terry Reedy on 13 Aug 2010 18:09 On 8/13/2010 11:27 AM, Den wrote: > > I smile every time I see the non-nonsensical sentence "The first > thing, therefore, is in thing[0]" in a programming language learning > book or tutorial. I laugh every time I hear someone defend that as > common sense. If one thinks in terms of slicing at gap positions, the 'proper' indexes would range from 0.5 (average of 0 and 1) to n-0.5. For convenience, we round down or up. To put it another way, seq[n:n+1] is abbreviated as either seq[n] or seq[n+1]. Put this way, the first choice is at least as sensible as the second. Given that Python allows indexing from both end, I prefer 0,1,2,... and -1,-2,-3,... to 1,2,3... and 0,-1,-2,... or 1,2,3,... and -1,-2,-3. As someone else pointed out, discretizing a continuous variable starting at 0 gives 0,1,2,... so having indexes that match is handy. If a problem is formulated in terms of 1,2,3, one can simply leave the first cell blank rather than reformulate. If a problem is formulated in terms of 0,1,2,... and indexes are 1 based, then one must reformulate. > Every three year old watching Sesame Street knows > counting things starts with '1', not '0'. And that is the same mistake that most societies make, the mistake that put a lid on Greak math, science, and finance. All fresh counts begin with 0. Counting by people usually begins with a silent 0, just as fresh tallies begin with a blank stick or paper. But not always. For instance, lets count the people who have, up to noe, become billionaires with Python. We start with an overt 0. Now we can discuss whether the founders of Google should increase that to 2. Mechanical counting requires an overt 0. A car odometer starts at 0, not 1 and not . Have you never written a counting program? Starting with n = 1 instead of n = 0 before counting the first item would be a bad bug. > There may be loads of reasons for it, but don't throw common sense > around as one of them. I won't. Only a few (about 3 or 4) societies included a proper 0 in their number systems. -- Terry Jan Reedy
From: Ian Kelly on 13 Aug 2010 19:10
On Fri, Aug 13, 2010 at 11:53 AM, Martin Gregorie <martin(a)address-in-sig.invalid> wrote: > In a higher level language 1-based indexing is just as limiting as 0- > based indexing. What you really want is the ability to declare the index > range to suit the problem: in Algol 60 it is very useful to be able to > declare something like: > > real sample[-500:750]; Ugh, no. The ability to change the minimum index is evil. I don't much care whether a high-level language uses 0-based or 1-based indexing, but I do care that it is consistent. On the occasions when I am forced to use Visual Basic, the single biggest wart that drives me up a wall is constantly having to figure out whether the particular thing that I am currently indexing is 0-based or 1-based. Cheers, Ian |