[Python-Dev] range objects in 3.x

Steven D'Aprano steve at pearwood.info
Sun Sep 25 07:21:06 CEST 2011


Guido van Rossum wrote:
> On Fri, Sep 23, 2011 at 7:13 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>>>> http://code.activestate.com/recipes/577068-floating-point-range/
>>> I notice that your examples carefully skirt around the rounding issues.
>> I also carefully *didn't* claim that it made rounding issues disappear
>> completely. I'll add a note clarifying that rounding still occurs and as a
>> consequence results can be unexpected.
> 
> I believe this API is fundamentally wrong for float ranges, even if
> it's great for int ranges, and I will fight against adding it to the
> stdlib in that form.

I wasn't proposing it to be in the standard lib, it was just an idle 
comment triggered by the OP's question. But I'm gratified it has started 
an interesting discussion.

Whether the most float-friendly or not, the start/stop/step API is the 
most obvious and user-friendly for at least one use-case: graphing of 
functions.

It is natural to say something like "draw a graph starting at 0, 
sampling every 0.1 unit, and stop when you get past 3". My HP-48 
graphing calculator does exactly that: you must specify the start and 
stop coordinates, and an optional step size. By default, the step size 
is calculated for you assuming you want one point plotted per pixel. 
Given that the calculator display is both low-resolution and fixed size, 
that makes sense as the default, but you can set the step size manually 
if desired.

start/stop/step is also familiar for users of Excel and other 
spreadsheets' Fill>Series command.

Numeric integration is an interesting case, because generally you want 
multiple iterations, interpolating between the points previously seen 
until you reach some desired level of accuracy. E.g.:

#1:  0.0, 0.5, 1.0
#2:  0.25, 0.75
#3:  0.125, 0.375, 0.625, 0.875

For integration, I would probably want both APIs.


> Maybe we can come up with a better API, and e.g. specify begin and end
> points and the number of subdivisions? 

Thanks to Mark Dickinson for suggesting using Fraction, I have this:

http://code.activestate.com/recipes/577878-generate-equally-spaced-floats/



-- 
Steven


More information about the Python-Dev mailing list