Generating equally-spaced floats with least rounding error

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Sep 25 01:21:31 EDT 2011


Terry Reedy wrote:

> On 9/24/2011 9:53 AM, Steven D'Aprano wrote:
[...]
>>>>> [0.0 + i*width for i in range(8)]
>> [0.0, 0.3, 0.6, 0.8999999999999999, 1.2, 1.5, 1.7999999999999998, 2.1]
>>
>> The 4th and 7th values have rounding errors, the rest are exact
> 
> No they are not. Their errors are just smaller and not visible with 16
> digits.

Pardon. I meant that they were as close to exact as is possible in binary
floats. With the exception of 0.8999... and 1.7999... I don't believe any
other float can be closer to the exact value.

I did mention that "If the exact value isn't representable as a float, I'm
okay with returning the nearest possible float." :)



-- 
Steven




More information about the Python-list mailing list