|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Number
org.apache.commons.math.fraction.Fraction
public class Fraction
Representation of a rational number.
Field Summary | |
---|---|
private int |
denominator
The denominator. |
private int |
numerator
The numerator. |
static Fraction |
ONE
A fraction representing "1 / 1". |
private static long |
serialVersionUID
Serializable version identifier |
static Fraction |
ZERO
A fraction representing "0 / 1". |
Constructor Summary | |
---|---|
Fraction(double value)
Create a fraction given the double value. |
|
Fraction(double value,
double epsilon,
int maxIterations)
Create a fraction given the double value. |
|
Fraction(int num,
int den)
Create a fraction given the numerator and denominator. |
Method Summary | |
---|---|
Fraction |
abs()
Returns the absolute value of this fraction. |
Fraction |
add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form. |
private Fraction |
addSub(Fraction fraction,
boolean isAdd)
Implement add and subtract using algorithm described in Knuth 4.5.1. |
int |
compareTo(java.lang.Object object)
Compares this object to another based on size. |
Fraction |
divide(Fraction fraction)
Divide the value of this fraction by another. |
double |
doubleValue()
Gets the fraction as a double. |
boolean |
equals(java.lang.Object other)
Test for the equality of two fractions. |
float |
floatValue()
Gets the fraction as a float. |
int |
getDenominator()
Access the denominator. |
int |
getNumerator()
Access the numerator. |
static Fraction |
getReducedFraction(int numerator,
int denominator)
Creates a Fraction instance with the 2 parts
of a fraction Y/Z. |
int |
hashCode()
Gets a hashCode for the fraction. |
int |
intValue()
Gets the fraction as an int. |
long |
longValue()
Gets the fraction as a long. |
Fraction |
multiply(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form. |
Fraction |
negate()
Return the additive inverse of this fraction. |
Fraction |
reciprocal()
Return the multiplicative inverse of this fraction. |
private void |
reduce()
Reduce this fraction to lowest terms. |
Fraction |
subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form. |
Methods inherited from class java.lang.Number |
---|
byteValue, shortValue |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Fraction ONE
public static final Fraction ZERO
private static final long serialVersionUID
private int denominator
private int numerator
Constructor Detail |
---|
public Fraction(double value) throws ConvergenceException
value
- the double value to convert to a fraction.
ConvergenceException
- if the continued fraction failed to
converge.public Fraction(double value, double epsilon, int maxIterations) throws ConvergenceException
References:
value
- the double value to convert to a fraction.epsilon
- maximum error allowed. The resulting fraction is within
epsilon
of value
, in absolute terms.maxIterations
- maximum number of convergents
ConvergenceException
- if the continued fraction failed to
converge.public Fraction(int num, int den)
num
- the numerator.den
- the denominator.
java.lang.ArithmeticException
- if the denomiator is zero
Method Detail |
---|
public Fraction abs()
public int compareTo(java.lang.Object object)
compareTo
in interface java.lang.Comparable
object
- the object to compare to
public double doubleValue()
doubleValue
in class java.lang.Number
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
other
- fraction to test for equality to this fraction
Fraction
, or not equal
to this fraction instance.public float floatValue()
floatValue
in class java.lang.Number
public int getDenominator()
public int getNumerator()
public int hashCode()
hashCode
in class java.lang.Object
public int intValue()
intValue
in class java.lang.Number
public long longValue()
longValue
in class java.lang.Number
public Fraction negate()
public Fraction reciprocal()
public Fraction add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
fraction
- the fraction to add, must not be null
Fraction
instance with the resulting values
java.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
fraction
- the fraction to subtract, must not be null
Fraction
instance with the resulting values
java.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.private Fraction addSub(Fraction fraction, boolean isAdd)
fraction
- the fraction to subtract, must not be null
isAdd
- true to add, false to subtract
Fraction
instance with the resulting values
java.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction multiply(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
fraction
- the fraction to multiply by, must not be null
Fraction
instance with the resulting values
java.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction divide(Fraction fraction)
Divide the value of this fraction by another.
fraction
- the fraction to divide by, must not be null
Fraction
instance with the resulting values
java.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the fraction to divide by is zero
java.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public static Fraction getReducedFraction(int numerator, int denominator)
Creates a Fraction
instance with the 2 parts
of a fraction Y/Z.
Any negative signs are resolved to be on the numerator.
numerator
- the numerator, for example the three in 'three sevenths'denominator
- the denominator, for example the seven in 'three sevenths'
java.lang.ArithmeticException
- if the denominator is zero
private void reduce()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |