Fix KthSelector to consider natural order of doubles

This commit is contained in:
Otmar Ertl 2016-08-05 17:03:09 +02:00 committed by Emmanuel Bourg
parent 83b70a377f
commit 819318486d
2 changed files with 59 additions and 3 deletions

View File

@ -133,10 +133,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;
}
@ -147,7 +147,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];

View File

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.commons.math4.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));
}
}
}
}