Generating equally-spaced floats with least rounding error

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Sep 24 19:01:47 CEST 2011


Mark Dickinson wrote:

> Using Fraction for intermediate calculations actually works perfectly
> for this, since conversions from float to Fraction are exact, while
> conversions from Fraction to float are correctly rounded.  So if
> you're using Python, you're not too bothered about efficiency, and you
> want provably correctly-rounded results, why not use Fraction?
> 
>>>> from fractions import Fraction
>>>> start, stop, n = 0.0, 2.1, 7
>>>> [float(Fraction(start) + i * (Fraction(stop) - Fraction(start)) / n)
>>>> [for i in range(n+1)]
> [0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1]


Ah, I knew it was too easy!

>>> from fractions import Fraction as F
>>> start, stop, n = 1, 3.1, 7
>>> [float(F(start) + i*(F(stop)-F(start))/n) for i in range(n+1)]
[1.0, 1.3, 1.6, 1.9000000000000001, 2.2, 2.5, 2.8000000000000003, 3.1]
>>>
>>> start, stop, n = -1, 1.1, 7
>>> [float(F(start) + i*(F(stop)-F(start))/n) for i in range(n+1)]
[-1.0, -0.7, -0.39999999999999997, -0.09999999999999996,
0.20000000000000004, 0.5000000000000001, 0.8, 1.1]



-- 
Steven




More information about the Python-list mailing list