jdk/src/java.base/share/classes/java/util/DoubleSummaryStatistics.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 31262 b694a580405b
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     1
/*
25753
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     4
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    10
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    15
 * accompanied this code).
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    16
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    20
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    23
 * questions.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    24
 */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    25
package java.util;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    26
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    27
import java.util.function.DoubleConsumer;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    28
import java.util.stream.Collector;
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    29
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    30
/**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    31
 * A state object for collecting statistics such as count, min, max, sum, and
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    32
 * average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    33
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    34
 * <p>This class is designed to work with (though does not require)
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    35
 * {@linkplain java.util.stream streams}. For example, you can compute
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    36
 * summary statistics on a stream of doubles with:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    37
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    38
 * DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new,
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    39
 *                                                      DoubleSummaryStatistics::accept,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    40
 *                                                      DoubleSummaryStatistics::combine);
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    41
 * }</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    42
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    43
 * <p>{@code DoubleSummaryStatistics} can be used as a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    44
 * {@linkplain java.util.stream.Stream#collect(Collector) reduction}
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    45
 * target for a {@linkplain java.util.stream.Stream stream}. For example:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    46
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    47
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    48
 * DoubleSummaryStatistics stats = people.stream()
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    49
 *     .collect(Collectors.summarizingDouble(Person::getWeight));
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    50
 *}</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    51
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    52
 * This computes, in a single pass, the count of people, as well as the minimum,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    53
 * maximum, sum, and average of their weights.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    54
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    55
 * @implNote This implementation is not thread safe. However, it is safe to use
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
    56
 * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction)
24513
2b9212fa87d8 8043772: Typos in Double/Int/LongSummaryStatistics.java
igerasim
parents: 22101
diff changeset
    57
 * Collectors.summarizingDouble()} on a parallel stream, because the parallel
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    58
 * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    59
 * provides the necessary partitioning, isolation, and merging of results for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    60
 * safe and efficient parallel execution.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    61
 * @since 1.8
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    62
 */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    63
public class DoubleSummaryStatistics implements DoubleConsumer {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    64
    private long count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    65
    private double sum;
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
    66
    private double sumCompensation; // Low order bits of sum
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
    67
    private double simpleSum; // Used to compute right sum for non-finite inputs
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    68
    private double min = Double.POSITIVE_INFINITY;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    69
    private double max = Double.NEGATIVE_INFINITY;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    70
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    71
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    72
     * Construct an empty instance with zero count, zero sum,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    73
     * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    74
     * max and zero average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    75
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    76
    public DoubleSummaryStatistics() { }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    77
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    78
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    79
     * Records another value into the summary information.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    80
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    81
     * @param value the input value
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    82
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    83
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    84
    public void accept(double value) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    85
        ++count;
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
    86
        simpleSum += value;
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
    87
        sumWithCompensation(value);
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    88
        min = Math.min(min, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    89
        max = Math.max(max, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    90
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    91
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    92
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    93
     * Combines the state of another {@code DoubleSummaryStatistics} into this
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    94
     * one.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    95
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    96
     * @param other another {@code DoubleSummaryStatistics}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    97
     * @throws NullPointerException if {@code other} is null
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    98
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    99
    public void combine(DoubleSummaryStatistics other) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   100
        count += other.count;
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   101
        simpleSum += other.simpleSum;
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   102
        sumWithCompensation(other.sum);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   103
        sumWithCompensation(other.sumCompensation);
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   104
        min = Math.min(min, other.min);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   105
        max = Math.max(max, other.max);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   106
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   107
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   108
    /**
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   109
     * Incorporate a new double value using Kahan summation /
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   110
     * compensated summation.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   111
     */
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   112
    private void sumWithCompensation(double value) {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   113
        double tmp = value - sumCompensation;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   114
        double velvel = sum + tmp; // Little wolf of rounding error
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   115
        sumCompensation = (velvel - sum) - tmp;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   116
        sum = velvel;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   117
    }
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   118
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   119
    /**
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   120
     * Return the count of values recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   121
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   122
     * @return the count of values
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   123
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   124
    public final long getCount() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   125
        return count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   126
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   127
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   128
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   129
     * Returns the sum of values recorded, or zero if no values have been
20758
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   130
     * recorded.
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   131
     *
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   132
     * <p> The value of a floating-point sum is a function both of the
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   133
     * input values as well as the order of addition operations. The
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   134
     * order of addition operations of this method is intentionally
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   135
     * not defined to allow for implementation flexibility to improve
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   136
     * the speed and accuracy of the computed result.
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   137
     *
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   138
     * In particular, this method may be implemented using compensated
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   139
     * summation or other technique to reduce the error bound in the
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   140
     * numerical sum compared to a simple summation of {@code double}
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   141
     * values.
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   142
     *
25753
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   143
     * Because of the unspecified order of operations and the
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   144
     * possibility of using differing summation schemes, the output of
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   145
     * this method may vary on the same input values.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   146
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   147
     * <p>Various conditions can result in a non-finite sum being
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   148
     * computed. This can occur even if the all the recorded values
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   149
     * being summed are finite. If any recorded value is non-finite,
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   150
     * the sum will be non-finite:
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   151
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   152
     * <ul>
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   153
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   154
     * <li>If any recorded value is a NaN, then the final sum will be
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   155
     * NaN.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   156
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   157
     * <li>If the recorded values contain one or more infinities, the
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   158
     * sum will be infinite or NaN.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   159
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   160
     * <ul>
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   161
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   162
     * <li>If the recorded values contain infinities of opposite sign,
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   163
     * the sum will be NaN.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   164
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   165
     * <li>If the recorded values contain infinities of one sign and
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   166
     * an intermediate sum overflows to an infinity of the opposite
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   167
     * sign, the sum may be NaN.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   168
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   169
     * </ul>
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   170
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   171
     * </ul>
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   172
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   173
     * It is possible for intermediate sums of finite values to
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   174
     * overflow into opposite-signed infinities; if that occurs, the
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   175
     * final sum will be NaN even if the recorded values are all
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   176
     * finite.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   177
     *
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   178
     * If all the recorded values are zero, the sign of zero is
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   179
     * <em>not</em> guaranteed to be preserved in the final sum.
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   180
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20758
diff changeset
   181
     * @apiNote Values sorted by increasing absolute magnitude tend to yield
20758
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   182
     * more accurate results.
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   183
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   184
     * @return the sum of values, or zero if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   185
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   186
    public final double getSum() {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   187
        // Better error bounds to add both terms as the final sum
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   188
        double tmp =  sum + sumCompensation;
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   189
        if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   190
            // If the compensated sum is spuriously NaN from
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   191
            // accumulating one or more same-signed infinite values,
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   192
            // return the correctly-signed infinity stored in
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   193
            // simpleSum.
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   194
            return simpleSum;
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   195
        else
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   196
            return tmp;
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   197
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   198
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   199
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   200
     * Returns the minimum recorded value, {@code Double.NaN} if any recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   201
     * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   202
     * recorded. Unlike the numerical comparison operators, this method
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   203
     * considers negative zero to be strictly smaller than positive zero.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   204
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   205
     * @return the minimum recorded value, {@code Double.NaN} if any recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   206
     * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   207
     * recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   208
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   209
    public final double getMin() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   210
        return min;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   211
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   212
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   213
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   214
     * Returns the maximum recorded value, {@code Double.NaN} if any recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   215
     * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   216
     * recorded. Unlike the numerical comparison operators, this method
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   217
     * considers negative zero to be strictly smaller than positive zero.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   218
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   219
     * @return the maximum recorded value, {@code Double.NaN} if any recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   220
     * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   221
     * recorded
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   222
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   223
    public final double getMax() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   224
        return max;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   225
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   226
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   227
    /**
20758
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   228
     * Returns the arithmetic mean of values recorded, or zero if no
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   229
     * values have been recorded.
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   230
     *
25753
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   231
     * <p> The computed average can vary numerically and have the
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   232
     * special case behavior as computing the sum; see {@link #getSum}
b73bd85f9068 8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents: 24513
diff changeset
   233
     * for details.
20758
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   234
     *
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   235
     * @apiNote Values sorted by increasing absolute magnitude tend to yield
d8845d3fb428 8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents: 19214
diff changeset
   236
     * more accurate results.
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   237
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 16922
diff changeset
   238
     * @return the arithmetic mean of values, or zero if none
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   239
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   240
    public final double getAverage() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   241
        return getCount() > 0 ? getSum() / getCount() : 0.0d;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   242
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   243
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   244
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   245
     * {@inheritDoc}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   246
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   247
     * Returns a non-empty string representation of this object suitable for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   248
     * debugging. The exact presentation format is unspecified and may vary
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   249
     * between implementations and versions.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   250
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   251
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   252
    public String toString() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   253
        return String.format(
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   254
            "%s{count=%d, sum=%f, min=%f, average=%f, max=%f}",
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   255
            this.getClass().getSimpleName(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   256
            getCount(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   257
            getSum(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   258
            getMin(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   259
            getAverage(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   260
            getMax());
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   261
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   262
}