Uploaded image for project: 'Commons Statistics'
  1. Commons Statistics
  2. STATISTICS-82

Raise exception for integer overflow during statistic computation.

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • None
    • None
    • descriptive
    • 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.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            aherbert Alex Herbert
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment