1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8214761
  27  * @summary When combining two DoubleSummaryStatistics, the compensation
  28  *          has to be subtracted.
  29  */
  30 
  31 import java.util.DoubleSummaryStatistics;
  32 
  33 public class NegativeCompensation {
  34     static final double VAL = 1.000000001;
  35     static final int LOG_ITER = 21;
  36 
  37     public static void main(String[] args) {
  38         DoubleSummaryStatistics stat0 = new DoubleSummaryStatistics();
  39         DoubleSummaryStatistics stat1 = new DoubleSummaryStatistics();
  40         DoubleSummaryStatistics stat2 = new DoubleSummaryStatistics();
  41 
  42         stat1.accept(VAL);
  43         stat1.accept(VAL);
  44         stat2.accept(VAL);
  45         stat2.accept(VAL);
  46         stat2.accept(VAL);
  47 
  48         for (int i = 0; i < LOG_ITER; ++i) {
  49             stat1.combine(stat2);
  50             stat2.combine(stat1);
  51         }
  52 
  53         System.out.println("count: " + stat2.getCount());
  54         for (long i = 0, iend = stat2.getCount(); i < iend; ++i) {
  55             stat0.accept(VAL);
  56         }
  57 
  58         double absErr = Math.abs(stat0.getSum() - stat2.getSum());
  59         System.out.println("serial sum: " + stat0.getSum());
  60         System.out.println("combined sum: " + stat2.getSum());
  61         System.out.println("abs error: " + absErr);
  62         if (absErr > 0.00000001) {
  63             throw new RuntimeException("Absolute error is too big: " + absErr);
  64         }
  65     }
  66 }