Generating equally-spaced floats with least rounding error

Terry Reedy tjreedy at udel.edu
Sun Sep 25 02:07:25 EDT 2011


On 9/25/2011 1:21 AM, Steven D'Aprano wrote:
> 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." :)

I do hope you did not stop with my lead-in sentence, and read to the 
end, where I gave you most of the answer you were looking for, without 
using the fractions module.

-- 
Terry Jan Reedy




More information about the Python-list mailing list