From 572e1795fa5dea5dd48cf2d0e5f4a886ae223fee Mon Sep 17 00:00:00 2001 From: Mahammad Insaf Date: Mon, 16 Nov 2026 11:37:58 +0530 Subject: [PATCH] reject NaN in MathArrays.checkOrder --- .../commons/math4/legacy/core/MathArrays.java | 8 ++++++- .../math4/legacy/core/MathArraysTest.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java index be8803631..52bf5287e 100644 --- a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java +++ b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java @@ -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) { @@ -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) { diff --git a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java index 9a185110d..d5030e80a 100644 --- a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java +++ b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java @@ -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},