You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ce...@apache.org on 2012/02/27 07:33:00 UTC

svn commit: r1294043 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/util/BigReal.java test/java/org/apache/commons/math3/util/BigRealTest.java

Author: celestin
Date: Mon Feb 27 06:32:59 2012
New Revision: 1294043

URL: http://svn.apache.org/viewvc?rev=1294043&view=rev
Log:
BigReal.divide(BigReal) now throws MathArithmeticException (see MATH-755).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/BigReal.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/BigRealTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/BigReal.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/BigReal.java?rev=1294043&r1=1294042&r2=1294043&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/BigReal.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/BigReal.java Mon Feb 27 06:32:59 2012
@@ -25,6 +25,8 @@ import java.math.RoundingMode;
 
 import org.apache.commons.math3.Field;
 import org.apache.commons.math3.FieldElement;
+import org.apache.commons.math3.exception.MathArithmeticException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
 
 /**
  * Arbitrary precision decimal number.
@@ -241,14 +243,32 @@ public class BigReal implements FieldEle
         return new BigReal(d.negate());
     }
 
-    /** {@inheritDoc} */
-    public BigReal divide(BigReal a) throws ArithmeticException {
-        return new BigReal(d.divide(a.d, scale, roundingMode));
+    /**
+     * {@inheritDoc}
+     *
+     * @throws MathArithmeticException if {@code a} is zero
+     */
+    public BigReal divide(BigReal a) {
+        try {
+            return new BigReal(d.divide(a.d, scale, roundingMode));
+        } catch (ArithmeticException e) {
+            // Division by zero has occured
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
+        }
     }
 
-    /** {@inheritDoc}} */
-    public BigReal reciprocal(){
-        return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
+    /**
+     * {@inheritDoc}
+     *
+     * @throws MathArithmeticException if {@code this} is zero
+     */
+    public BigReal reciprocal() {
+        try {
+            return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
+        } catch (ArithmeticException e) {
+            // Division by zero has occured
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
+        }
     }
 
     /** {@inheritDoc} */

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/BigRealTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/BigRealTest.java?rev=1294043&r1=1294042&r2=1294043&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/BigRealTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/BigRealTest.java Mon Feb 27 06:32:59 2012
@@ -21,6 +21,7 @@ import java.math.BigInteger;
 import java.math.MathContext;
 
 import org.apache.commons.math3.TestUtils;
+import org.apache.commons.math3.exception.MathArithmeticException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -113,6 +114,13 @@ public class BigRealTest {
         Assert.assertEquals(1024.0, a.divide(b).doubleValue(), 1.0e-15);
     }
 
+    @Test(expected = MathArithmeticException.class)
+    public void testDivisionByZero() {
+        final BigReal a = BigReal.ONE;
+        final BigReal b = BigReal.ZERO;
+        a.divide(b);
+    }
+
     @Test
     public void testReciprocal() {
         BigReal a = new BigReal("1.2345678");
@@ -125,6 +133,11 @@ public class BigRealTest {
         Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps);
     }
 
+    @Test(expected = MathArithmeticException.class)
+    public void testReciprocalOfZero() {
+        BigReal.ZERO.reciprocal();
+    }
+
     @Test
     public void testMultiply() {
         BigReal a = new BigReal("1024.0");