Generating equally-spaced floats with least rounding error

Vlastimil Brom vlastimil.brom at gmail.com
Sat Sep 24 16:17:40 CEST 2011


2011/9/24 Chris Angelico <rosuav at gmail.com>:
> On Sat, Sep 24, 2011 at 11:53 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> #1 add multiples of the width to the starting value, 0.
>>
>> #2 subtract multiples of width from the ending value, 2.1.
>
> #3, go half and half - generate the lower half by adding to the lower
> bound, and the upper half by subtracting from the upper bound. Not
> sure if it'll help or not but it might be worth a shot.
>
> ChrisA
> --
Just a naive way:
#4 compute the values in both directions and use the average of the
results (of, course, given, there isn't a better way to distinguish
the values showing rounding errors automatically).

(I guess dealing manually with values obtained by .as_integer_ratio()
doesn't count as pure float operation...)

   vbr



More information about the Python-list mailing list