[Python-Dev] range objects in 3.x

Guido van Rossum guido at python.org
Tue Sep 27 20:53:39 CEST 2011


On Tue, Sep 27, 2011 at 11:48 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Tue, Sep 27, 2011 at 2:36 PM, Guido van Rossum <guido at python.org> wrote:
> ..
>> But that's exactly what we don't *want* you to do! Because (unless you
>> are a numerical wizard) you probably aren't doing the error analysis
>> needed to avoid the "unexpected extra point" problem due to floating
>> point inaccuracies. For your own good, we want you to state the count
>> and let us deliver the number of points you want.
>
> But the likely result will be that a non-wizard will find that range()
> does not work with floats, reach for some_name_here(), find the
> absence of step option, curse the developers, write
> count=int((stop-start)/step) and leave this with a nagging thought
> that (s)he forgot +/-1 somewhere.

But the *user* can just force this to round by using
int((stop-start+0.5)/step) or by using int(round()); either of these
is an easy pattern to teach and learn and useful in many other places.

The problem is that frange() cannot do that rounding for you, since
its contract (if it is to be analogous to range() at all) is that
there is no assumption that stop is anywhere close to start + a
multiple of step.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list