Weird result returned from adding floats depending on order I add them

John Machin sjmachin at lexicon.net
Tue Feb 20 10:22:24 EST 2007


On Feb 21, 2:05 am, Grant Edwards <gra... at visi.com> wrote:
> On 2007-02-20, joanne matthews (RRes-Roth) <joanne.matth... at bbsrc.ac.uk> wrote:
>
> > I'm getting different results when I add up a list of floats depending
> > on the order that I list the floats.

>
> Don't use floating point if you expect exact results.

It's not the floating point that's the problem, it's the radix, in
this case 2, not being able to express n/10 exactly. As the tutorial
points out, radix-10 has problems representing n/3 (and n/7 and ...)
exactly.

Another take: Don't expect exact results. If the input is exact to 1
or two decimal places, don't expect the sum to be exact to 15 or more
decimal places.




More information about the Python-list mailing list