Created
June 9, 2016 07:53
-
-
Save tdebatty/315263abe00150bc4833e135a8f6cc31 to your computer and use it in GitHub Desktop.
Helper class to perform the sum of a large number of values (sum aggregation).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
/** | |
* Helper class to perform the sum of a large number of values. | |
* A double value in java only has 15 significant digits. Hence, when performing | |
* the sum of a large number of double values, the precision of the result | |
* may be highly altered. For example, if performing the sum of 1E9 values, | |
* the result only has 6 significant digits. This helper class performs the | |
* addition using groups of 1E5 values. The result has 10 significant digits, | |
* for sums of up to 1E10 values. | |
* @author Thibault Debatty | |
*/ | |
public class SumAggregator { | |
private static final int CAPACITY = 100000; | |
private double value = 0; | |
private ArrayList<Double> stack = new ArrayList<Double>(CAPACITY); | |
/** | |
* Add a value to the aggregator. | |
* @param value | |
*/ | |
public final void add(final double value) { | |
stack.add(value); | |
if (stack.size() == CAPACITY) { | |
double stack_value = 0; | |
for (Double d : stack) { | |
stack_value += d; | |
} | |
this.value += stack_value; | |
stack = new ArrayList<Double>(CAPACITY); | |
} | |
} | |
/** | |
* Get the current value of the aggregator. | |
* @return | |
*/ | |
public final double value() { | |
double stack_value = 0; | |
for (Double d : stack) { | |
stack_value += d; | |
} | |
return value + stack_value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment