1
\name{sum.exact, cumsum.exact & runsum.exact}
5
\title{Basic Sum Operations without Round-off Errors}
7
Functions for performing basic sum operations without round-off errors
10
sum.exact(..., na.rm = FALSE)
16
\item{x}{numeric vector}
17
\item{...}{numeric vector(s), numbers or other objects to be summed}
18
\item{na.rm}{logical. Should missing values be removed?}
19
\item{k}{width of moving window; must be an odd integer between one and n }
23
All three functions use full precision summation using multiple doubles for
24
intermediate values. The sum of numbers x & y is a=x+y with error term
25
b=error(a+b). That way a+b is equal exactly x+y, so sum of 2 numbers is stored
26
as 2 or fewer values, which when added would under-flow. By extension sum of n
27
numbers is calculated with intermediate results stored as array of numbers
28
that can not be added without introducing an error. Only final result is
29
converted to a single number
33
Function \code{sum.exact} returns single number. Function \code{cumsum.exact}
34
returns vector of the same length as \code{x}. Function \code{runsum.exact}
35
returns vector of length \code{length(x)-k} and attribute "count" containing
36
number of finite (as in \code{\link{is.finite}}) elements in each window.
40
Round-off error correction is based on:
41
Shewchuk, Jonathan, \emph{Adaptive Precision Floating-Point Arithmetic and
42
Fast Robust Geometric Predicates},
43
\url{http://www-2.cs.cmu.edu/afs/cs/project/quake/public/papers/robust-arithmetic.ps}
44
and its implementation found at:
45
\url{http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090}
47
McCullough, D.B., (1998) \emph{Assessing the Reliability of Statistical
48
Software, Part I}, The American Statistician, Vol. 52 No 4,
49
\url{http://www.amstat.org/publications/tas/mccull-1.pdf}
51
McCullough, D.B., (1999) \emph{Assessing the Reliability of Statistical
52
Software, Part II}, The American Statistician, Vol. 53 No 2
53
\url{http://www.amstat.org/publications/tas/mccull.pdf}
55
NIST Statistical Reference Datasets (StRD) website
56
\url{http://www.nist.gov/itl/div898/strd}
60
\author{Jarek Tuszynski (SAIC) \email{jaroslaw.w.tuszynski@saic.com} based on
61
code by Vadim Ogranovich, which is based on algorithms described in
62
references, pointed out by Gabor Grothendieck.
67
\item \code{\link[base]{sum}} is faster but not error-save version of
69
\item \code{\link[base]{cumsum}} is equivalent to \code{cumsum.exact}
70
\item \code{\link{runmean}(x,k,endrule="trim")} is similar to
76
x = c(1, 1e20, 1e40, -1e40, -1e20, -1)
78
b = sum.exact(x); print(b)
80
a = cumsum(x); print(a)
81
b = cumsum.exact(x); print(b)
88
\concept{sum with no round-off errors}
89
\concept{cumulative sum with no round-off errors}
90
\concept{moving sum with no round-off errors}
91
\concept{rolling sum with no round-off errors}
92
\concept{running sum with no round-off errors}