Add an example for math.fsum() and elaborate on the accurary note.

This commit is contained in:
Raymond Hettinger 2009-02-19 05:48:05 +00:00
parent cdf8ba369b
commit f3936f875a
1 changed files with 10 additions and 6 deletions

View File

@ -80,14 +80,18 @@ Number-theoretic and representation functions
.. function:: fsum(iterable)
Return an accurate floating point sum of values in the iterable. Avoids
loss of precision by tracking multiple intermediate partial sums. The
algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the
typical case where the rounding mode is half-even.
loss of precision by tracking multiple intermediate partial sums::
.. note::
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.99999999999999989
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
The accuracy of fsum() may be impaired on builds that use
extended precision addition and then double-round the results.
The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the
typical case where the rounding mode is half-even. On some non-Windows
builds, the underlying C library uses extended precision addition and may
occasionally double-round an intermediate sum causing it to be off in its
least significant bit.
.. function:: isinf(x)