Какая польза от наличия этих трех вещей.
public class DoubleSummaryStatistics
implements DoubleConsumer
{
private long count;
private double sum;
private double sumCompensation;
private double simpleSum;
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
}





sum и sumCompensation используются для уменьшения ошибки регулярного суммирования с плавающей запятой.
simpleSum содержит простую сумму (полученную путем применения simpleSum += value; к каждой добавленной стоимости) и используется для нескончаемых сумм.
Детали реализации объясняют, что:
private double sum;
private double sumCompensation; // Low order bits of sum
private double simpleSum; // Used to compute right sum for non-finite inputs
Вот как вычисляются sum и sumCompensation:
/**
* 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;
}
Вы можете увидеть, как они используются в getSum():
public final double getSum() {
// Better error bounds to add both terms as the final sum
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;
}
Javadoc getSum() не требует какой-либо конкретной реализации, но допускает реализации, которые уменьшают границу ошибок:
The value of a floating-point sum is a function both of the input values as well as the order of addition operations. The order of addition operations of this method is intentionally not defined to allow for implementation flexibility to improve the speed and accuracy of the computed result. In particular, this method may be implemented using compensated summation or other technique to reduce the error bound in the numerical sum compared to a simple summation of double values.
double velvel = sum + tmp; // Little wolf of rounding error Для записи, прозвище Кахана было «Велвел», что означает Маленький волк.
В дополнение к ответу Эрана см. Также en.wikipedia.org/wiki/Kahan_sumpting_algorithm