Method for checking that no array entry is NaN.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1604648 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2014-06-22 20:06:14 +00:00
parent 02cf7cbffa
commit b69e6ca5c1
2 changed files with 58 additions and 0 deletions

View File

@ -38,6 +38,7 @@ import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.NotANumberException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
/**
@ -489,6 +490,22 @@ public class MathArrays {
}
}
/**
* Check that no entry of the input array is {@code NaN}.
*
* @param in Array to be tested.
* @throws NotANumberException if an entry is {@code NaN}.
* @since 3.4
*/
public static void checkNotNaN(final double[] in)
throws NotStrictlyPositiveException {
for(int i = 0; i < in.length; i++) {
if (Double.isNaN(in[i])) {
throw new NotANumberException();
}
}
}
/**
* Check that all entries of the input array are >= 0.
*

View File

@ -24,6 +24,7 @@ import org.apache.commons.math3.exception.NonMonotonicSequenceException;
import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.NotANumberException;
import org.apache.commons.math3.random.Well1024a;
import org.junit.Assert;
import org.junit.Test;
@ -384,6 +385,46 @@ public class MathArraysTest {
}
}
@Test
public void testCheckNotNaN() {
final double[] withoutNaN = { Double.NEGATIVE_INFINITY,
-Double.MAX_VALUE,
-1, 0,
Double.MIN_VALUE,
FastMath.ulp(1d),
1, 3, 113, 4769,
Double.MAX_VALUE,
Double.POSITIVE_INFINITY };
final double[] withNaN = { Double.NEGATIVE_INFINITY,
-Double.MAX_VALUE,
-1, 0,
Double.MIN_VALUE,
FastMath.ulp(1d),
1, 3, 113, 4769,
Double.MAX_VALUE,
Double.POSITIVE_INFINITY,
Double.NaN };
final double[] nullArray = null;
final double[] empty = new double[] {};
MathArrays.checkNotNaN(withoutNaN);
MathArrays.checkNotNaN(empty);
try {
MathArrays.checkNotNaN(nullArray);
Assert.fail("Expecting NullPointerException");
} catch (NullPointerException ex) {
// Expected
}
try {
MathArrays.checkNotNaN(withNaN);
Assert.fail("Expecting NotANumberException");
} catch (NotANumberException ex) {
// Expected
}
}
@Test
public void testSortInPlace() {
final double[] x1 = {2, 5, -3, 1, 4};