Alex Herbert created STATISTICS-82:
--------------------------------------

             Summary: Raise exception for integer overflow during statistic 
computation.
                 Key: STATISTICS-82
                 URL: https://issues.apache.org/jira/browse/STATISTICS-82
             Project: Commons Statistics
          Issue Type: Improvement
          Components: descriptive
            Reporter: Alex Herbert


Many statistics count the number of observations using a long. This limited 
computation to ~2^63 values.

This is effectively impossible to achieve using single values. It is very easy 
to achieve using a combine:
{code:java}
final IntMean m = IntMean.of(2, 3);
System.out.println(m.getAsDouble());
for (int i = 0; i < 62; i++) {
    m.combine(m);
}
// n = 2^63 = -9223372036854775808
System.out.println(m.getAsDouble());
{code}
Prints:
{noformat}
2.5
-0.5
{noformat}
Overflow can be detected in combine, e.g.
{code:java}
// throws ArithmeticException when n+m overflows
n = Math.addExact(n, other.n)
{code}
However it is possible to create a statistic with a count very close to 2^63 
via combine and then increment it past 2^63 with single values. To avoid an 
expensive check at each addition, this case will have to detect overflow of n 
to negative during the final computation of the statistic.

Raising an exception will guard against misuse. It is unlikely to affect any 
real use cases. Without an exception the user will have an invalid result which 
may cause downstream problems in the dependent code.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to