Added and used a specialized exception for continued fraction convergence errors
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@506591 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ee71801e77
commit
21a95478c2
|
@ -17,7 +17,6 @@
|
|||
package org.apache.commons.math.fraction;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import org.apache.commons.math.ConvergenceException;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
|
||||
/**
|
||||
|
@ -35,7 +34,8 @@ public class Fraction extends Number implements Comparable {
|
|||
public static final Fraction ZERO = new Fraction(0, 1);
|
||||
|
||||
/** Serializable version identifier */
|
||||
private static final long serialVersionUID = 65382027393090L;
|
||||
private static final long serialVersionUID = 6222990762865980424L;
|
||||
|
||||
|
||||
/** The denominator. */
|
||||
private int denominator;
|
||||
|
@ -46,10 +46,10 @@ public class Fraction extends Number implements Comparable {
|
|||
/**
|
||||
* Create a fraction given the double value.
|
||||
* @param value the double value to convert to a fraction.
|
||||
* @throws ConvergenceException if the continued fraction failed to
|
||||
* @throws FractionConversionException if the continued fraction failed to
|
||||
* converge.
|
||||
*/
|
||||
public Fraction(double value) throws ConvergenceException {
|
||||
public Fraction(double value) throws FractionConversionException {
|
||||
this(value, 1.0e-5, 100);
|
||||
}
|
||||
|
||||
|
@ -66,11 +66,11 @@ public class Fraction extends Number implements Comparable {
|
|||
* @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 ConvergenceException if the continued fraction failed to
|
||||
* @throws FractionConversionException if the continued fraction failed to
|
||||
* converge.
|
||||
*/
|
||||
public Fraction(double value, double epsilon, int maxIterations)
|
||||
throws ConvergenceException
|
||||
throws FractionConversionException
|
||||
{
|
||||
double r0 = value;
|
||||
int a0 = (int)Math.floor(r0);
|
||||
|
@ -114,8 +114,7 @@ public class Fraction extends Number implements Comparable {
|
|||
} while (!stop);
|
||||
|
||||
if (n >= maxIterations) {
|
||||
throw new ConvergenceException(
|
||||
"Unable to convert double to fraction");
|
||||
throw new FractionConversionException(value, maxIterations);
|
||||
}
|
||||
|
||||
this.numerator = p2;
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package org.apache.commons.math.fraction;
|
||||
|
||||
import org.apache.commons.math.MaxIterationsExceededException;
|
||||
|
||||
public class FractionConversionException extends MaxIterationsExceededException {
|
||||
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = 4588659344016668813L;
|
||||
|
||||
/**
|
||||
* Constructs an exception with specified formatted detail message.
|
||||
* Message formatting is delegated to {@link java.text.MessageFormat}.
|
||||
* @param value double value to convert
|
||||
* @param maxIterations maximal number of iterations allowed
|
||||
*/
|
||||
public FractionConversionException(double value, int maxIterations) {
|
||||
super(maxIterations,
|
||||
"Unable to convert {0} to fraction after {1} iterations",
|
||||
new Object[] { new Double(value), new Integer(maxIterations) });
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue