Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ public static void checkEqualLength(int[] a,
* @param abort Whether to throw an exception if the check fails.
* @return {@code true} if the array is sorted.
* @throws NonMonotonicSequenceException if the array is not sorted
* and {@code abort} is {@code true}.
* and {@code abort} is {@code true}. A comparison involving {@code NaN}
* fails the check.
*/
public static boolean checkOrder(double[] val, OrderDirection dir,
boolean strict, boolean abort) {
Expand All @@ -456,6 +457,11 @@ public static boolean checkOrder(double[] val, OrderDirection dir,
int index;
ITEM:
for (index = 1; index < max; index++) {
if (Double.isNaN(val[index]) ||
Double.isNaN(previous)) {
// NaN cannot be ordered.
break ITEM;
}
switch (dir) {
case INCREASING:
if (strict) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ public void testCheckOrder() {
}
}

@Test
public void testCheckOrderNaN() {
final double nan = Double.NaN;
for (final MathArrays.OrderDirection dir : MathArrays.OrderDirection.values()) {
for (final boolean strict : new boolean[] {true, false}) {
Assert.assertFalse(MathArrays.checkOrder(new double[] {1, nan, 0}, dir, strict, false));
Assert.assertFalse(MathArrays.checkOrder(new double[] {0, nan, 1}, dir, strict, false));
Assert.assertFalse(MathArrays.checkOrder(new double[] {nan, 0}, dir, strict, false));
Assert.assertFalse(MathArrays.checkOrder(new double[] {0, nan}, dir, strict, false));
Assert.assertFalse(MathArrays.isMonotonic(new double[] {1, nan, 0}, dir, strict));
Assert.assertFalse(MathArrays.isMonotonic(new double[] {0, nan, 1}, dir, strict));
}
}
try {
MathArrays.checkOrder(new double[] {0, 2, nan, 1, 3});
Assert.fail("an exception should have been thrown");
} catch (NonMonotonicSequenceException e) {
// Expected
}
}

@Test
public void testIsMonotonic() {
Assert.assertFalse(MathArrays.isMonotonic(new double[] {-15, -5.5, -1, -1, 2, 15},
Expand Down
Loading