拥有这三样东西有什么用。
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;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
您可以看到它们是如何使用的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;
}
Run Code Online (Sandbox Code Playgroud)
的 JavadocgetSum()不需要任何特定的实现,但允许减少错误范围的实现:
浮点和的值是输入值以及加法运算顺序的函数。故意不定义该方法的加法运算的顺序,以允许实现灵活性,以提高计算结果的速度和准确性。具体地,该方法可以使用补偿求和或其他技术来实现,以与双精度值的简单求和相比减少数值和中的误差范围。
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |