Class Adder

java.lang.Object
org.bzdev.math.Adder
Direct Known Subclasses:
Adder.Kahan, Adder.Pairwise

public abstract class Adder extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static 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 Type
    Method
    Description
    abstract 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(Iterable<? extends Number> iterable)
    Add the values produced by an Iterable to the summation.
    abstract double
    Get the sum.
    abstract void
    Reset the class to sum a new set of values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public abstract void add(double[] array, int start, int end) throws IllegalArgumentException
      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 sum
      start - 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

      public abstract void add(Iterable<? extends Number> iterable)
      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.