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-rng.git
commit d04082e8e1091fb3da0f892ad28576098ac20332 Author: Alex Herbert <[email protected]> AuthorDate: Fri Aug 13 19:31:42 2021 +0100 Use JDK 8 Double.isFinite and Math.nextUp/Down --- .../rng/sampling/distribution/GaussianSampler.java | 3 +- .../sampling/distribution/PoissonSamplerCache.java | 2 +- .../rng/sampling/distribution/StableSampler.java | 3 +- .../commons/rng/sampling/shape/Coordinates.java | 13 +-- .../rng/sampling/UnitSphereSamplerTest.java | 2 +- .../distribution/ContinuousUniformSamplerTest.java | 10 +-- .../distribution/GeometricSamplerTest.java | 2 +- .../distribution/LargeMeanPoissonSamplerTest.java | 2 +- .../MarsagliaTsangWangDiscreteSamplerTest.java | 4 +- .../distribution/PoissonSamplerCacheTest.java | 18 ++-- .../sampling/distribution/StableSamplerTest.java | 98 +++++++--------------- 11 files changed, 54 insertions(+), 103 deletions(-) diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java index 38e0537..7601993 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java @@ -54,8 +54,7 @@ public class GaussianSampler implements SharedStateContinuousSampler { throw new IllegalArgumentException( "standard deviation is not strictly positive and finite: " + standardDeviation); } - // To be replaced by JDK 1.8 Double.isFinite. This will detect NaN values. - if (!(Math.abs(mean) <= Double.MAX_VALUE)) { + if (!Double.isFinite(mean)) { throw new IllegalArgumentException("mean is not finite: " + mean); } this.normalized = normalized; diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java index e935d2b..d918e7d 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java @@ -301,7 +301,7 @@ public class PoissonSamplerCache { */ public double getMaxMean() { if (isValidRange()) { - return Math.nextAfter(maxN + 1.0, -1); + return Math.nextDown(maxN + 1.0); } return 0; } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/StableSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/StableSampler.java index bbf6600..feb86ec 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/StableSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/StableSampler.java @@ -1483,8 +1483,7 @@ public abstract class StableSampler implements SharedStateContinuousSampler { if (!(0 < gamma && gamma <= Double.MAX_VALUE)) { throw new IllegalArgumentException("gamma is not strictly positive and finite: " + gamma); } - // To be replaced by !Double.isFinite(double) from JDK 1.8. - if (!(Math.abs(delta) <= Double.MAX_VALUE)) { + if (!Double.isFinite(delta)) { throw new IllegalArgumentException("delta is not finite: " + delta); } } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/Coordinates.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/Coordinates.java index a1f1fc2..e9a73d9 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/Coordinates.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/Coordinates.java @@ -45,7 +45,7 @@ final class Coordinates { */ static double[] requireFinite(double[] values, String message) { for (final double value : values) { - if (!isFinite(value)) { + if (!Double.isFinite(value)) { throw new IllegalArgumentException(message + " contains non-finite value: " + value); } } @@ -53,17 +53,6 @@ final class Coordinates { } /** - * Checks if the value is finite. - * To be replaced by {@code Double.isFinite(double)} when source requires Java 8. - * - * @param value the value - * @return true if finite - */ - private static boolean isFinite(double value) { - return Math.abs(value) <= Double.MAX_VALUE; - } - - /** * Check that the values is the specified length. This method is primarily for * parameter validation in methods and constructors, for example: * diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/UnitSphereSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/UnitSphereSamplerTest.java index 2a304e8..d731a98 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/UnitSphereSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/UnitSphereSamplerTest.java @@ -443,7 +443,7 @@ public class UnitSphereSamplerTest { public void testNextNormSquaredAfterZeroIsValid() { // The sampler explicitly handles length == 0 using recursion. // Anything above zero should be valid. - final double normSq = Math.nextAfter(0.0, 1); + final double normSq = Math.nextUp(0.0); // Map to the scaling factor final double f = 1 / Math.sqrt(normSq); // As long as this is finite positive then the sampler is valid diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSamplerTest.java index 76d56a4..9db2c6e 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSamplerTest.java @@ -105,10 +105,10 @@ public class ContinuousUniformSamplerTest { {-Double.MIN_VALUE, Double.MIN_VALUE}, // Same signs. Requires one double inside the range. // Same exponent - {1.23, Math.nextAfter(1.23, Double.POSITIVE_INFINITY)}, - {1.23, Math.nextAfter(1.23, Double.NEGATIVE_INFINITY)}, + {1.23, Math.nextUp(1.23)}, + {1.23, Math.nextUp(1.23)}, // Different exponent - {2.0, Math.nextAfter(2.0, Double.NEGATIVE_INFINITY)}, + {2.0, Math.nextDown(2.0)}, }) { final double low = interval[0]; final double high = interval[1]; @@ -145,8 +145,8 @@ public class ContinuousUniformSamplerTest { for (final double expected : new double[] { 1.23, 2, 56787.7893, 3 * x, 2 * x, x }) { - final double low = Math.nextAfter(expected, Double.POSITIVE_INFINITY); - final double high = Math.nextAfter(expected, Double.NEGATIVE_INFINITY); + final double low = Math.nextUp(expected); + final double high = Math.nextDown(expected); Assert.assertEquals(expected, ContinuousUniformSampler.of(rng, low, high, true).sample(), 0.0); Assert.assertEquals(expected, ContinuousUniformSampler.of(rng, high, low, true).sample(), 0.0); Assert.assertEquals(-expected, ContinuousUniformSampler.of(rng, -low, -high, true).sample(), 0.0); diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java index a04d841..53f0585 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java @@ -48,7 +48,7 @@ public class GeometricSamplerTest { public void testProbabilityOfSuccessUnderOneIsValid() { // The sampler explicitly handles probabilityOfSuccess == 1 as an edge case. // Anything under it should be valid for sampling from an ExponentialDistribution. - final double probabilityOfSuccess = Math.nextAfter(1, -1); + final double probabilityOfSuccess = Math.nextDown(1); // Map to the mean final double exponentialMean = 1.0 / (-Math.log1p(-probabilityOfSuccess)); // As long as this is finite positive then the sampler is valid diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSamplerTest.java index c56ae8e..dbdfa6b 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSamplerTest.java @@ -51,7 +51,7 @@ public class LargeMeanPoissonSamplerTest { public void testConstructorThrowsWithMeanBelow1() { final RestorableUniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); - final double mean = Math.nextAfter(1, -1); + final double mean = Math.nextDown(1); LargeMeanPoissonSampler.of(rng, mean); } diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSamplerTest.java index e2e2b9e..8e3573c 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSamplerTest.java @@ -467,7 +467,7 @@ public class MarsagliaTsangWangDiscreteSamplerTest { // Validate set-up Assert.assertEquals("Invalid test set-up for p(0)", Double.MIN_VALUE, getBinomialP0(trials, p), 0); - // Search for larger p until Math.nextAfter(p, 1) produces 0 + // Search for larger p until Math.nextUp(p) produces 0 double upper = p * 2; Assert.assertEquals("Invalid test set-up for p(0)", 0, getBinomialP0(trials, upper), 0); @@ -484,7 +484,7 @@ public class MarsagliaTsangWangDiscreteSamplerTest { // Re-validate Assert.assertEquals("Invalid test set-up for p(0)", Double.MIN_VALUE, getBinomialP0(trials, p), 0); - Assert.assertEquals("Invalid test set-up for p(0)", 0, getBinomialP0(trials, Math.nextAfter(p, 1)), 0); + Assert.assertEquals("Invalid test set-up for p(0)", 0, getBinomialP0(trials, Math.nextUp(p)), 0); final DiscreteSampler sampler = MarsagliaTsangWangDiscreteSampler.Binomial.of(rng, trials, p); // This will throw if the table does not sum to 2^30 diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java index 7ce5c00..85490e9 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java @@ -75,7 +75,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(min, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -91,7 +91,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(min, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -101,7 +101,7 @@ public class PoissonSamplerCacheTest { @Test(expected = IllegalArgumentException.class) public void testConstructorThrowsWhenMaxIsLessThanMin() { final double min = PoissonSampler.PIVOT; - final double max = Math.nextAfter(min, -1); + final double max = Math.nextDown(min); createPoissonSamplerCache(min, max); } @@ -116,7 +116,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(PoissonSampler.PIVOT, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -161,7 +161,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache().withRange(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(min, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -177,7 +177,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache().withRange(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(min, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -187,7 +187,7 @@ public class PoissonSamplerCacheTest { @Test(expected = IllegalArgumentException.class) public void testWithRangeConstructorThrowsWhenMaxIsLessThanMin() { final double min = PoissonSampler.PIVOT; - final double max = Math.nextAfter(min, -1); + final double max = Math.nextDown(min); createPoissonSamplerCache().withRange(min, max); } @@ -202,7 +202,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache().withRange(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(PoissonSampler.PIVOT, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } @@ -216,7 +216,7 @@ public class PoissonSamplerCacheTest { final PoissonSamplerCache cache = createPoissonSamplerCache(0, 0).withRange(min, max); Assert.assertTrue(cache.isValidRange()); Assert.assertEquals(min, cache.getMinMean(), 0); - Assert.assertEquals(Math.nextAfter(Math.floor(max) + 1, -1), + Assert.assertEquals(Math.nextDown(Math.floor(max) + 1), cache.getMaxMean(), 0); } diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java index f56dbc7..efe2c02 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java @@ -65,7 +65,7 @@ public class StableSamplerTest { /** A largest sample from the ZigguratSampler.Exponential after 4 recursions of the sample method. */ private static final double LARGE_W = 4 * TAIL_W; /** The smallest value for alpha where 1 - (1-alpha) = alpha. */ - private static final double SMALLEST_ALPHA = 1.0 - nextDown(1.0); + private static final double SMALLEST_ALPHA = 1.0 - Math.nextDown(1.0); private static final double VALID_ALPHA = 1.23; private static final double VALID_BETA = 0.23; @@ -79,7 +79,7 @@ public class StableSamplerTest { @Test(expected = IllegalArgumentException.class) public void testAlphaBelowZeroThrows() { - createStableSampler(nextDown(0.0), VALID_BETA, VALID_GAMMA, VALID_DELTA); + createStableSampler(Math.nextDown(0.0), VALID_BETA, VALID_GAMMA, VALID_DELTA); } @Test(expected = IllegalArgumentException.class) @@ -101,7 +101,7 @@ public class StableSamplerTest { @Test(expected = IllegalArgumentException.class) public void testAlphaAboveTwoThrows() { - createStableSampler(nextUp(2.0), VALID_BETA, VALID_GAMMA, VALID_DELTA); + createStableSampler(Math.nextUp(2.0), VALID_BETA, VALID_GAMMA, VALID_DELTA); } @Test(expected = IllegalArgumentException.class) @@ -111,12 +111,12 @@ public class StableSamplerTest { @Test(expected = IllegalArgumentException.class) public void testBetaBelowMinusOneThrows() { - createStableSampler(VALID_ALPHA, nextDown(-1.0), VALID_GAMMA, VALID_DELTA); + createStableSampler(VALID_ALPHA, Math.nextDown(-1.0), VALID_GAMMA, VALID_DELTA); } @Test(expected = IllegalArgumentException.class) public void testBetaAboveOneThrows() { - createStableSampler(VALID_ALPHA, nextUp(1.0), VALID_GAMMA, VALID_DELTA); + createStableSampler(VALID_ALPHA, Math.nextUp(1.0), VALID_GAMMA, VALID_DELTA); } @Test(expected = IllegalArgumentException.class) @@ -224,15 +224,15 @@ public class StableSamplerTest { Assert.assertEquals(-0.5, CMSStableSampler.getTau(0.5, -beta), 0.0); // Check monototic at the transition point to switch to a different computation. - final double tau1 = CMSStableSampler.getTau(nextDown(1.5), 1); + final double tau1 = CMSStableSampler.getTau(Math.nextDown(1.5), 1); final double tau2 = CMSStableSampler.getTau(1.5, 1); - final double tau3 = CMSStableSampler.getTau(nextUp(1.5), 1); + final double tau3 = CMSStableSampler.getTau(Math.nextUp(1.5), 1); Assert.assertTrue(tau1 > tau2); Assert.assertTrue(tau2 > tau3); // Test symmetry at the transition - Assert.assertEquals(tau1, CMSStableSampler.getTau(2 - nextDown(1.5), 1), 0.0); + Assert.assertEquals(tau1, CMSStableSampler.getTau(2 - Math.nextDown(1.5), 1), 0.0); Assert.assertEquals(tau2, CMSStableSampler.getTau(0.5, 1), 0.0); - Assert.assertEquals(tau3, CMSStableSampler.getTau(2 - nextUp(1.5), 1), 0.0); + Assert.assertEquals(tau3, CMSStableSampler.getTau(2 - Math.nextUp(1.5), 1), 0.0); } /** @@ -292,7 +292,7 @@ public class StableSamplerTest { // phiby2 in (-pi/4, pi/4) // a in (-1, 1) final double a = phiby2 * SpecialMath.tan2(phiby2); - Assert.assertEquals(Math.copySign(nextDown(1.0), phiby2), a, 0.0); + Assert.assertEquals(Math.copySign(Math.nextDown(1.0), phiby2), a, 0.0); final double da = a * a; final double a2 = 1 - da; // The number is close to but not equal to zero @@ -388,8 +388,8 @@ public class StableSamplerTest { Assert.assertEquals(0.0, computeNumerator(2, beta, x00), 0.0); Assert.assertTrue(0.0 < computeNumerator(2, beta, x0)); Assert.assertTrue(0.0 < computeNumerator(2, beta, x1)); - Assert.assertTrue(0.0 < computeNumerator(nextDown(2), beta, x0)); - Assert.assertTrue(0.0 < computeNumerator(nextDown(2), beta, x1)); + Assert.assertTrue(0.0 < computeNumerator(Math.nextDown(2), beta, x0)); + Assert.assertTrue(0.0 < computeNumerator(Math.nextDown(2), beta, x1)); // alpha=1 case the numerator reduces to: // 1 + 2 * phi/2 * tau @@ -409,7 +409,7 @@ public class StableSamplerTest { // beta < 1 => u < 0 // beta > -1 => u > 1 // z=0 not possible with any other beta - Assert.assertTrue(0.0 < computeNumerator(alpha, nextUp(-1), x00)); + Assert.assertTrue(0.0 < computeNumerator(alpha, Math.nextUp(-1), x00)); } /** @@ -539,7 +539,7 @@ public class StableSamplerTest { final UniformRandomProvider rng = RandomSource.XO_SHI_RO_256_SS.create(); // The term is mirrored around 1 so use extremes between 1 and 0 - final double[] alphas = {1, nextDown(1), 0.99, 0.5, 0.1, 0.05, 0.01, DU}; + final double[] alphas = {1, Math.nextDown(1), 0.99, 0.5, 0.1, 0.05, 0.01, DU}; // Longs to generate extremes for the angle phi. This is mirrored // by negation is the assert method so use values to create phi in [0, pi/2). final long[] xs = {0, 1 << 10, Long.MIN_VALUE >>> 1, Long.MAX_VALUE}; @@ -668,13 +668,13 @@ public class StableSamplerTest { // Test monotonic at the switch point Assert.assertEquals(d2(0.5), d2b(0.5), 0.0); // When positive x -> 0 the value smaller bigger. - Assert.assertTrue(d2(nextDown(0.5)) <= d2b(0.5)); + Assert.assertTrue(d2(Math.nextDown(0.5)) <= d2b(0.5)); Assert.assertEquals(d2(-0.5), d2b(-0.5), 0.0); // When negative x -> 0 the value gets bigger. - Assert.assertTrue(d2(-nextDown(0.5)) >= d2b(-0.5)); + Assert.assertTrue(d2(-Math.nextDown(0.5)) >= d2b(-0.5)); // Potentially the next power of 2 could be used based on ULP errors but // the switch is not monotonic. - Assert.assertFalse(d2(nextDown(0.25)) <= d2b(0.25)); + Assert.assertFalse(d2(Math.nextDown(0.25)) <= d2b(0.25)); } /** @@ -899,9 +899,9 @@ public class StableSamplerTest { Assert.assertEquals(1.0, PI_4 * SpecialMath.tan2(PI_4), Math.ulp(1.0)); // If this is above 1 then the sampler will break. Test at the switch point pi/4. Assert.assertTrue(1.0 >= PI_4 * SpecialMath.tan2(PI_4)); - Assert.assertTrue(1.0 >= PI_4 * SpecialMath.tan2(nextDown(PI_4))); + Assert.assertTrue(1.0 >= PI_4 * SpecialMath.tan2(Math.nextDown(PI_4))); // Monotonic function at the transition - Assert.assertTrue(SpecialMath.tan2(nextUp(PI_4)) >= SpecialMath.tan2(PI_4)); + Assert.assertTrue(SpecialMath.tan2(Math.nextUp(PI_4)) >= SpecialMath.tan2(PI_4)); } /** @@ -991,7 +991,7 @@ public class StableSamplerTest { for (final double u : us) { // CMS formulas double x0 = sampleCMS(1, beta, w, u); - Assert.assertTrue("Target must be finite", isFinite(x0)); + Assert.assertTrue("Target must be finite", Double.isFinite(x0)); // Sample should approach x0 as alpha approaches 1 double delta = deltaStart; @@ -1007,7 +1007,7 @@ public class StableSamplerTest { // Weron formulas x0 = sampleWeronAlpha1(beta, w, u); - Assert.assertTrue("Target must be finite", isFinite(x0)); + Assert.assertTrue("Target must be finite", Double.isFinite(x0)); // Sample should approach x0 as alpha approaches 1 delta = deltaStart; @@ -1042,11 +1042,11 @@ public class StableSamplerTest { public void testExtremeInputsToSample() { // Demonstrate instability when w = 0 Assert.assertEquals(Double.NaN, sampleCMS(1.3, 0.7, 0, 0.25), 0.0); - Assert.assertTrue(isFinite(sampleCMS(1.3, 0.7, SMALL_W, 0.25))); + Assert.assertTrue(Double.isFinite(sampleCMS(1.3, 0.7, SMALL_W, 0.25))); // Demonstrate instability when u -> 0 or 1, and |beta| = 1 Assert.assertEquals(Double.NaN, sampleCMS(1.1, 1.0, 0.1, 0), 0.0); - Assert.assertTrue(isFinite(sampleCMS(1.1, 1.0, 0.1, DU))); + Assert.assertTrue(Double.isFinite(sampleCMS(1.1, 1.0, 0.1, DU))); // Demonstrate instability when alpha -> 0 @@ -1058,7 +1058,7 @@ public class StableSamplerTest { Assert.assertEquals(Double.NaN, sampleCMS(1e-5, 0.7, 1.0, 1e-4), 0.0); Assert.assertEquals(Double.NaN, sampleCMS(1e-5, -0.7, 1.0, 1 - 1e-4), 0.0); - final double[] alphas = {nextDown(2), 1.3, 1.1, nextUp(1), 1, nextDown(1), 0.7, 0.1, 0.05, 0.01, 0x1.0p-16}; + final double[] alphas = {Math.nextDown(2), 1.3, 1.1, Math.nextUp(1), 1, Math.nextDown(1), 0.7, 0.1, 0.05, 0.01, 0x1.0p-16}; final double[] betas = {1, 0.9, 0.001, 0}; // Avoid zero for the exponential sample. // Test the smallest non-zero sample from the ArhensDieter exponential sampler, @@ -1375,7 +1375,7 @@ public class StableSamplerTest { final StableSampler sampler = StableSampler.of(createRngWithSequence(longs), alpha, beta); // It should not be NaN or infinite - Assert.assertTrue("Sampler did not recover", isFinite(sampler.sample())); + Assert.assertTrue("Sampler did not recover", Double.isFinite(sampler.sample())); } /** @@ -1510,7 +1510,7 @@ public class StableSamplerTest { }, longs1); final StableSampler sampler1 = StableSampler.of(createRngWithSequence(longs1), 1.0, 1.0); final double x1 = sampler1.sample(); - Assert.assertTrue("Sampler did not recover", isFinite(x1)); + Assert.assertTrue("Sampler did not recover", Double.isFinite(x1)); // u -> pi/4 final long[] longs2 = {Long.MAX_VALUE, 1446480648965178882L}; @@ -1519,7 +1519,7 @@ public class StableSamplerTest { }, longs2); final StableSampler sampler2 = StableSampler.of(createRngWithSequence(longs2), 1.0, -1.0); final double x2 = sampler2.sample(); - Assert.assertTrue("Sampler did not recover", isFinite(x2)); + Assert.assertTrue("Sampler did not recover", Double.isFinite(x2)); // Sample should be a reflection Assert.assertEquals(x1, -x2, 0.0); @@ -1562,7 +1562,7 @@ public class StableSamplerTest { */ private static void testSupport(double gamma, double delta) { // When alpha is small (<=0.1) the computation becomes limited by floating-point precision. - final double[] alphas = {2.0, 1.5, 1.0, nextDown(1), 0.99, 0.75, 0.5, 0.25, 0.1, 0.01}; + final double[] alphas = {2.0, 1.5, 1.0, Math.nextDown(1), 0.99, 0.75, 0.5, 0.25, 0.1, 0.01}; for (final double alpha : alphas) { testSupport(alpha, 1, gamma, delta); testSupport(alpha, -1, gamma, delta); @@ -1689,7 +1689,7 @@ public class StableSamplerTest { if (alpha > 1) { mu = beta * Math.tan((2 - meps1) * PI_2); } else { - // Special case where tan(pi/4) is not 1 (it is nextDown(1.0)). + // Special case where tan(pi/4) is not 1 (it is Math.nextDown(1.0)). // This is needed when testing the Levy case during sampling. if (alpha == 0.5) { mu = -beta; @@ -2004,7 +2004,7 @@ public class StableSamplerTest { // Alpha 1 case testSharedStateSampler(1.0, 0.23); // Alpha close to 1 - testSharedStateSampler(nextUp(1.0), 0.23); + testSharedStateSampler(Math.nextUp(1.0), 0.23); // General case testSharedStateSampler(1.3, 0.1); // Small alpha cases @@ -2053,7 +2053,7 @@ public class StableSamplerTest { // Alpha 1 case testTransformedSampler(1.0, 0.23); // Alpha close to 1 - testTransformedSampler(nextUp(1.0), 0.23); + testTransformedSampler(Math.nextUp(1.0), 0.23); // General case testTransformedSampler(1.3, 0.1); // Small alpha case @@ -2428,40 +2428,4 @@ public class StableSamplerTest { Assert.assertEquals(zeta, sampler.sample(), 0.0); Assert.assertEquals(zeta, sampler.sample(), 0.0); } - - /** - * Checks if the value is finite. - * - * <p>To be replaced by {@code Double.isFinite(double)} from JDK 1.8. - * - * @param value the value - * @return true if finite - */ - private static boolean isFinite(double value) { - return Math.abs(value) <= Double.MAX_VALUE; - } - - /** - * Return the next value after {@code x} in the direction of positive infinity. - * - * <p>To be replaced by Math.nextUp(double) from JDK 1.8. - * - * @param x the x - * @return the next value up - */ - private static double nextUp(double x) { - return Math.nextAfter(x, Double.POSITIVE_INFINITY); - } - - /** - * Return the next value after {@code x} in the direction of negative infinity. - * - * <p>To be replaced by Math.nextDown(double) from JDK 1.8. - * - * @param x the x - * @return the next value down - */ - private static double nextDown(double x) { - return Math.nextAfter(x, Double.NEGATIVE_INFINITY); - } }
