diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/MullerSolver.java b/src/main/java/org/apache/commons/math/analysis/solvers/MullerSolver.java index 9077109f4..4d8977544 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/MullerSolver.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/MullerSolver.java @@ -17,7 +17,6 @@ package org.apache.commons.math.analysis.solvers; import org.apache.commons.math.util.FastMath; -import org.apache.commons.math.util.MathUtils; /** * This class implements the @@ -181,7 +180,7 @@ public class MullerSolver extends AbstractUnivariateRealSolver { } else { double xm = 0.5 * (x0 + x2); double ym = computeObjectiveValue(xm); - if (MathUtils.sign(y0) + MathUtils.sign(ym) == 0.0) { + if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) { x2 = xm; y2 = ym; } else { x0 = xm; y0 = ym; diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java b/src/main/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java index c999d15b8..664e10df6 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/RiddersSolver.java @@ -17,7 +17,6 @@ package org.apache.commons.math.analysis.solvers; import org.apache.commons.math.util.FastMath; -import org.apache.commons.math.util.MathUtils; /** * Implements the @@ -97,7 +96,7 @@ public class RiddersSolver extends AbstractUnivariateRealSolver { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing - final double correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) * + final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); @@ -114,7 +113,7 @@ public class RiddersSolver extends AbstractUnivariateRealSolver { // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 - if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) { + if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { @@ -124,7 +123,7 @@ public class RiddersSolver extends AbstractUnivariateRealSolver { y2 = y3; } } else { // x3 < x < x2 - if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) { + if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { diff --git a/src/main/java/org/apache/commons/math/util/MathUtils.java b/src/main/java/org/apache/commons/math/util/MathUtils.java index bfafe3563..f61fc49fb 100644 --- a/src/main/java/org/apache/commons/math/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math/util/MathUtils.java @@ -418,43 +418,6 @@ public final class MathUtils { return (x == ZB) ? ZB : (x > ZB) ? PB : NB; } - /** - * Returns the sign - * for double precision {@code x}. - *

- * For a double value {@code x}, this method returns - * {@code +1.0} if {@code x > 0}, {@code 0.0} if - * {@code x = 0.0}, and {@code -1.0} if {@code x < 0}. - * Returns {@code NaN} if {@code x} is {@code NaN}.

- * - * @param x the value, a double - * @return +1.0, 0.0, or -1.0, depending on the sign of x - */ - public static double sign(final double x) { - if (Double.isNaN(x)) { - return Double.NaN; - } - return (x == 0.0) ? 0.0 : (x > 0.0) ? 1.0 : -1.0; - } - - /** - * Returns the sign - * for float value {@code x}. - *

- * For a float value x, this method returns +1.0F if x > 0, 0.0F if x = - * 0.0F, and -1.0F if x < 0. Returns {@code NaN} if {@code x} - * is {@code NaN}.

- * - * @param x the value, a float - * @return +1.0F, 0.0F, or -1.0F, depending on the sign of x - */ - public static float sign(final float x) { - if (Float.isNaN(x)) { - return Float.NaN; - } - return (x == 0.0F) ? 0.0F : (x > 0.0F) ? 1.0F : -1.0F; - } - /** * Returns the sign * for int value {@code x}. diff --git a/src/test/java/org/apache/commons/math/util/FastMathTest.java b/src/test/java/org/apache/commons/math/util/FastMathTest.java index f5f5f8df5..89cefbce9 100644 --- a/src/test/java/org/apache/commons/math/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math/util/FastMathTest.java @@ -25,6 +25,7 @@ import org.apache.commons.math.dfp.DfpField; import org.apache.commons.math.dfp.DfpMath; import org.apache.commons.math.random.MersenneTwister; import org.apache.commons.math.random.RandomGenerator; +import org.apache.commons.math.TestUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; @@ -1060,4 +1061,21 @@ public class FastMathTest { compareClassMethods( FastMath.class, StrictMath.class); } + @Test + public void testSignumDouble() { + final double delta = 0.0; + Assert.assertEquals(1.0, FastMath.signum(2.0), delta); + Assert.assertEquals(0.0, FastMath.signum(0.0), delta); + Assert.assertEquals(-1.0, FastMath.signum(-2.0), delta); + TestUtils.assertSame(-0. / 0., FastMath.signum(Double.NaN)); + } + + @Test + public void testSignumFloat() { + final float delta = 0.0F; + Assert.assertEquals(1.0F, FastMath.signum(2.0F), delta); + Assert.assertEquals(0.0F, FastMath.signum(0.0F), delta); + Assert.assertEquals(-1.0F, FastMath.signum(-2.0F), delta); + TestUtils.assertSame(Float.NaN, FastMath.signum(Float.NaN)); + } } diff --git a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java index ef129caee..a7b6ade2d 100644 --- a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java @@ -454,24 +454,6 @@ public final class MathUtilsTest { Assert.assertEquals((byte) (-1), MathUtils.sign((byte) (-2))); } - @Test - public void testSignDouble() { - double delta = 0.0; - Assert.assertEquals(1.0, MathUtils.sign(2.0), delta); - Assert.assertEquals(0.0, MathUtils.sign(0.0), delta); - Assert.assertEquals(-1.0, MathUtils.sign(-2.0), delta); - TestUtils.assertSame(-0. / 0., MathUtils.sign(Double.NaN)); - } - - @Test - public void testSignFloat() { - float delta = 0.0F; - Assert.assertEquals(1.0F, MathUtils.sign(2.0F), delta); - Assert.assertEquals(0.0F, MathUtils.sign(0.0F), delta); - Assert.assertEquals(-1.0F, MathUtils.sign(-2.0F), delta); - TestUtils.assertSame(Float.NaN, MathUtils.sign(Float.NaN)); - } - @Test public void testSignInt() { Assert.assertEquals(1, MathUtils.sign(2));