Generating equally-spaced floats with least rounding error

Mark Dickinson dickinsm at
Sat Sep 24 18:55:35 CEST 2011

On Sep 24, 5:46 pm, Steven D'Aprano <steve
+comp.lang.pyt... at> wrote:
> Speed is important, but secondary to correctness. To be honest, I never even
> thought of using fractions as an intermediate result -- I was thinking of
> generating lists of Fractions. I think I can use this approach.

Speed could be improved greatly by using integer arithmetic (with some
help from the float.as_integer_ratio method) instead of using Fraction
directly, though it would require writing quite a lot more code;
Fraction's many and slow gcd calls for normalization are a problem
here, and since all denominators will be powers of 2 they're largely

> But out of curiosity, how would you do it using nothing but floats? Is there
> a way?

Hmm.  Beyond writing my own multiple-precision integer library using
floats as the base type, I can't think of anything obvious. :-)


More information about the Python-list mailing list