Class Fraction
java.lang.Object
java.lang.Number
org.apache.commons.math3.fraction.Fraction
- All Implemented Interfaces:
Serializable,Comparable<Fraction>,FieldElement<Fraction>
public class Fraction
extends Number
implements FieldElement<Fraction>, Comparable<Fraction>, Serializable
Representation of a rational number.
implements Serializable since 2.0
- Since:
- 1.1
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FractionA fraction representing "4/5".static final FractionA fraction representing "-1 / 1".static final FractionA fraction representing "1".static final FractionA fraction representing "1/5".static final FractionA fraction representing "1/2".static final FractionA fraction representing "1/4".static final FractionA fraction representing "1/3".static final FractionA fraction representing "3/5".static final FractionA fraction representing "3/4".static final FractionA fraction representing "2 / 1".static final FractionA fraction representing "2/5".static final FractionA fraction representing "2/4".static final FractionA fraction representing "2/3".static final FractionA fraction representing "0". -
Constructor Summary
ConstructorsConstructorDescriptionFraction(double value) Create a fraction given the double value.Fraction(double value, double epsilon, int maxIterations) Create a fraction given the double value and maximum error allowed.Fraction(double value, int maxDenominator) Create a fraction given the double value and maximum denominator.Fraction(int num) Create a fraction from an int.Fraction(int num, int den) Create a fraction given the numerator and denominator. -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns the absolute value of this fraction.add(int i) Add an integer to the fraction.Adds the value of this fraction to another, returning the result in reduced form.intCompares this object to another based on size.divide(int i) Divide the fraction by an integer.Divide the value of this fraction by another.doubleGets the fraction as adouble.booleanTest for the equality of two fractions.floatGets the fraction as afloat.intAccess the denominator.getField()Get theFieldto which the instance belongs.intAccess the numerator.static FractiongetReducedFraction(int numerator, int denominator) Creates aFractioninstance with the 2 parts of a fraction Y/Z.inthashCode()Gets a hashCode for the fraction.intintValue()Gets the fraction as anint.longGets the fraction as along.multiply(int i) Multiply the fraction by an integer.Multiplies the value of this fraction by another, returning the result in reduced form.negate()Return the additive inverse of this fraction.doubleGets the fraction percentage as adouble.Return the multiplicative inverse of this fraction.subtract(int i) Subtract an integer from the fraction.Subtracts the value of another fraction from the value of this one, returning the result in reduced form.toString()Returns theStringrepresenting this fraction, ie "num / dem" or just "num" if the denominator is one.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
TWO
A fraction representing "2 / 1". -
ONE
A fraction representing "1". -
ZERO
A fraction representing "0". -
FOUR_FIFTHS
A fraction representing "4/5". -
ONE_FIFTH
A fraction representing "1/5". -
ONE_HALF
A fraction representing "1/2". -
ONE_QUARTER
A fraction representing "1/4". -
ONE_THIRD
A fraction representing "1/3". -
THREE_FIFTHS
A fraction representing "3/5". -
THREE_QUARTERS
A fraction representing "3/4". -
TWO_FIFTHS
A fraction representing "2/5". -
TWO_QUARTERS
A fraction representing "2/4". -
TWO_THIRDS
A fraction representing "2/3". -
MINUS_ONE
A fraction representing "-1 / 1".
-
-
Constructor Details
-
Fraction
Create a fraction given the double value.- Parameters:
value- the double value to convert to a fraction.- Throws:
FractionConversionException- if the continued fraction failed to converge.
-
Fraction
Create a fraction given the double value and maximum error allowed.References:
- Continued Fraction equations (11) and (22)-(26)
- Parameters:
value- the double value to convert to a fraction.epsilon- maximum error allowed. The resulting fraction is withinepsilonofvalue, in absolute terms.maxIterations- maximum number of convergents- Throws:
FractionConversionException- if the continued fraction failed to converge.
-
Fraction
Create a fraction given the double value and maximum denominator.References:
- Continued Fraction equations (11) and (22)-(26)
- Parameters:
value- the double value to convert to a fraction.maxDenominator- The maximum allowed value for denominator- Throws:
FractionConversionException- if the continued fraction failed to converge
-
Fraction
public Fraction(int num) Create a fraction from an int. The fraction is num / 1.- Parameters:
num- the numerator.
-
Fraction
public Fraction(int num, int den) Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.- Parameters:
num- the numerator.den- the denominator.- Throws:
MathArithmeticException- if the denominator iszero
-
-
Method Details
-
abs
Returns the absolute value of this fraction.- Returns:
- the absolute value.
-
compareTo
Compares this object to another based on size.- Specified by:
compareToin interfaceComparable<Fraction>- Parameters:
object- the object to compare to- Returns:
- -1 if this is less than
object, +1 if this is greater thanobject, 0 if they are equal.
-
doubleValue
public double doubleValue()Gets the fraction as adouble. This calculates the fraction as the numerator divided by denominator.- Specified by:
doubleValuein classNumber- Returns:
- the fraction as a
double
-
equals
Test for the equality of two fractions. If the lowest term numerator and denominators are the same for both fractions, the two fractions are considered to be equal. -
floatValue
public float floatValue()Gets the fraction as afloat. This calculates the fraction as the numerator divided by denominator.- Specified by:
floatValuein classNumber- Returns:
- the fraction as a
float
-
getDenominator
public int getDenominator()Access the denominator.- Returns:
- the denominator.
-
getNumerator
public int getNumerator()Access the numerator.- Returns:
- the numerator.
-
hashCode
public int hashCode()Gets a hashCode for the fraction. -
intValue
public int intValue()Gets the fraction as anint. This returns the whole number part of the fraction. -
longValue
public long longValue()Gets the fraction as along. This returns the whole number part of the fraction. -
negate
Return the additive inverse of this fraction.- Specified by:
negatein interfaceFieldElement<Fraction>- Returns:
- the negation of this fraction.
-
reciprocal
Return the multiplicative inverse of this fraction.- Specified by:
reciprocalin interfaceFieldElement<Fraction>- Returns:
- the reciprocal fraction
-
add
Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
- Specified by:
addin interfaceFieldElement<Fraction>- Parameters:
fraction- the fraction to add, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
NullArgumentException- if the fraction isnullMathArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
add
Add an integer to the fraction.- Parameters:
i- theintegerto add.- Returns:
- this + i
-
subtract
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
- Specified by:
subtractin interfaceFieldElement<Fraction>- Parameters:
fraction- the fraction to subtract, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
NullArgumentException- if the fraction isnullMathArithmeticException- if the resulting numerator or denominator cannot be represented in anint.
-
subtract
Subtract an integer from the fraction.- Parameters:
i- theintegerto subtract.- Returns:
- this - i
-
multiply
Multiplies the value of this fraction by another, returning the result in reduced form.
- Specified by:
multiplyin interfaceFieldElement<Fraction>- Parameters:
fraction- the fraction to multiply by, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
NullArgumentException- if the fraction isnullMathArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
multiply
Multiply the fraction by an integer.- Specified by:
multiplyin interfaceFieldElement<Fraction>- Parameters:
i- theintegerto multiply by.- Returns:
- this * i
-
divide
Divide the value of this fraction by another.
- Specified by:
dividein interfaceFieldElement<Fraction>- Parameters:
fraction- the fraction to divide by, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
IllegalArgumentException- if the fraction isnullMathArithmeticException- if the fraction to divide by is zeroMathArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
divide
Divide the fraction by an integer.- Parameters:
i- theintegerto divide by.- Returns:
- this * i
-
percentageValue
public double percentageValue()Gets the fraction percentage as a
double. This calculates the fraction as the numerator divided by denominator multiplied by 100.- Returns:
- the fraction percentage as a
double.
-
getReducedFraction
Creates a
Fractioninstance with the 2 parts of a fraction Y/Z.Any negative signs are resolved to be on the numerator.
- Parameters:
numerator- the numerator, for example the three in 'three sevenths'denominator- the denominator, for example the seven in 'three sevenths'- Returns:
- a new fraction instance, with the numerator and denominator reduced
- Throws:
MathArithmeticException- if the denominator iszero
-
toString
Returns the
Stringrepresenting this fraction, ie "num / dem" or just "num" if the denominator is one. -
getField
Get theFieldto which the instance belongs.- Specified by:
getFieldin interfaceFieldElement<Fraction>- Returns:
Fieldto which the instance belongs
-