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

New methods for java.lang.Math: median, min/max, sqr

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 5.0, 6, 7
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      I often programs algorithmic tasks in Java, especially geometrical. There are several simple functions, that are not included in Math and StrictMath classes yet, but are necessary very often. I would appreciate very much if you'll include them in Java 1.7.

      1) Median: the middle value among 3 numbers.
          public static double median(double a, double b, double c) {
              return b < c ? (a < b ? b : a > c ? c : a) : (a < c ? c : a > b ? b : a);
          }
      and overloaded versions for float, int, long (with, maybe, more thorough processing NaN and 0.0). This method is very useful when we need to "truncate" some value to required range. So, median(x, 0, 1000) replaces "x" with 0 or 1000 if this value is out of 0..1000 range. (Here "x", for example, may be some coordinate in a picture 1000x1000, and truncation of invalid coordinates may be suitable solution to avoid unexpected algorithm behavior.)

      2) Overloaded versions of min and max methods with variable number of arguments. It was already requested in http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6239196, but, unlike the submitter of that RFE, I think these methods should be added in Math/StrictMath for primitive types only: see my comment there.

      3) Math.sqr: very, very needful :)
           public static double sqr(double a) {
               return a * a;
           }
      and overloaded versions for float, int, long. There are a lot of situations, especially in geometry, when we need to calculate a square of some expressions. Of course, for a simple variable "x" we may write "x * x", but for any longer expressions we must either create extra temporary variables:
          double dx = points[k].x - anotherStructure.points[k].x;
          double dy = points[k].y - anotherStructure.points[k].y;
          double dSqr = dx * dx + dy * dy;
      or write complicated and probably inefficient code:
          double dSqr = (points[k].x - anotherStructure.points[k].x)
              * (points[k].x - anotherStructure.points[k].x)
              + (points[k].y - anotherStructure.points[k].y)
              * (points[k].y - anotherStructure.points[k].y);
      Special "sqr" method could help in such situations very much. Some other languages contains this method from the first.

      Another possible solution is optimizing Math.pow method. Unfortunately, in current JRE, the call Math.pow(x, 2) works slower than "x * x" in many times.


      JUSTIFICATION :
      The described operations are necessary very often in algorithmic classes, according to my experience during last 20 years. Creating own "Math" library with these very little number of functions, in addition to standard java.lang.Math, seems very bad solution: another programmers reading my code know nothing about such library.

            darcy Joe Darcy
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: