Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8229259

Math.hypot result not always within 1 ulp of exact result

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • None
    • None
    • core-libs
    • None

      (As reported by Hans Boehm (boehm@acm.org))

      Math.hypot says:

      """The computed result must be within 1 ulp of the exact result."""

      This means that if the exact result is between adjacent floating point numbers f1 and f2, then the result must be either f1 or f2. I can exhibit a pair of doubles for which this is not the case under OpenJDK 8 through 14.. It's only off by a little.

      public class BasicHypotBug {
          public static void main(String[] args) {
              double arg1 = 2.6718173667255144E-307;
              double arg2 = 1.1432573432387167E-308; // Slightly denormal
              // Actual result:
              // 2.6742622187558112649563688387773761534655742E-307
              double resultUpperBound = 2.6742622187558113E-307;
              // Actual value of resultUpperBound:
              // 2.6742622187558112997755783398169632667381058E-307
              double result = Math.hypot(arg1, arg2);
              if (result > resultUpperBound) {
                  System.out.println(
                      "Result too large: " + result + " > " + resultUpperBound);
              }
          }
      }

      =>
      Result too large: 2.6742622187558117E-307 > 2.6742622187558113E-307

            darcy Joe Darcy
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: