Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Won't Fix
-
None
-
None
-
None
-
Easy
Description
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:
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());
Prints:
2.5 -0.5
Overflow can be detected in combine, e.g.
// throws ArithmeticException when n+m overflows n = Math.addExact(n, other.n)
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.