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/06 18:06:19 UTC
[commons-numbers] branch fraction-dev updated: NUMBERS-77: final
checkstyle edits
This is an automated email from the ASF dual-hosted git repository.
ericbarnhill pushed a commit to branch fraction-dev
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/fraction-dev by this push:
new 0a83233 NUMBERS-77: final checkstyle edits
0a83233 is described below
commit 0a8323369bd6f0da6f8f6f594a073286f9daf59a
Author: Eric Barnhill <eb...@irhythmtech.com>
AuthorDate: Thu Jun 6 10:57:16 2019 -0700
NUMBERS-77: final checkstyle edits
---
.../commons/numbers/fraction/BigFraction.java | 301 ++++++++++-----------
.../apache/commons/numbers/fraction/Fraction.java | 156 +++++------
2 files changed, 229 insertions(+), 228 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 283475c..edaf3a4 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
@@ -20,7 +20,6 @@ import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
-import java.text.NumberFormat;
import org.apache.commons.numbers.core.ArithmeticUtils;
@@ -28,19 +27,16 @@ import org.apache.commons.numbers.core.ArithmeticUtils;
* Representation of a rational number without any overflow. This class is
* immutable.
*/
-public class BigFraction
- extends Number
- implements Comparable<BigFraction>, Serializable {
-
- /** Serializable version identifier. */
- private static final long serialVersionUID = -5630213147331578515L;
-
+public class BigFraction extends Number implements Comparable<BigFraction>, Serializable {
/** A fraction representing "0". */
public static final BigFraction ZERO = new BigFraction(0);
/** A fraction representing "1". */
public static final BigFraction ONE = new BigFraction(1);
+ /** Serializable version identifier. */
+ private static final long serialVersionUID = -5630213147331578515L;
+
/** Parameter name for fraction (to satisfy checkstyle). */
private static final String PARAM_NAME_FRACTION = "fraction";
@@ -52,31 +48,6 @@ public class BigFraction
/** The denominator. */
private final BigInteger denominator;
-
- /**
- * <p>
- * Create a {@link BigFraction} equivalent to the passed {@code BigInteger}, ie
- * "num / 1".
- * </p>
- *
- * @param num
- * the numerator.
- */
- 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.
- */
- public static BigFraction of(BigInteger num, BigInteger den) {
- return new BigFraction(num, den);
- }
/**
* Private constructor for BigFraction ofInt() factory methods.
@@ -117,113 +88,6 @@ public class BigFraction
}
/**
- * 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(double, double, int)
- * @param value the double value to convert to a fraction.
- * @exception IllegalArgumentException if value is NaN or infinite
- */
- public static BigFraction from(final double value) throws IllegalArgumentException {
- return new BigFraction(value);
- }
-
- /**
- * 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
- * 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(double, double, int)
- * @param value the double value to convert to a fraction.
- * @exception IllegalArgumentException if value is NaN or infinite
- */
- private BigFraction(final double value) throws IllegalArgumentException {
- if (Double.isNaN(value)) {
- throw new IllegalArgumentException("cannot convert NaN value");
- }
- if (Double.isInfinite(value)) {
- throw new IllegalArgumentException("cannot convert infinite value");
- }
-
- // compute m and k such that value = m * 2^k
- final long bits = Double.doubleToLongBits(value);
- final long sign = bits & 0x8000000000000000L;
- final long exponent = bits & 0x7ff0000000000000L;
- long m = bits & 0x000fffffffffffffL;
- if (exponent != 0) {
- // this was a normalized number, add the implicit most significant bit
- m |= 0x0010000000000000L;
- }
- if (sign != 0) {
- m = -m;
- }
- int k = ((int) (exponent >> 52)) - 1075;
- while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
- m >>= 1;
- ++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;
- }
-
- }
-
- /**
- * Create a fraction given the double value and maximum error allowed.
- * <p>
- * References:
- * <ul>
- * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
- * Continued Fraction</a> equations (11) and (22)-(26)</li>
- * </ul>
- *
- * @param value
- * the double value to convert to a fraction.
- * @param epsilon
- * maximum error allowed. The resulting fraction is within
- * <code>epsilon</code> of <code>value</code>, in absolute terms.
- * @param maxIterations
- * maximum number of convergents.
- * @throws ArithmeticException
- * if the continued fraction failed to converge.
- * @see #BigFraction(double)
- */
- public static BigFraction from(final double value, final double epsilon,
- final int maxIterations) {
- return new BigFraction(value, epsilon, Integer.MAX_VALUE, maxIterations);
- }
-
- /**
* Create a fraction given the double value and either the maximum error
* allowed or the maximum number of denominator digits.
* <p>
@@ -318,7 +182,6 @@ public class BigFraction
if (n >= maxIterations) {
throw new FractionException(FractionException.ERROR_CONVERSION, value, maxIterations);
}
-
if (q2 < maxDenominator) {
numerator = BigInteger.valueOf(p2);
denominator = BigInteger.valueOf(q2);
@@ -327,6 +190,141 @@ public class BigFraction
denominator = BigInteger.valueOf(q1);
}
}
+
+ /**
+ * 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
+ * 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(double, double, int)
+ * @param value the double value to convert to a fraction.
+ * @exception IllegalArgumentException if value is NaN or infinite
+ */
+ private BigFraction(final double value) throws IllegalArgumentException {
+ if (Double.isNaN(value)) {
+ throw new IllegalArgumentException("cannot convert NaN value");
+ }
+ if (Double.isInfinite(value)) {
+ throw new IllegalArgumentException("cannot convert infinite value");
+ }
+
+ // compute m and k such that value = m * 2^k
+ final long bits = Double.doubleToLongBits(value);
+ final long sign = bits & 0x8000000000000000L;
+ final long exponent = bits & 0x7ff0000000000000L;
+ long m = bits & 0x000fffffffffffffL;
+ if (exponent != 0) {
+ // this was a normalized number, add the implicit most significant bit
+ m |= 0x0010000000000000L;
+ }
+ if (sign != 0) {
+ m = -m;
+ }
+ int k = ((int) (exponent >> 52)) - 1075;
+ while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
+ m >>= 1;
+ ++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 {@link BigFraction 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 {@link BigFraction 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(double, double, int)
+ * @param value the double value to convert to a fraction.
+ * @exception IllegalArgumentException if value is NaN or infinite
+ * @return {@link BigFraction instance
+ */
+ public static BigFraction from(final double value) throws IllegalArgumentException {
+ return new BigFraction(value);
+ }
+
+ /**
+ * Create a fraction given the double value and maximum error allowed.
+ * <p>
+ * References:
+ * <ul>
+ * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+ * Continued Fraction</a> equations (11) and (22)-(26)</li>
+ * </ul>
+ *
+ * @param value
+ * the double value to convert to a fraction.
+ * @param epsilon
+ * maximum error allowed. The resulting fraction is within
+ * <code>epsilon</code> of <code>value</code>, in absolute terms.
+ * @param maxIterations
+ * maximum number of convergents.
+ * @throws ArithmeticException
+ * if the continued fraction failed to converge.
+ * @see #BigFraction(double)
+ * @return {@link BigFraction instance
+ */
+ public static BigFraction from(final double value, final double epsilon,
+ final int maxIterations) {
+ return new BigFraction(value, epsilon, Integer.MAX_VALUE, maxIterations);
+ }
/**
* Create a fraction given the double value and maximum denominator.
@@ -343,6 +341,7 @@ public class BigFraction
* The maximum allowed value for denominator.
* @throws ArithmeticException
* if the continued fraction failed to converge.
+ * @return {@link BigFraction instance
*/
public static BigFraction from(final double value, final int maxDenominator) {
return new BigFraction(value, 0, maxDenominator, 100);
@@ -356,6 +355,7 @@ public class BigFraction
*
* @param num
* the numerator.
+ * @return {@link BigFraction instance
*/
public static BigFraction of(final int num) {
return new BigFraction(BigInteger.valueOf(num), BigInteger.ONE);
@@ -367,10 +367,9 @@ public class BigFraction
* {@code int}. The {@link BigFraction} is reduced to lowest terms.
* </p>
*
- * @param num
- * the numerator.
- * @param den
- * the denominator.
+ * @param num the numerator.
+ * @param den the denominator.
+ * @return {@link BigFraction instance
*/
public static BigFraction of(final int num, final int den) {
return new BigFraction(BigInteger.valueOf(num), BigInteger.valueOf(den));
@@ -381,8 +380,8 @@ public class BigFraction
* Create a {@link BigFraction} equivalent to the passed long, ie "num / 1".
* </p>
*
- * @param num
- * the numerator.
+ * @param num the numerator.
+ * @return {@link BigFraction instance
*/
public static BigFraction of(final long num) {
return new BigFraction(BigInteger.valueOf(num), BigInteger.ONE);
@@ -394,10 +393,9 @@ public class BigFraction
* {@code long}. The {@link BigFraction} is reduced to lowest terms.
* </p>
*
- * @param num
- * the numerator.
- * @param den
- * the denominator.
+ * @param num the numerator.
+ * @param den the denominator.
+ * @return {@link BigFraction instance
*/
public static BigFraction of(final long num, final long den) {
return new BigFraction(BigInteger.valueOf(num), BigInteger.valueOf(den));
@@ -1176,7 +1174,6 @@ public class BigFraction
return str;
}
-
/**
* Parses a string that would be produced by {@link #toString()}
* and instantiates the corresponding object.
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index c9ac484..c6ed60e 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -28,21 +28,21 @@ public class Fraction
extends Number
implements Comparable<Fraction>, Serializable {
- /** Serializable version identifier */
- private static final long serialVersionUID = 3698073679419233275L;
-
/** A fraction representing "1". */
public static final Fraction ONE = new Fraction(1, 1);
/** A fraction representing "0". */
public static final Fraction ZERO = new Fraction(0, 1);
+ /** Serializable version identifier */
+ private static final long serialVersionUID = 3698073679419233275L;
+
/** Parameter name for fraction (to satisfy checkstyle). */
private static final String PARAM_NAME_FRACTION = "fraction";
/** The default epsilon used for convergence. */
private static final double DEFAULT_EPSILON = 1e-5;
-
+
/** The denominator. */
private final int denominator;
@@ -50,56 +50,6 @@ public class Fraction
private final int numerator;
/**
- * Create a fraction given the double value.
- * @param value the double value to convert to a fraction.
- * @throws IllegalArgumentException if the continued fraction failed to
- * converge.
- */
- public static Fraction from(double value) {
- return from(value, DEFAULT_EPSILON, 100);
- }
-
- /**
- * Create a fraction given the double value and maximum error allowed.
- * <p>
- * References:
- * <ul>
- * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
- * Continued Fraction</a> equations (11) and (22)-(26)</li>
- * </ul>
- *
- * @param value the double value to convert to a fraction.
- * @param epsilon maximum error allowed. The resulting fraction is within
- * {@code epsilon} of {@code value}, in absolute terms.
- * @param maxIterations maximum number of convergents
- * @throws IllegalArgumentException if the continued fraction failed to
- * converge.
- */
- public static Fraction from(double value, double epsilon, int maxIterations)
- {
- return new Fraction(value, epsilon, Integer.MAX_VALUE, maxIterations);
- }
-
- /**
- * Create a fraction given the double value and maximum denominator.
- * <p>
- * References:
- * <ul>
- * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
- * Continued Fraction</a> equations (11) and (22)-(26)</li>
- * </ul>
- *
- * @param value the double value to convert to a fraction.
- * @param maxDenominator The maximum allowed value for denominator
- * @throws IllegalArgumentException if the continued fraction failed to
- * converge
- */
- public static Fraction from(double value, int maxDenominator)
- {
- return new Fraction(value, 0, maxDenominator, 100);
- }
-
- /**
* Create a fraction given the double value and either the maximum error
* allowed or the maximum number of denominator digits.
* <p>
@@ -196,28 +146,6 @@ public class Fraction
this.numerator = (int) p1;
this.denominator = (int) q1;
}
-
- }
-
- /**
- * Create a fraction from an int.
- * The fraction is num / 1.
- * @param num the numerator.
- */
- public static Fraction of(int num) {
- return of(num, 1);
- }
-
- /**
- * Return a fraction given the numerator and denominator. The fraction is
- * reduced to lowest terms.
- * @param num the numerator.
- * @param den the denominator.
- * @throws ArithmeticException if the denominator is {@code zero}
- * or if integer overflow occurs
- */
- public static Fraction of(int num, int den) {
- return new Fraction(num, den);
}
/**
@@ -254,6 +182,82 @@ public class Fraction
this.numerator = num;
this.denominator = den;
}
+ /**
+ * Create a fraction given the double value.
+ * @param value the double value to convert to a fraction.
+ * @throws IllegalArgumentException if the continued fraction failed to
+ * converge.
+ * @return {@link Fraction} instance
+ */
+ public static Fraction from(double value) {
+ return from(value, DEFAULT_EPSILON, 100);
+ }
+
+ /**
+ * Create a fraction given the double value and maximum error allowed.
+ * <p>
+ * References:
+ * <ul>
+ * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+ * Continued Fraction</a> equations (11) and (22)-(26)</li>
+ * </ul>
+ *
+ * @param value the double value to convert to a fraction.
+ * @param epsilon maximum error allowed. The resulting fraction is within
+ * {@code epsilon} of {@code value}, in absolute terms.
+ * @param maxIterations maximum number of convergents
+ * @throws IllegalArgumentException if the continued fraction failed to
+ * converge.
+ * @return {@link Fraction} instance
+ */
+ public static Fraction from(double value, double epsilon, int maxIterations)
+ {
+ return new Fraction(value, epsilon, Integer.MAX_VALUE, maxIterations);
+ }
+
+ /**
+ * Create a fraction given the double value and maximum denominator.
+ * <p>
+ * References:
+ * <ul>
+ * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+ * Continued Fraction</a> equations (11) and (22)-(26)</li>
+ * </ul>
+ *
+ * @param value the double value to convert to a fraction.
+ * @param maxDenominator The maximum allowed value for denominator
+ * @throws IllegalArgumentException if the continued fraction failed to
+ * converge
+ * @return {@link Fraction} instance
+ */
+ public static Fraction from(double value, int maxDenominator)
+ {
+ return new Fraction(value, 0, maxDenominator, 100);
+ }
+
+
+ /**
+ * Create a fraction from an int.
+ * The fraction is num / 1.
+ * @param num the numerator.
+ * @return {@link Fraction} instance
+ */
+ public static Fraction of(int num) {
+ return of(num, 1);
+ }
+
+ /**
+ * Return a fraction given the numerator and denominator. The fraction is
+ * reduced to lowest terms.
+ * @param num the numerator.
+ * @param den the denominator.
+ * @throws ArithmeticException if the denominator is {@code zero}
+ * or if integer overflow occurs
+ * @return {@link Fraction} instance
+ */
+ public static Fraction of(int num, int den) {
+ return new Fraction(num, den);
+ }
/**
* Returns the absolute value of this fraction.