This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 4db361ad9 Reject NaN in MathArrays.checkOrder (#356)
4db361ad9 is described below
commit 4db361ad9426e5a19d73668aaa9e862f3a51b151
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:36:24 2026 +0530
Reject NaN in MathArrays.checkOrder (#356)
---
.../commons/math4/legacy/core/MathArrays.java | 8 +++++++-
.../commons/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 final class MathArrays {
* @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 final class MathArrays {
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 class MathArraysTest {
}
}
+ @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},