Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-866

New method to compute relative deviation between two floating numbers

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.1
    • 3.1
    • None
    • None

    Description

      A new method to compute a relative deviation would be very useful.

      In order to handle properly odd values (O+,0-,NaN,Infinity), we suggest to call first the classical equals() method (strict equality, using one ulp as a threshold) and then the relative tolerance.

      Here is the code we use :

        public static boolean equalsWithRelativeTolerance(final double x, final double y, final double eps) {
      
              // Initialisation
              boolean isEqual = false;
      
              // very close (including both equals 0.0) case
              if (equals(x, y)) {
                  isEqual = true;
              }
              // common case
              else {
                  // Relative difference computation
                  final double absoluteMax = FastMath.max(FastMath.abs(x), FastMath.abs(y));
                  final double relativeDifference = FastMath.abs((x - y) / absoluteMax);
      
                  //test
                  if (relativeDifference < eps) {
                      isEqual = true;
                  }
              }
              return isEqual;
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            ytanguy Yannick TANGUY
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: