src/share/classes/java/util/DoubleSummaryStatistics.java
Print this page
@@ -62,10 +62,11 @@
*/
public class DoubleSummaryStatistics implements DoubleConsumer {
private long count;
private double sum;
private double sumCompensation; // Low order bits of sum
+ private double simpleSum; // Used to compute right sum for non-finite inputs
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
/**
* Construct an empty instance with zero count, zero sum,
@@ -80,10 +81,11 @@
* @param value the input value
*/
@Override
public void accept(double value) {
++count;
+ simpleSum += value;
sumWithCompensation(value);
min = Math.min(min, value);
max = Math.max(max, value);
}
@@ -94,10 +96,11 @@
* @param other another {@code DoubleSummaryStatistics}
* @throws NullPointerException if {@code other} is null
*/
public void combine(DoubleSummaryStatistics other) {
count += other.count;
+ simpleSum += other.simpleSum;
sumWithCompensation(other.sum);
sumWithCompensation(other.sumCompensation);
min = Math.min(min, other.min);
max = Math.max(max, other.max);
}
@@ -145,11 +148,19 @@
*
* @return the sum of values, or zero if none
*/
public final double getSum() {
// Better error bounds to add both terms as the final sum
- return sum + sumCompensation;
+ double tmp = sum + sumCompensation;
+ if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
+ // If the compensated sum is spuriously NaN from
+ // accumulating one or more same-signed infinite values,
+ // return the correctly-signed infinity stored in
+ // simpleSum.
+ return simpleSum;
+ else
+ return tmp;
}
/**
* Returns the minimum recorded value, {@code Double.NaN} if any recorded
* value was NaN or {@code Double.POSITIVE_INFINITY} if no values were