summation {cmna}R Documentation

Two summing algorithms

Description

Find the sum of a vector

Usage

naivesum(x)

kahansum(x)

pwisesum(x)

Arguments

x

a vector of numbers to be summed

Details

naivesum calculates the sum of a vector by keeping a counter and repeatedly adding the next value to the interim sum. kahansum uses Kahan's algorithm to capture the low-order precision loss and ensure that the loss is reintegrated into the final sum. pwisesum is a recursive implementation of the piecewise summation algorithm that divides the vector in two and adds the individual vector sums for a result.

Value

the sum

Examples

k <- 1:10^6
n <- sample(k, 1)
bound <- sample(k, 2)
bound.upper <- max(bound) - 10^6 / 2
bound.lower <- min(bound) - 10^6 / 2
x <- runif(n, bound.lower, bound.upper)
naivesum(x)
kahansum(x)
pwisesum(x)

[Package cmna version 1.0.5 Index]