Learn R Programming

caTools (version 1.5)

sum.exact: Basic Sum Operations without Round-off Errors

Description

Functions for performing basic sum operations without round-off errors

Usage

sum.exact(..., na.rm = FALSE)
  cumsum.exact(x)
  runsum.exact(x,k)

Arguments

x
numeric vector
...
numeric vector(s), numbers or other objects to be summed
na.rm
logical. Should missing values be removed?
k
width of moving window; must be an odd integer between one and n

Value

  • Function sum.exact returns single number. Function cumsum.exact returns vector of the same length as x. Function runsum.exact returns vector of length length(x)-k and attribute "count" containing number of finite (as in is.finite) elements in each window.

Details

All three functions use full precision summation using multiple doubles for intermediate values. The sum of numbers x & y is a=x+y with error term b=error(a+b). That way a+b is equal exactly x+y, so sum of 2 numbers is stored as 2 or fewer values, which when added would under-flow. By extension sum of n numbers is calculated with intermediate results stored as array of numbers that can not be added without introducing an error. Only final result is converted to a single number

References

Round-off error correction is based on: Shewchuk, Jonathan, 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 and its implementation found at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090 McCullough, D.B., (1998) Assessing the Reliability of Statistical Software, Part I, The American Statistician, Vol. 52 No 4, http://www.amstat.org/publications/tas/mccull-1.pdf McCullough, D.B., (1999) Assessing the Reliability of Statistical Software, Part II, The American Statistician, Vol. 53 No 2 http://www.amstat.org/publications/tas/mccull.pdf NIST Statistical Reference Datasets (StRD) website http://www.nist.gov/itl/div898/strd

See Also

  • sum.exact- is equivalent tosum
  • cumsum.exact- is equivalent tocumsum
  • runsum.exact- is similar torunmean (x,k,endrule="trim")

Examples

Run this code
x = c(1, 1e20, 1e40, -1e40, -1e20, -1)
  a = sum(x);          print(a)
  b = sum.exact(x);    print(b)
  stopifnot(b==0)
  a = cumsum(x);       print(a)
  b = cumsum.exact(x); print(b)
  stopifnot(b[6]==0)

Run the code above in your browser using DataLab