You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/06/30 21:55:54 UTC
[commons-numbers] 07/09: NUMBERS-123: Move code from constructor to
conversion method.
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 29f0745308fae2eb557565b6e1ee20b79b795a73
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Jun 30 23:11:02 2019 +0200
NUMBERS-123: Move code from constructor to conversion method.
---
.../commons/numbers/fraction/BigFraction.java | 114 ++++++++-------------
1 file changed, 43 insertions(+), 71 deletions(-)
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 4a79128..6ef2549 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -28,10 +28,10 @@ import org.apache.commons.numbers.core.ArithmeticUtils;
*/
public class BigFraction extends Number implements Comparable<BigFraction>, Serializable {
/** A fraction representing "0". */
- public static final BigFraction ZERO = new BigFraction(0);
+ public static final BigFraction ZERO = of(0);
/** A fraction representing "1". */
- public static final BigFraction ONE = new BigFraction(1);
+ public static final BigFraction ONE = of(1);
/** Serializable version identifier. */
private static final long serialVersionUID = -5630213147331578515L;
@@ -82,7 +82,6 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
// store the values in the final fields
numerator = num;
denominator = den;
-
}
}
@@ -191,10 +190,36 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
}
/**
+ * <p>
+ * Create a {@link BigFraction} equivalent to the passed {@code BigInteger}, ie
+ * "num / 1".
+ * </p>
+ *
+ * @param num the numerator.
+ * @return a new instance.
+ */
+ public static BigFraction of(final BigInteger num) {
+ return new BigFraction(num, BigInteger.ONE);
+ }
+
+ /**
+ * Create a {@link BigFraction} given the numerator and denominator as
+ * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
+ *
+ * @param num the numerator, must not be {@code null}.
+ * @param den the denominator, must not be {@code null}.
+ * @throws ArithmeticException if the denominator is zero.
+ * @return a new instance.
+ */
+ public static BigFraction of(BigInteger num, BigInteger den) {
+ return new BigFraction(num, den);
+ }
+
+ /**
* Create a fraction given the double value.
* <p>
- * This constructor behaves <em>differently</em> from
- * {@link #BigFraction(double, double, int)}. It converts the double value
+ * This factory method behaves <em>differently</em> from
+ * {@link #from(double, double, int)}. It converts the double value
* exactly, considering its internal bits representation. This works for all
* values except NaN and infinities and does not requires any loop or
* convergence threshold.
@@ -207,16 +232,18 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
* because the double number passed to the constructor is not exactly 1/3
* (this number cannot be stored exactly in IEEE754).
* </p>
- * @see #BigFraction(double, double, int)
- * @param value the double value to convert to a fraction.
- * @exception IllegalArgumentException if value is NaN or infinite
+ *
+ * @see BigFraction#from(double, double, int)
+ * @param value Value to convert to a fraction.
+ * @throws IllegalArgumentException if the given {@code value} is NaN or infinite.
+ * @return a new instance.
*/
- private BigFraction(final double value) throws IllegalArgumentException {
+ public static BigFraction from(final double value) {
if (Double.isNaN(value)) {
- throw new IllegalArgumentException("cannot convert NaN value");
+ throw new IllegalArgumentException("Cannot convert NaN value");
}
if (Double.isInfinite(value)) {
- throw new IllegalArgumentException("cannot convert infinite value");
+ throw new IllegalArgumentException("Cannot convert infinite value");
}
final long bits = Double.doubleToLongBits(value);
@@ -249,65 +276,11 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
++k;
}
- if (k < 0) {
- numerator = BigInteger.valueOf(m);
- denominator = BigInteger.ZERO.flipBit(-k);
- } else {
- numerator = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
- denominator = BigInteger.ONE;
- }
- }
-
- /**
- * <p>
- * Create a {@link BigFraction} equivalent to the passed {@code BigInteger}, ie
- * "num / 1".
- * </p>
- *
- * @param num the numerator.
- * @return a new instance.
- */
- public static BigFraction of(final BigInteger num) {
- return new BigFraction(num, BigInteger.ONE);
- }
-
- /**
- * Create a {@link BigFraction} given the numerator and denominator as
- * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
- *
- * @param num the numerator, must not be {@code null}.
- * @param den the denominator, must not be {@code null}.
- * @throws ArithmeticException if the denominator is zero.
- * @return a new instance.
- */
- public static BigFraction of(BigInteger num, BigInteger den) {
- return new BigFraction(num, den);
- }
-
- /**
- * Create a fraction given the double value.
- * <p>
- * This factory method behaves <em>differently</em> from
- * {@link #from(double, double, int)}. It converts the double value
- * exactly, considering its internal bits representation. This works for all
- * values except NaN and infinities and does not requires any loop or
- * convergence threshold.
- * </p>
- * <p>
- * Since this conversion is exact and since double numbers are sometimes
- * approximated, the fraction created may seem strange in some cases. For example,
- * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create
- * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984
- * because the double number passed to the constructor is not exactly 1/3
- * (this number cannot be stored exactly in IEEE754).
- * </p>
- * @see BigFraction#from(double, double, int)
- * @param value the double value to convert to a fraction.
- * @exception IllegalArgumentException if value is NaN or infinite
- * @return a new instance.
- */
- public static BigFraction from(final double value) throws IllegalArgumentException {
- return new BigFraction(value);
+ return k < 0 ?
+ new BigFraction(BigInteger.valueOf(m),
+ BigInteger.ZERO.flipBit(-k)) :
+ new BigFraction(BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k)),
+ BigInteger.ONE);
}
/**
@@ -1220,5 +1193,4 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
throw new NullPointerException(argName);
}
}
-
}