diff --git a/src/java/org/apache/commons/math/util/MathUtils.java b/src/java/org/apache/commons/math/util/MathUtils.java index b6e99e1a9..b23b58aa2 100644 --- a/src/java/org/apache/commons/math/util/MathUtils.java +++ b/src/java/org/apache/commons/math/util/MathUtils.java @@ -117,7 +117,26 @@ public final class MathUtils { } return result; } - + + /** + *

Returns the + * logarithm + * for base b of x. + *

+ *

Returns NaN if either argument is negative. If + * base is 0 and x is positive, 0 is returned. + * If base is positive and x is 0, + * Double.NEGATIVE_INFINITY is returned. If both arguments + * are 0, the result is 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. + */ + public static double log(double base, double x) { + return Math.log(x)/Math.log(base); + } + /** * Returns a double representation of the Binomial diff --git a/src/test/org/apache/commons/math/util/MathUtilsTest.java b/src/test/org/apache/commons/math/util/MathUtilsTest.java index 0c94041a7..376371765 100644 --- a/src/test/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/org/apache/commons/math/util/MathUtilsTest.java @@ -444,6 +444,16 @@ public final class MathUtilsTest extends TestCase { } } + public void testLog() { + assertEquals(2.0, MathUtils.log(2,4), 0); + assertEquals(3.0, MathUtils.log(2,8), 0); + assertTrue(Double.isNaN(MathUtils.log(-1, 1))); + assertTrue(Double.isNaN(MathUtils.log(1, -1))); + assertTrue(Double.isNaN(MathUtils.log(0, 0))); + assertEquals(0, MathUtils.log(0, 10), 0); + assertEquals(Double.NEGATIVE_INFINITY, MathUtils.log(10, 0), 0); + } + public void testGcd() { int a = 30; int b = 50; diff --git a/xdocs/changes.xml b/xdocs/changes.xml index 441a7a262..512a28eeb 100644 --- a/xdocs/changes.xml +++ b/xdocs/changes.xml @@ -81,6 +81,9 @@ Commons Math Release Notes Increased default precision of Gamma and Beta functions. + + Added log function to MathUtils. +