commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/util MathUtilsTest.java
Date Thu, 14 Oct 2004 04:01:05 GMT
psteitz     2004/10/13 21:01:05

  Modified:    math/src/java/org/apache/commons/math/util MathUtils.java
               math/src/test/org/apache/commons/math/util
                        MathUtilsTest.java
  Log:
  Changed factorial methods to return 1 for argument = 0.
  Pr #31687
  Reported by: Fredrik Norin
  
  Revision  Changes    Path
  1.20      +9 -7      jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java
  
  Index: MathUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- MathUtils.java	23 Jun 2004 16:26:16 -0000	1.19
  +++ MathUtils.java	14 Oct 2004 04:01:04 -0000	1.20
  @@ -348,7 +348,7 @@
        *
        * <p>
        * <Strong>Preconditions</strong>:<ul>
  -     * <li> <code>n > 0</code> (otherwise
  +     * <li> <code>n >= 0</code> (otherwise
        *      <code>IllegalArgumentException</code> is thrown)</li>
        * <li> The result is small enough to fit into a <code>long</code>.
 The
        *      largest value of <code>n</code> for which <code>n!</code>
  @@ -362,6 +362,7 @@
        * @return <code>n!</code>
        * @throws ArithmeticException if the result is too large to be represented
        *         by a long integer.
  +     * @throws IllegalArgumentException if n < 0
        */
       public static long factorial(final int n) {
           long result = Math.round(factorialDouble(n));
  @@ -380,7 +381,7 @@
        *
        * <p>
        * <Strong>Preconditions</strong>:<ul>
  -     * <li> <code>n > 0</code> (otherwise
  +     * <li> <code>n >= 0</code> (otherwise
        *      <code>IllegalArgumentException</code> is thrown)</li>
        * <li> The result is small enough to fit into a <code>double</code>.
 The
        *      largest value of <code>n</code> for which <code>n!</code>
  @@ -391,10 +392,11 @@
        *
        * @param n argument
        * @return <code>n!</code>
  +     * @throws IllegalArgumentException if n < 0
        */
       public static double factorialDouble(final int n) {
  -        if (n <= 0) {
  -            throw new IllegalArgumentException("must have n > 0 for n!");
  +        if (n < 0) {
  +            throw new IllegalArgumentException("must have n >= 0 for n!");
           }
           return Math.floor(Math.exp(factorialLog(n)) + 0.5);
       }
  @@ -403,7 +405,7 @@
        * Returns the natural logarithm of n!.
        * <p>
        * <Strong>Preconditions</strong>:<ul>
  -     * <li> <code>n > 0</code> (otherwise
  +     * <li> <code>n >= 0</code> (otherwise
        *      <code>IllegalArgumentException</code> is thrown)</li>
        * </ul>
        *
  @@ -412,7 +414,7 @@
        * @throws IllegalArgumentException if preconditions are not met.
        */
       public static double factorialLog(final int n) {
  -        if (n <= 0) {
  +        if (n < 0) {
               throw new IllegalArgumentException("must have n > 0 for n!");
           }
           double logSum = 0;
  
  
  
  1.15      +7 -5      jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java
  
  Index: MathUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- MathUtilsTest.java	17 Jun 2004 21:31:00 -0000	1.14
  +++ MathUtilsTest.java	14 Oct 2004 04:01:04 -0000	1.15
  @@ -120,23 +120,26 @@
               assertEquals(i + "! ",Math.log((double)factorial(i)),
                   MathUtils.factorialLog(i),10E-12);
           }
  +        assertEquals("0", 1, MathUtils.factorial(0));
  +        assertEquals("0", 1.0d, MathUtils.factorialDouble(0), 1E-14);
  +        assertEquals("0", 0.0d, MathUtils.factorialLog(0), 1E-14);
       }
   
       public void testFactorialFail() {
           try {
  -            long x = MathUtils.factorial(0);
  +            long x = MathUtils.factorial(-1);
               fail ("expecting IllegalArgumentException");
           } catch (IllegalArgumentException ex) {
               ;
           }
           try {
  -            double x = MathUtils.factorialDouble(0);
  +            double x = MathUtils.factorialDouble(-1);
               fail ("expecting IllegalArgumentException");
           } catch (IllegalArgumentException ex) {
               ;
           }
           try {
  -            double x = MathUtils.factorialLog(0);
  +            double x = MathUtils.factorialLog(-1);
               fail ("expecting IllegalArgumentException");
           } catch (IllegalArgumentException ex) {
               ;
  @@ -149,7 +152,6 @@
           }
           assertTrue("expecting infinite factorial value",
               Double.isInfinite(MathUtils.factorialDouble(171)));
  -
       }
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message