summation: Two summing algorithms

Description Usage Arguments Details Value Examples

Description

Find the sum of a vector

Usage

1
2
3
4
5

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

1
2
3
4
5
6
7
8
9
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)

Example output

[1] -59243315009
[1] -59243315009
[1] -59243315009

cmna documentation built on July 14, 2021, 5:11 p.m.