java.lang.Object
org.bzdev.math.Adder
- Direct Known Subclasses:
Adder.Kahan
,Adder.Pairwise
Class representing a summation.
Subclasses provide various algorithms.
The naive algorithm for summing a series of values is subject to relatively large floating-point errors when a large number of values are summed. This class provides summation algorithms that substantially reduce errors. Multiple algorithms are provided to allow accuracy to be traded off for performance.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Add a series of values using the Kahan summation algorithm.static final class
Add a series of numbers using pairwise summation. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
add
(double value) Add a single number to the summation.abstract void
add
(double[] array) Add the elements of an array to the summation.abstract void
add
(double[] array, int start, int end) Add the elements of an array to the summation, specifying an offset into the array and the number of elements to process.abstract void
Add the values produced by an Iterable to the summation.abstract double
getSum()
Get the sum.abstract void
reset()
Reset the class to sum a new set of values.
-
Method Details
-
add
public abstract void add(double[] array) Add the elements of an array to the summation.- Parameters:
array
- elements to add to the sum
-
add
Add the elements of an array to the summation, specifying an offset into the array and the number of elements to process. The indices of the elements added are members of the interval [start,end).- Parameters:
array
- the array of elements to sumstart
- the starting index (inclusive)end
- the ending index (exclusive)- Throws:
IllegalArgumentException
- the start and end values are not consistent with the array's size or are out of range or out of order
-
add
public abstract void add(double value) Add a single number to the summation.- Parameters:
value
- the value to add
-
add
Add the values produced by an Iterable to the summation. This method will add the elements of any object that can be used in a 'foreach' statement (the "for (TYPE value: object)" syntax) when TYPE is a subclass of Number (e.g., Double, Float, etc.)- Parameters:
iterable
- the Iterable whose values will be summed
-
getSum
public abstract double getSum()Get the sum. Additional values can be subsequently added.- Returns:
- the sum
-
reset
public abstract void reset()Reset the class to sum a new set of values.
-