RFR 8214761 : Bug in parallel Kahan summation implementation

Ivan Gerasimov ivan.gerasimov at oracle.com
Mon Dec 10 03:37:39 UTC 2018


Hello!

DoubleSummaryStatistics takes advantage of Kahan summation algorithm to 
reduce the error of the total sum.

Internally it maintains a field double sumCompensation, which keeps 
lower bits (which were rounded off) of the last addition.

Note, that the compensation has to be subtracted from the result to add 
those bits back:

  166     private void sumWithCompensation(double value) {
  167         double tmp = value - sumCompensation;
  168         double velvel = sum + tmp; // Little wolf of rounding error
  169         sumCompensation = (velvel - sum) - tmp;
  170         sum = velvel;
  171     }

At the line 169, tmp normally has more lower bits than (velvel - sum).

However, when two DoubleSummaryStatistics objects are combined, this 
compensation part is *added* to the total, which may result in a less 
accurate result.

The same bug is replicated in DoubleStreams.

Would you please help review the fix?

BUGURL: https://bugs.openjdk.java.net/browse/JDK-8214761
WEBREV: http://cr.openjdk.java.net/~igerasim/8214761/00/webrev/

-- 
With kind regards,
Ivan Gerasimov



More information about the core-libs-dev mailing list