Repository: Hipparchus-Math/hipparchus (java)

Issue:
KthSelector does not consider natural order of doubles
As a user I would expect that sorting the array and taking the k-th element yields the same result as using the KthSelector. Unfortunately, this is not true if the array contains NaN or -0/+0 values, because the implementation uses < and > operators for comparison instead of Double.compare.

Pull request:
fixed #2, KthSelector does not consider natural order of doubles

Patch:
diff --git a/hipparchus-core/src/main/java/org/hipparchus/util/KthSelector.java b/hipparchus-core/src/main/java/org/hipparchus/util/KthSelector.java
index 38eb4177c..3da48f88f 100644
--- a/hipparchus-core/src/main/java/org/hipparchus/util/KthSelector.java
+++ b/hipparchus-core/src/main/java/org/hipparchus/util/KthSelector.java
@@ -130,10 +130,10 @@ public class KthSelector implements Serializable {
         int i = begin + 1;
         int j = end - 1;
         while (i < j) {
-            while (i < j && work[j] > value) {
+            while (i < j && Double.compare(work[j], value) > 0) {
                 --j;
             }
-            while (i < j && work[i] < value) {
+            while (i < j && Double.compare(work[i], value) < 0) {
                 ++i;
             }
 
@@ -144,7 +144,7 @@ public class KthSelector implements Serializable {
             }
         }
 
-        if (i >= end || work[i] > value) {
+        if (i >= end || Double.compare(work[i], value) > 0) {
             --i;
         }
         work[begin] = work[i];


Test patch:
diff --git a/hipparchus-core/src/test/java/org/hipparchus/util/KthSelectorTest.java b/hipparchus-core/src/test/java/org/hipparchus/util/KthSelectorTest.java
new file mode 100644
index 000000000..98c42a5a2
--- /dev/null
+++ b/hipparchus-core/src/test/java/org/hipparchus/util/KthSelectorTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Hipparchus project under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.hipparchus.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import org.junit.Test;
+
+public class KthSelectorTest {
+
+    @Test
+    public void testRandom() {
+        
+        final int numIterations = 100000;
+        final double[] possibleValues = {Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, -0., 0., 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        final Random rnd = new Random(0);
+        for (int i = 0; i < numIterations; ++i) {
+            
+            final int dataSize = rnd.nextInt(30);
+
+            final double[] data = new double[dataSize];
+
+            for (int j = 0; j < dataSize; ++j) {
+                data[j] = possibleValues[rnd.nextInt(possibleValues.length)];
+            }
+            
+            final double[] dataSorted = Arrays.copyOf(data, data.length);
+            Arrays.sort(dataSorted);
+
+            for (int j = 0; j < dataSize; ++j) {
+
+                final double[] dataTmp = Arrays.copyOf(data, data.length);
+                final double resultKthSelector = new KthSelector().select(dataTmp, null, j);
+                final double resultSort = dataSorted[j];
+                assertEquals(Double.doubleToLongBits(resultKthSelector), Double.doubleToLongBits(resultSort));
+            }
+        }
+    }
+}