[Python-Dev] range objects in 3.x

Guido van Rossum guido at python.org
Sun Sep 25 16:38:55 CEST 2011


On Sat, Sep 24, 2011 at 10:21 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Guido van Rossum wrote:
>> 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 *appears* that. But the flaws make for hard-to-debug edge cases
(when an extra point unexpectedly appears). I've debugged a few bits
of charting code, and there are enough other causes for confusing
output that we don't need this problem.

> 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.

Yeah, but the HP uses decimal internally.

It's just as easy for the user to specify the number of steps, and it
has the advantage of not having the edge case problems. And you know
how many points you'll get.

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

Not sure I want to follow Excel's example for *anything*.

> 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

So double the number of steps each time. Seems simpler to me
(manipulating ints instead of floats).

> 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/

Nice one!

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


More information about the Python-Dev mailing list