Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 21467 invoked from network); 6 Jun 2003 03:07:43 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 6 Jun 2003 03:07:43 -0000 Received: (qmail 9735 invoked by uid 97); 6 Jun 2003 03:10:06 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 9728 invoked from network); 6 Jun 2003 03:10:05 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 6 Jun 2003 03:10:05 -0000 Received: (qmail 21248 invoked by uid 500); 6 Jun 2003 03:07:40 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 21237 invoked by uid 500); 6 Jun 2003 03:07:40 -0000 Received: (qmail 21230 invoked from network); 6 Jun 2003 03:07:40 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 6 Jun 2003 03:07:40 -0000 Received: (qmail 31311 invoked by uid 1674); 6 Jun 2003 03:07:39 -0000 Date: 6 Jun 2003 03:07:39 -0000 Message-ID: <20030606030739.31310.qmail@icarus.apache.org> From: mdiggory@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math MathUtilsTest.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N mdiggory 2003/06/05 20:07:39 Modified: math/src/java/org/apache/commons/math MathUtils.java math/src/test/org/apache/commons/math MathUtilsTest.java Log: PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20496 Submitted by: Albert Davidson Chou Revision Changes Path 1.2 +175 -75 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/MathUtils.java Index: MathUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/MathUtils.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MathUtils.java 4 Jun 2003 02:31:13 -0000 1.1 +++ MathUtils.java 6 Jun 2003 03:07:39 -0000 1.2 @@ -14,7 +14,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the - * distribution. + * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: @@ -63,26 +63,126 @@ public class MathUtils { /** - * Returns an exact representation of the - * - * Binomial Coefficient, "n choose k", - * the number of k-element subsets that can be selected from - * an n-element set. - *

- * Preconditions:

    - *
  • 0 < k <= n (otherwise - * IllegalArgumentException is thrown)
  • - *
  • The result is small enough to fit into a long. The - * largest value of n for which all coefficients are - * < Long.MAX_VALUE is 66. If the computed value - * exceeds Long.MAX_VALUE an ArithMeticException - * is thrown.
  • - *
- * - * @param n the size of the set - * @param k the size of the subsets to be counted - * @return n choose k + * For a double precision value x, this method returns +1.0 if x >= 0 + * and -1.0 if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, a double + * @return +1.0 or -1.0, depending on the the sign of x + */ + public static double sign( double x ) { + if ( x >= 0.0 ) { + return 1.0 ; + } else { + return -1.0 ; + } + } + + /** + * For a float value x, this method returns +1.0F if x >= 0 + * and -1.0F if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, a float + * @return +1.0F or -1.0F, depending on the the sign of x + */ + public static float sign( float x ) { + if ( x >= 0.0F ) { + return 1.0F ; + } else { + return -1.0F ; + } + } + + /** + * For a byte value x, this method returns (byte)(+1) if x >= 0 + * and (byte)(-1) if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, a byte + * @return (byte)(+1) or (byte)(-1), depending on the the sign of x + */ + public static byte sign( byte x ) { + if ( x >= (byte)0 ) { + return (byte)1 ; + } else { + return (byte)(-1) ; + } + } + + /** + * For a short value x, this method returns (short)(+1) if x >= 0 + * and (short)(-1) if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, a short + * @return (short)(+1) or (short)(-1), depending on the the sign of x + */ + public static short sign( short x ) { + if ( x >= (short)0 ) { + return (short)1 ; + } else { + return (short)(-1) ; + } + } + + /** + * For an int value x, this method returns +1 if x >= 0 + * and -1 if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, an int + * @return +1 or -1, depending on the the sign of x */ + public static int sign( int x ) { + if ( x >= 0 ) { + return 1 ; + } else { + return -1 ; + } + } + + /** + * For a long value x, this method returns +1L if x >= 0 + * and -1L if x < 0. + * + * @author Albert Davidson Chou + * @param x the value, a long + * @return +1L or -1L, depending on the the sign of x + */ + public static long sign( long x ) { + if ( x >= 0L ) { + return 1L ; + } else { + return -1L ; + } + } + /** + * Returns an exact representation of the + * + * Binomial Coefficient, "n choose k", + * the number of k-element subsets that can be selected from + * an n-element set. + *

+ * Preconditions:

    + *
  • 0 < k <= n (otherwise + *
  • 0 < k <= n (otherwise + * IllegalArgumentException is thrown)
  • + *
  • The result is small enough to fit into a long. The + * largest value of n for which all coefficients are + * < Long.MAX_VALUE is 66. If the computed value + *
  • The result is small enough to fit into a long. The + * largest value of n for which all coefficients are + * < Long.MAX_VALUE is 66. If the computed value + * exceeds Long.MAX_VALUE an ArithMeticException + * is thrown.
  • + *
+ * + * + * @param n the size of the set + * @param k the size of the subsets to be counted + * @return n choose k + */ public static long binomialCoefficient(int n, int k) { if (n < k) { throw new IllegalArgumentException @@ -98,51 +198,51 @@ if ((k == 1) || (k == n - 1)) { return n; } - + long result = Math.round(binomialCoefficientDouble(n, k)); if (result == Long.MAX_VALUE) { throw new ArithmeticException ("result too large to represent in a long integer"); } - return result; - } - - /** - * Returns a double representation of the - * - * Binomial Coefficient, "n choose k", - * the number of k-element subsets that can be selected from + return result; + } + + /** + * Returns a double representation of the + * + * Binomial Coefficient, "n choose k", + * the number of k-element subsets that can be selected from * an n-element set. *

* Preconditions:

    - *
  • 0 < k <= n (otherwise + *
  • 0 < k <= n (otherwise * IllegalArgumentException is thrown)
  • - *
  • The result is small enough to fit into a double. - * The largest value of n for which all coefficients are - * < Double.MAX_VALUE is 1029. If the computed value exceeds + *
  • The result is small enough to fit into a double. + * The largest value of n for which all coefficients are + * < Double.MAX_VALUE is 1029. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
  • *
- * + * * @param n the size of the set * @param k the size of the subsets to be counted * @return n choose k */ - public static double binomialCoefficientDouble(int n, int k) { - return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5); + public static double binomialCoefficientDouble(int n, int k) { + return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5); } - + /** * Returns the natural log of the - * - * Binomial Coefficient, "n choose k", - * the number of k-element subsets that can be selected from + * + * Binomial Coefficient, "n choose k", + * the number of k-element subsets that can be selected from * an n-element set. *

* Preconditions:

    - *
  • 0 < k <= n (otherwise + *
  • 0 < k <= n (otherwise * IllegalArgumentException is thrown)
  • *
- * + * * @param n the size of the set * @param k the size of the subsets to be counted * @return n choose k @@ -161,38 +261,38 @@ } if ((k == 1) || (k == n - 1)) { return Math.log((double) n); - } - double logSum = 0; - + } + double logSum = 0; + // n!/k! for (int i = k + 1; i <= n; i++) { logSum += Math.log((double) i); } - + // divide by (n-k)! for (int i = 2; i <= n - k; i++) { logSum -= Math.log((double) i); } - + return logSum; } - + /** * Returns n - * - * Factorial, or n!, + * + * Factorial, or n!, * the product of the numbers 1,...,n. *

* Preconditions:

    - *
  • n > 0 (otherwise + *
  • n > 0 (otherwise * IllegalArgumentException is thrown)
  • - *
  • The result is small enough to fit into a long. The - * largest value of n for which n! - * < Long.MAX_VALUE is 20. If the computed value + *
  • The result is small enough to fit into a long. The + * largest value of n for which n! + * < Long.MAX_VALUE is 20. If the computed value * exceeds Long.MAX_VALUE an ArithMeticException * is thrown.
  • *
- * + * * @param n argument * @return n! */ @@ -202,25 +302,25 @@ throw new ArithmeticException ("result too large to represent in a long integer"); } - return result; + return result; } - + /** * Returns n - * - * Factorial, or n!, - * the product of the numbers 1,...,n, as as + * + * Factorial, or n!, + * the product of the numbers 1,...,n, as as * double. *

* Preconditions:

    - *
  • n > 0 (otherwise + *
  • n > 0 (otherwise * IllegalArgumentException is thrown)
  • - *
  • The result is small enough to fit into a double. The - * largest value of n for which n! - * < Double.MAX_VALUE is 170. If the computed value exceeds + *
  • The result is small enough to fit into a double. The + * largest value of n for which n! + * < Double.MAX_VALUE is 170. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
  • *
- * + * * @param n argument * @return n! */ @@ -229,21 +329,21 @@ throw new IllegalArgumentException ("must have n > 0 for n!"); } - return Math.floor(Math.exp(factorialLog(n)) + 0.5); + return Math.floor(Math.exp(factorialLog(n)) + 0.5); } - + /** * Returns the natural log of n - * - * Factorial, or n!, - * the product of the numbers 1,...,n, as as + * + * Factorial, or n!, + * the product of the numbers 1,...,n, as as * double. *

* Preconditions:

    - *
  • n > 0 (otherwise + *
  • n > 0 (otherwise * IllegalArgumentException is thrown)
  • *
- * + * * @param n argument * @return n! */ @@ -255,7 +355,7 @@ double logSum = 0; for (int i = 2; i <= n; i++) { logSum += Math.log((double) i); - } + } return logSum; - } + } } 1.2 +69 -34 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/MathUtilsTest.java Index: MathUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/MathUtilsTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MathUtilsTest.java 4 Jun 2003 02:31:14 -0000 1.1 +++ MathUtilsTest.java 6 Jun 2003 03:07:39 -0000 1.2 @@ -14,7 +14,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the - * distribution. + * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: @@ -69,9 +69,9 @@ public MathUtilsTest(String name) { super(name); - } - - public void setUp() { + } + + public void setUp() { } public static Test suite() { @@ -157,10 +157,10 @@ ; } double x = MathUtils.binomialCoefficientDouble(1030,515); - assertTrue("expecting infinite binomial coefficient", + assertTrue("expecting infinite binomial coefficient", Double.isInfinite(x)); } - + public void testFactorial() { for (int i = 1; i < 10; i++) { assertEquals(i + "! ",factorial(i),MathUtils.factorial(i)); @@ -170,7 +170,7 @@ MathUtils.factorialLog(i),10E-12); } } - + public void testFactorialFail() { try { long x = MathUtils.factorial(0); @@ -196,26 +196,26 @@ } catch (ArithmeticException ex) { ; } - assertTrue("expecting infinite factorial value", + assertTrue("expecting infinite factorial value", Double.isInfinite(MathUtils.factorialDouble(171))); - + } - - - /** + + + /** * Exact recursive implementation to test against */ - private long binomialCoefficient(int n, int k) { + private long binomialCoefficient(int n, int k) { if ((n == k) || (k == 0)) { return 1; } if ((k == 1) || (k == n - 1)) { return n; } - return binomialCoefficient(n - 1, k - 1) + + return binomialCoefficient(n - 1, k - 1) + binomialCoefficient(n - 1, k); - } - + } + /** * Finds the largest values of n for which binomialCoefficient and * binomialCoefficientDouble will return values that fit in a long, double, @@ -225,7 +225,7 @@ findBinomialLimits(); } */ - + private void findBinomialLimits() { /** * will kick out 66 as the limit for long @@ -241,8 +241,8 @@ ("largest n for binomialCoefficient = " + (test - 1) ); } test++; - } - + } + /** * will kick out 1029 as the limit for double */ @@ -256,19 +256,19 @@ ("largest n for binomialCoefficientD = " + (test - 1) ); } test++; - } + } } - + /** * Finds the largest values of n for which factiorial and * factorialDouble will return values that fit in a long, double, * resp. Remove comments around test below to get this in test-report - + public void testFactiorialLimits() { findFactorialLimits(); } */ - + private void findFactorialLimits() { /** * will kick out 20 as the limit for long @@ -284,8 +284,8 @@ ("largest n for factorial = " + (test - 1) ); } test++; - } - + } + /** * will kick out 170 as the limit for double */ @@ -299,21 +299,56 @@ ("largest n for factorialDouble = " + (test - 1) ); } test++; - } + } } - - - /** + + + /** * Exact direct multiplication implementation to test against */ - private long factorial(int n) { + private long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; - } - - + } + + + public void testSignDouble() { + double delta = 0.0 ; + assertEquals( 1.0, MathUtils.sign( 2.0 ), delta ) ; + assertEquals( -1.0, MathUtils.sign( -2.0 ), delta ) ; + } + + public void testSignFloat() { + float delta = 0.0F ; + assertEquals( 1.0F, MathUtils.sign( 2.0F ), delta ) ; + assertEquals( -1.0F, MathUtils.sign( -2.0F ), delta ) ; + } + + + public void testSignByte() { + assertEquals( (byte)1, MathUtils.sign( (byte)2 ) ) ; + assertEquals( (byte)(-1), MathUtils.sign( (byte)(-2) ) ) ; + } + + + public void testSignShort() { + assertEquals( (short)1, MathUtils.sign( (short)2 ) ) ; + assertEquals( (short)(-1), MathUtils.sign( (short)(-2) ) ) ; + } + + + public void testSignInt() { + assertEquals( (int)1, MathUtils.sign( (int)(2) ) ) ; + assertEquals( (int)(-1), MathUtils.sign( (int)(-2) ) ) ; + } + + + public void testSignLong() { + assertEquals( 1L, MathUtils.sign( 2L ) ) ; + assertEquals( -1L, MathUtils.sign( -2L ) ) ; + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org