Returns the sum of elements in this stream. Summation is a special case of a reduction. If floating-point summation were exact, this method would be equivalent to:
return reduce(0, Double::sum);
However, since floating-point summation is not exact, the above
code is not necessarily equivalent to the summation computation
done by this method.
The value of a floating-point sum is a function both
of the input values as well as the order of addition
operations. The order of addition operations of this method is
intentionally not defined to allow for implementation
flexibility to improve the speed and accuracy of the computed
result.
In particular, this method may be implemented using compensated
summation or other technique to reduce the error bound in the
numerical sum compared to a simple summation of double
values.
Because of the unspecified order of operations and the
possibility of using differing summation schemes, the output of
this method may vary on the same input elements.
Various conditions can result in a non-finite sum being computed. This can occur even if the all the elements being summed are finite. If any element is non-finite, the sum will be non-finite:
This is a terminal operation.
@apiNote Elements sorted by increasing absolute magnitude tend
to yield more accurate results.
Diagram: Stream