Details
Description
If a pipeline is of a known size then the average operation need not maintain a count of the elements.
For example, given the following pipeline:
double[] d =
DoubleStream ds = DoubleStream.of(d);
double a = ds.average().getOrElse(0.0);
The size of the pipeline is the size of the array.
The implementation of average is currently:
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]);
ll[2] += rr[2];
ll[3] += rr[3];
});
and it can be implemented as follows when the size is known:
sum() / pipeline_size
(Assuming it is easy to surface the size of the pipeline, otherwise a special op is required to get access to that information).
For example, given the following pipeline:
double[] d =
DoubleStream ds = DoubleStream.of(d);
double a = ds.average().getOrElse(0.0);
The size of the pipeline is the size of the array.
The implementation of average is currently:
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]);
ll[2] += rr[2];
ll[3] += rr[3];
});
and it can be implemented as follows when the size is known:
sum() / pipeline_size
(Assuming it is easy to surface the size of the pipeline, otherwise a special op is required to get access to that information).
Issue Links
 relates to

JDK8030942 Explicitly state floatingpoint summation requirements on nonfinite inputs

JDK8030212 Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "Infinity"