[Python-Dev] range objects in 3.x

Georg Brandl g.brandl at gmx.net
Sat Sep 24 08:55:19 CEST 2011


Am 24.09.2011 04:40, schrieb Guido van Rossum:
> 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.
> 
> Maybe we can come up with a better API, and e.g. specify begin and end
> points and the number of subdivisions? E.g. frange(0.0, 2.1, 3) would
> generate [0.0, 0.7, 1.4].

This is what numpy calls linspace:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

numpy also has an "arange" that works with floats, but:
"""When using a non-integer step, such as 0.1, the results will often not be
consistent. It is better to use linspace for these cases."""

Georg



More information about the Python-Dev mailing list