diff --git a/src/main/java/org/apache/commons/math/util/FastMath.java b/src/main/java/org/apache/commons/math/util/FastMath.java index b338c5124..7b43d371a 100644 --- a/src/main/java/org/apache/commons/math/util/FastMath.java +++ b/src/main/java/org/apache/commons/math/util/FastMath.java @@ -1389,6 +1389,26 @@ public class FastMath { return rln10b * lnb + rln10b * lna + rln10a * lnb + rln10a * lna; } + /** + * Computes the + * logarithm in a given base. + * + * Returns {@code NaN} if either argument is negative. + * If {@code base} is 0 and {@code x} is positive, 0 is returned. + * If {@code base} is positive and {@code x} is 0, + * {@code Double.NEGATIVE_INFINITY} is returned. + * If both arguments are 0, the result is {@code NaN}. + * + * @param base Base of the logarithm, must be greater than 0. + * @param x Argument, must be greater than 0. + * @return the value of the logarithm, i.e. the number {@code y} such that + * basey = x. + * @since 1.2 (previously in {@code MathUtils}, moved as of version 3.0) + */ + public static double log(double base, double x) { + return log(x) / log(base); + } + /** * Power function. Compute x^y. * 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 d24459adb..947e36f37 100644 --- a/src/main/java/org/apache/commons/math/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math/util/MathUtils.java @@ -165,26 +165,6 @@ public final class MathUtils { return (x >= ZS) ? PS : NS; } - /** - *

Returns the - * logarithm - * for base {@code b} of {@code x}. - *

- *

Returns {@code NaN} if either argument is negative. If - * {@code base} is 0 and {@code x} is positive, 0 is returned. - * If {@code base} is positive and {@code x} is 0, - * {@code Double.NEGATIVE_INFINITY} is returned. If both arguments - * are 0, the result is {@code NaN}.

- * - * @param base the base of the logarithm, must be greater than 0 - * @param x argument, must be greater than 0 - * @return the value of the logarithm - the number y such that base^y = x. - * @since 1.2 - */ - public static double log(double base, double x) { - return FastMath.log(x)/FastMath.log(base); - } - /** * Normalize an angle in a 2&pi wide interval around a center value. *

This method has three main uses:

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 89cefbce9..4847021e7 100644 --- a/src/test/java/org/apache/commons/math/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math/util/FastMathTest.java @@ -1078,4 +1078,15 @@ public class FastMathTest { Assert.assertEquals(-1.0F, FastMath.signum(-2.0F), delta); TestUtils.assertSame(Float.NaN, FastMath.signum(Float.NaN)); } + + @Test + public void testLogWithBase() { + Assert.assertEquals(2.0, FastMath.log(2, 4), 0); + Assert.assertEquals(3.0, FastMath.log(2, 8), 0); + Assert.assertTrue(Double.isNaN(FastMath.log(-1, 1))); + Assert.assertTrue(Double.isNaN(FastMath.log(1, -1))); + Assert.assertTrue(Double.isNaN(FastMath.log(0, 0))); + Assert.assertEquals(0, FastMath.log(0, 10), 0); + Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.log(10, 0), 0); + } } 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 bfd1578f6..f6c28ff3a 100644 --- a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java @@ -150,17 +150,6 @@ public final class MathUtilsTest { Assert.assertEquals((short)(-1), MathUtils.indicator((short)(-2))); } - @Test - public void testLog() { - Assert.assertEquals(2.0, MathUtils.log(2, 4), 0); - Assert.assertEquals(3.0, MathUtils.log(2, 8), 0); - Assert.assertTrue(Double.isNaN(MathUtils.log(-1, 1))); - Assert.assertTrue(Double.isNaN(MathUtils.log(1, -1))); - Assert.assertTrue(Double.isNaN(MathUtils.log(0, 0))); - Assert.assertEquals(0, MathUtils.log(0, 10), 0); - Assert.assertEquals(Double.NEGATIVE_INFINITY, MathUtils.log(10, 0), 0); - } - @Test public void testNormalizeAngle() { for (double a = -15.0; a <= 15.0; a += 0.1) {