< prev index next >

src/java.base/share/classes/java/util/stream/DoublePipeline.java

Print this page
rev 52916 : [mq]: 8214761-Bug-in-parallel-Kahan-summation-implementation-for-Doublestream-sum

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -415,23 +415,23 @@
     @Override
     public final double sum() {
         /*
          * In the arrays allocated for the collect operation, index 0
          * holds the high-order bits of the running sum, index 1 holds
-         * the low-order bits of the sum computed via compensated
-         * summation, and index 2 holds the simple sum used to compute
-         * the proper result if the stream contains infinite values of
-         * the same sign.
+         * the (negative) low-order bits of the sum computed via
+         * compensated summation, and index 2 holds the simple sum used
+         * to compute the proper result if the stream contains infinite
+         * values of the same sign.
          */
         double[] summation = collect(() -> new double[3],
                                (ll, d) -> {
                                    Collectors.sumWithCompensation(ll, d);
                                    ll[2] += d;
                                },
                                (ll, rr) -> {
                                    Collectors.sumWithCompensation(ll, rr[0]);
-                                   Collectors.sumWithCompensation(ll, rr[1]);
+                                   Collectors.sumWithCompensation(ll, -rr[1]);
                                    ll[2] += rr[2];
                                });
 
         return Collectors.computeFinalSum(summation);
     }

@@ -458,23 +458,23 @@
     @Override
     public final OptionalDouble average() {
         /*
          * In the arrays allocated for the collect operation, index 0
          * holds the high-order bits of the running sum, index 1 holds
-         * the low-order bits of the sum computed via compensated
-         * summation, index 2 holds the number of values seen, index 3
-         * holds the simple sum.
+         * the (negative) low-order bits of the sum computed via
+         * compensated summation, index 2 holds the number of values
+         * seen, index 3 holds the simple sum.
          */
         double[] avg = collect(() -> new double[4],
                                (ll, d) -> {
                                    ll[2]++;
                                    Collectors.sumWithCompensation(ll, d);
                                    ll[3] += d;
                                },
                                (ll, rr) -> {
                                    Collectors.sumWithCompensation(ll, rr[0]);
-                                   Collectors.sumWithCompensation(ll, rr[1]);
+                                   Collectors.sumWithCompensation(ll, -rr[1]);
                                    ll[2] += rr[2];
                                    ll[3] += rr[3];
                                });
         return avg[2] > 0
             ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
< prev index next >