On 2007-02-20, John Machin <[EMAIL PROTECTED]> wrote: > On Feb 21, 2:05 am, Grant Edwards <[EMAIL PROTECTED]> wrote: >> On 2007-02-20, joanne matthews (RRes-Roth) <[EMAIL PROTECTED]> 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.
No matter what radix you choose, you're going to be able to exactly represent 0% of the rational numbers within the range of the representation. Since you have no control over the FP representation (and hence radix), and little control over input values, the only sane thing to do is to write your code under the assumption that FP can't represent any values exactly. > Another take: Don't expect exact results. Which is what I said. :) > 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. In this case the input values have about 14 significant digits. So does the output. Unfortunately, the algorithm as written requires an infinite number of significant digits. -- Grant Edwards grante Yow! WHOA!! Ken and at Barbie are having TOO visi.com MUCH FUN!! It must be the NEGATIVE IONS!! -- http://mail.python.org/mailman/listinfo/python-list