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

Use Math.hypot in Point2D

XMLWordPrintable

    • 2d
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      java.awt.geom.Point2D has several overloads of methods to compute the distance between points. Currently, these methods use Math.sqrt:

          public double distance(Point2D pt) {
              double px = pt.getX() - this.getX();
              double py = pt.getY() - this.getY();
              return Math.sqrt(px * px + py * py);
          }

      But there is an opportunity to use Math.hypot:

          public double distance(Point2D pt) {
              double px = pt.getX() - this.getX();
              double py = pt.getY() - this.getY();
              return Math.hypot(px, py);
          }

      This is a minor improvement to accuracy, by avoiding overflow or underflow in computing the intermediate squared value.


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: