author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 31262 | b694a580405b |
permissions | -rw-r--r-- |
16922 | 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 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util; |
|
26 |
||
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 | 29 |
|
30 |
/** |
|
31 |
* A state object for collecting statistics such as count, min, max, sum, and |
|
32 |
* average. |
|
33 |
* |
|
34 |
* <p>This class is designed to work with (though does not require) |
|
35 |
* {@linkplain java.util.stream streams}. For example, you can compute |
|
36 |
* summary statistics on a stream of doubles with: |
|
37 |
* <pre> {@code |
|
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 | 41 |
* }</pre> |
42 |
* |
|
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 | 45 |
* target for a {@linkplain java.util.stream.Stream stream}. For example: |
46 |
* |
|
47 |
* <pre> {@code |
|
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 | 50 |
*}</pre> |
51 |
* |
|
52 |
* This computes, in a single pass, the count of people, as well as the minimum, |
|
53 |
* maximum, sum, and average of their weights. |
|
54 |
* |
|
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 | 58 |
* implementation of {@link java.util.stream.Stream#collect Stream.collect()} |
59 |
* provides the necessary partitioning, isolation, and merging of results for |
|
60 |
* safe and efficient parallel execution. |
|
61 |
* @since 1.8 |
|
62 |
*/ |
|
63 |
public class DoubleSummaryStatistics implements DoubleConsumer { |
|
64 |
private long count; |
|
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 | 68 |
private double min = Double.POSITIVE_INFINITY; |
69 |
private double max = Double.NEGATIVE_INFINITY; |
|
70 |
||
71 |
/** |
|
72 |
* Construct an empty instance with zero count, zero sum, |
|
73 |
* {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY} |
|
74 |
* max and zero average. |
|
75 |
*/ |
|
76 |
public DoubleSummaryStatistics() { } |
|
77 |
||
78 |
/** |
|
79 |
* Records another value into the summary information. |
|
80 |
* |
|
81 |
* @param value the input value |
|
82 |
*/ |
|
83 |
@Override |
|
84 |
public void accept(double value) { |
|
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 | 88 |
min = Math.min(min, value); |
89 |
max = Math.max(max, value); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Combines the state of another {@code DoubleSummaryStatistics} into this |
|
94 |
* one. |
|
95 |
* |
|
96 |
* @param other another {@code DoubleSummaryStatistics} |
|
97 |
* @throws NullPointerException if {@code other} is null |
|
98 |
*/ |
|
99 |
public void combine(DoubleSummaryStatistics other) { |
|
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 | 104 |
min = Math.min(min, other.min); |
105 |
max = Math.max(max, other.max); |
|
106 |
} |
|
107 |
||
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 | 120 |
* Return the count of values recorded. |
121 |
* |
|
122 |
* @return the count of values |
|
123 |
*/ |
|
124 |
public final long getCount() { |
|
125 |
return count; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
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 | 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 | 183 |
* |
184 |
* @return the sum of values, or zero if none |
|
185 |
*/ |
|
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 | 197 |
} |
198 |
||
199 |
/** |
|
200 |
* Returns the minimum recorded value, {@code Double.NaN} if any recorded |
|
201 |
* value was NaN or {@code Double.POSITIVE_INFINITY} if no values were |
|
202 |
* recorded. Unlike the numerical comparison operators, this method |
|
203 |
* considers negative zero to be strictly smaller than positive zero. |
|
204 |
* |
|
205 |
* @return the minimum recorded value, {@code Double.NaN} if any recorded |
|
206 |
* value was NaN or {@code Double.POSITIVE_INFINITY} if no values were |
|
207 |
* recorded |
|
208 |
*/ |
|
209 |
public final double getMin() { |
|
210 |
return min; |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Returns the maximum recorded value, {@code Double.NaN} if any recorded |
|
215 |
* value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were |
|
216 |
* recorded. Unlike the numerical comparison operators, this method |
|
217 |
* considers negative zero to be strictly smaller than positive zero. |
|
218 |
* |
|
219 |
* @return the maximum recorded value, {@code Double.NaN} if any recorded |
|
220 |
* value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were |
|
221 |
* recorded |
|
222 |
*/ |
|
223 |
public final double getMax() { |
|
224 |
return max; |
|
225 |
} |
|
226 |
||
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 | 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 | 239 |
*/ |
240 |
public final double getAverage() { |
|
241 |
return getCount() > 0 ? getSum() / getCount() : 0.0d; |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* {@inheritDoc} |
|
246 |
* |
|
247 |
* Returns a non-empty string representation of this object suitable for |
|
248 |
* debugging. The exact presentation format is unspecified and may vary |
|
249 |
* between implementations and versions. |
|
250 |
*/ |
|
251 |
@Override |
|
252 |
public String toString() { |
|
253 |
return String.format( |
|
254 |
"%s{count=%d, sum=%f, min=%f, average=%f, max=%f}", |
|
255 |
this.getClass().getSimpleName(), |
|
256 |
getCount(), |
|
257 |
getSum(), |
|
258 |
getMin(), |
|
259 |
getAverage(), |
|
260 |
getMax()); |
|
261 |
} |
|
262 |
} |