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

Calling NaturalRanking#rank() on an array of all NaNs throws a misleading ArrayIndexOutOfBoundsException when the NanStrategy is REMOVED

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None
    • None

    Description

      Consider the following code:

      import org.apache.commons.math3.stat.ranking.NaNStrategy;
      import org.apache.commons.math3.stat.ranking.NaturalRanking;
      import org.apache.commons.math3.stat.ranking.TiesStrategy;
      class AllNaNException{
      public NaturalRanking naturalranking;
          public double[] AllNaNArray(){
          naturalranking = new NaturalRanking(NaNStrategy.REMOVED, TiesStrategy.AVERAGE);
              double[] x = {Double.NaN, Double.NaN};
              double[] y = naturalranking.rank(x);
              return y;
          }
          public static void main(String[] args) {
              AllNaNException a = new AllNaNException();
              double[] res = a.bug();
              System.out.println(res[0] + "," + res[1]);
          }
      
      }
      

      Compiled it by: javac -cp /usr/share/java/commons-math3-3.6.1.jar tryit.java

      Executed it by: java -cp /usr/share/java/commons-math3-3.6.1.jar:. tryit

       

      Output:

      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at org.apache.commons.math3.stat.ranking.NaturalRanking.rank(NaturalRanking.java:231)
      at tryit.bug(tryit.java:9)
      at tryit.main(tryit.java:14)
      

       

      Currently, calling NaturalRanking#rank() on an array of all NaNs throws a misleading ArrayIndexOutOfBoundsException when the NanStrategy is REMOVED. I am unsure what outcome the user should expect in code like the test case I have provided below. Can you shed some light on this? I am happy to write a pull request once I know what fix would be best. I think throwing an IllegalArgumentException or returning an empty array would be more apt in this case.

      Attachments

        Activity

          People

            Unassigned Unassigned
            KakarotSSJ4 Akash Srivastava
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: