fsum | R Documentation |
This method avoids loss of precision by tracking multiple intermediate partial sums. Based on python's math.fsum
fsum(numbers)
numbers |
A vector of numbers to sum. |
Sum of numbers without loss of precision
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.
Matthew Fidler (R implementation), Raymond Hettinger, Jonathan Shewchuk, Python Team
https://docs.python.org/2/library/math.html https://github.com/python/cpython/blob/a0ce375e10b50f7606cb86b072fed7d8cd574fe7/Modules/mathmodule.c
Shewchuk, JR. (1996) Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates. http://www-2.cs.cmu.edu/afs/cs/project/quake/public/papers/robust-arithmetic.ps
sum(c(1,1e100,1,-1e100)) ## Should be 2, gives 0
fsum(c(1,1e100,1,-1e100)) ## Gives 2.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.