Uploaded image for project: 'Commons Numbers'
  1. Commons Numbers
  2. NUMBERS-200

Wrong output when adding two Sum instances containing special values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • None
    • 1.2
    • core
    • None
    • Easy

    Description

      Here's some setup code before describing the bug :
      Two sum instances are created and assigned Double.POSITIVE_INFINITY as follows:

      Sum sum1 = Sum.create();
      Sum sum2 = Sum.create();
      sum1.add(Double.POSITIVE_INFINITY);
      sum2.add(Double.POSITIVE_INFINITY);

      The `sum` and `comp` fields of both `sum` and `sum2` hold the values `Double.POSITIVE_INFINITY` and `Double.NaN` repectively. 

      sum2 is added to sum1 as:

      sum1.add(sum2);
      

      The `Sum add(Sum other)` returns the value returned by `Sum add(double s, double c)` method.

      In the Sum add(double s, double c) method, there's a bug in the return statement : 

      return add(s).add(c); 

      When add(c) is executed, c (i.e. sum2.c) which has a value Double.NaN, is causing the sum field of sum1 to be Double.NaN. This is due to :

      Sum add(final double t) { // t (= c)
          final double newSum = sum + t; // Inf + Nan = NaN
          comp += ExtendedPrecision.twoSumLow(sum, t, newSum); // NaN
          sum = newSum; // NaN 
          return this;
      } 

      Hence, the chaining of add operations should be removed.

      The fix would be in the `Sum add(double s, double c)` method as :

      Sum add(double s, double c) {
          add(s);
          if (!(Double.isNan(comp) && Double.isInfinite(sum))) {
              add(c);
          }
          return this;
      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ani5rudh Anirudh Joshi
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: