src/share/classes/java/util/DoubleSummaryStatistics.java

Print this page

        

*** 61,70 **** --- 61,71 ---- * @since 1.8 */ public class DoubleSummaryStatistics implements DoubleConsumer { private long count; private double sum; + private double sumCompensation; // Low order bits of sum private double min = Double.POSITIVE_INFINITY; private double max = Double.NEGATIVE_INFINITY; /** * Construct an empty instance with zero count, zero sum,
*** 79,89 **** * @param value the input value */ @Override public void accept(double value) { ++count; ! sum += value; min = Math.min(min, value); max = Math.max(max, value); } /** --- 80,90 ---- * @param value the input value */ @Override public void accept(double value) { ++count; ! sumWithCompensation(value); min = Math.min(min, value); max = Math.max(max, value); } /**
*** 93,108 **** * @param other another {@code DoubleSummaryStatistics} * @throws NullPointerException if {@code other} is null */ public void combine(DoubleSummaryStatistics other) { count += other.count; ! sum += other.sum; min = Math.min(min, other.min); max = Math.max(max, other.max); } /** * Return the count of values recorded. * * @return the count of values */ public final long getCount() { --- 94,121 ---- * @param other another {@code DoubleSummaryStatistics} * @throws NullPointerException if {@code other} is null */ public void combine(DoubleSummaryStatistics other) { count += other.count; ! sumWithCompensation(other.sum); ! sumWithCompensation(other.sumCompensation); min = Math.min(min, other.min); max = Math.max(max, other.max); } /** + * Incorporate a new double value using Kahan summation / + * compensated summation. + */ + private void sumWithCompensation(double value) { + double tmp = value - sumCompensation; + double velvel = sum + tmp; // Little wolf of rounding error + sumCompensation = (velvel - sum) - tmp; + sum = velvel; + } + + /** * Return the count of values recorded. * * @return the count of values */ public final long getCount() {