Removed "sign(float)" and "sign(double)" from "MathUtils"; replaced uses by
calls to "signum" in "FastMath".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1183138 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-10-13 22:21:04 +00:00
parent 41c29f826d
commit a5d473fe2f
5 changed files with 22 additions and 61 deletions

View File

@ -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 <a href="http://mathworld.wolfram.com/MullersMethod.html">
@ -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;

View File

@ -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 <a href="http://mathworld.wolfram.com/RiddersMethod.html">
@ -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 {

View File

@ -418,43 +418,6 @@ public final class MathUtils {
return (x == ZB) ? ZB : (x > ZB) ? PB : NB;
}
/**
* Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
* for double precision {@code x}.
* <p>
* 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}.</p>
*
* @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 <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
* for float value {@code x}.
* <p>
* 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}.</p>
*
* @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 <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
* for int value {@code x}.

View File

@ -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));
}
}

View File

@ -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));