Class BigFraction
- java.lang.Object
 - 
- java.lang.Number
 - 
- org.hipparchus.fraction.BigFraction
 
 
 
- 
- All Implemented Interfaces:
 Serializable,Comparable<BigFraction>,FieldElement<BigFraction>
public class BigFraction extends Number implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable
Representation of a rational number without any overflow. This class is immutable.- See Also:
 - Serialized Form
 
 
- 
- 
Field Summary
Fields Modifier and Type Field Description static BigFractionFOUR_FIFTHSA fraction representing "4/5".static BigFractionMINUS_ONEA fraction representing "-1 / 1".static BigFractionONEA fraction representing "1".static BigFractionONE_FIFTHA fraction representing "1/5".static BigFractionONE_HALFA fraction representing "1/2".static BigFractionONE_QUARTERA fraction representing "1/4".static BigFractionONE_THIRDA fraction representing "1/3".static BigFractionTHREE_FIFTHSA fraction representing "3/5".static BigFractionTHREE_QUARTERSA fraction representing "3/4".static BigFractionTWOA fraction representing "2 / 1".static BigFractionTWO_FIFTHSA fraction representing "2/5".static BigFractionTWO_QUARTERSA fraction representing "2/4".static BigFractionTWO_THIRDSA fraction representing "2/3".static BigFractionZEROA fraction representing "0". 
- 
Constructor Summary
Constructors Constructor Description BigFraction(double value)Create a fraction given the double value.BigFraction(double value, double epsilon, int maxIterations)Create a fraction given the double value and maximum error allowed.BigFraction(double value, int maxDenominator)Create a fraction given the double value and maximum denominator.BigFraction(int num)Create aBigFractionequivalent to the passedint, ie "num / 1".BigFraction(int num, int den)Create aBigFractiongiven the numerator and denominator as simpleint.BigFraction(long num)Create aBigFractionequivalent to the passed long, ie "num / 1".BigFraction(long num, long den)Create aBigFractiongiven the numerator and denominator as simplelong.BigFraction(BigInteger num)Create aBigFractionequivalent to the passedBigInteger, ie "num / 1".BigFraction(BigInteger num, BigInteger den)Create aBigFractiongiven the numerator and denominator asBigInteger. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFractionabs()Returns the absolute value of thisBigFraction.BigFractionadd(int i)Adds the value of this fraction to the passedinteger, returning the result in reduced form.BigFractionadd(long l)Adds the value of this fraction to the passedlong, returning the result in reduced form.BigFractionadd(BigInteger bg)Adds the value of this fraction to the passedBigInteger, returning the result in reduced form.BigFractionadd(BigFraction fraction)Adds the value of this fraction to another, returning the result in reduced form.BigDecimalbigDecimalValue()Gets the fraction as aBigDecimal.BigDecimalbigDecimalValue(int roundingMode)Gets the fraction as aBigDecimalfollowing the passed rounding mode.BigDecimalbigDecimalValue(int scale, int roundingMode)Gets the fraction as aBigDecimalfollowing the passed scale and rounding mode.intcompareTo(BigFraction object)Compares this object to another based on size.BigFractiondivide(int i)Divide the value of this fraction by the passedint, iethis * 1 / i, returning the result in reduced form.BigFractiondivide(long l)Divide the value of this fraction by the passedlong, iethis * 1 / l, returning the result in reduced form.BigFractiondivide(BigInteger bg)Divide the value of this fraction by the passedBigInteger, iethis * 1 / bg, returning the result in reduced form.BigFractiondivide(BigFraction fraction)Divide the value of this fraction by another, returning the result in reduced form.doubledoubleValue()Gets the fraction as adouble.booleanequals(Object other)Test for the equality of two fractions.floatfloatValue()Gets the fraction as afloat.BigIntegergetDenominator()Access the denominator as aBigInteger.intgetDenominatorAsInt()Access the denominator as aint.longgetDenominatorAsLong()Access the denominator as along.BigFractionFieldgetField()Get theFieldto which the instance belongs.BigIntegergetNumerator()Access the numerator as aBigInteger.intgetNumeratorAsInt()Access the numerator as aint.longgetNumeratorAsLong()Access the numerator as along.static BigFractiongetReducedFraction(int numerator, int denominator)Creates aBigFractioninstance with the 2 parts of a fraction Y/Z.inthashCode()Gets a hashCode for the fraction.intintValue()Gets the fraction as anint.longlongValue()Gets the fraction as along.BigFractionmultiply(int i)Multiply the value of this fraction by the passedint, returning the result in reduced form.BigFractionmultiply(long l)Multiply the value of this fraction by the passedlong, returning the result in reduced form.BigFractionmultiply(BigInteger bg)Multiplies the value of this fraction by the passedBigInteger, returning the result in reduced form.BigFractionmultiply(BigFraction fraction)Multiplies the value of this fraction by another, returning the result in reduced form.BigFractionnegate()Return the additive inverse of this fraction, returning the result in reduced form.doublepercentageValue()Gets the fraction percentage as adouble.doublepow(double exponent)Returns adoublewhose value is (thisexponent), returning the result in reduced form.BigFractionpow(int exponent)Returns aBigFractionwhose value is(this<sup>exponent</sup>), returning the result in reduced form.BigFractionpow(long exponent)Returns aBigFractionwhose value is (thisexponent), returning the result in reduced form.BigFractionpow(BigInteger exponent)Returns aBigFractionwhose value is (thisexponent), returning the result in reduced form.BigFractionreciprocal()Return the multiplicative inverse of this fraction.BigFractionreduce()Reduce thisBigFractionto its lowest terms.BigFractionsubtract(int i)Subtracts the value of anintegerfrom the value of thisBigFraction, returning the result in reduced form.BigFractionsubtract(long l)Subtracts the value of alongfrom the value of thisBigFraction, returning the result in reduced form.BigFractionsubtract(BigInteger bg)Subtracts the value of anBigIntegerfrom the value of thisBigFraction, returning the result in reduced form.BigFractionsubtract(BigFraction fraction)Subtracts the value of another fraction from the value of this one, returning the result in reduced form.StringtoString()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 Detail
- 
TWO
public static final BigFraction TWO
A fraction representing "2 / 1". 
- 
ONE
public static final BigFraction ONE
A fraction representing "1". 
- 
ZERO
public static final BigFraction ZERO
A fraction representing "0". 
- 
MINUS_ONE
public static final BigFraction MINUS_ONE
A fraction representing "-1 / 1". 
- 
FOUR_FIFTHS
public static final BigFraction FOUR_FIFTHS
A fraction representing "4/5". 
- 
ONE_FIFTH
public static final BigFraction ONE_FIFTH
A fraction representing "1/5". 
- 
ONE_HALF
public static final BigFraction ONE_HALF
A fraction representing "1/2". 
- 
ONE_QUARTER
public static final BigFraction ONE_QUARTER
A fraction representing "1/4". 
- 
ONE_THIRD
public static final BigFraction ONE_THIRD
A fraction representing "1/3". 
- 
THREE_FIFTHS
public static final BigFraction THREE_FIFTHS
A fraction representing "3/5". 
- 
THREE_QUARTERS
public static final BigFraction THREE_QUARTERS
A fraction representing "3/4". 
- 
TWO_FIFTHS
public static final BigFraction TWO_FIFTHS
A fraction representing "2/5". 
- 
TWO_QUARTERS
public static final BigFraction TWO_QUARTERS
A fraction representing "2/4". 
- 
TWO_THIRDS
public static final BigFraction TWO_THIRDS
A fraction representing "2/3". 
 - 
 
- 
Constructor Detail
- 
BigFraction
public BigFraction(BigInteger num)
Create a
BigFractionequivalent to the passedBigInteger, ie "num / 1".- Parameters:
 num- the numerator.
 
- 
BigFraction
public BigFraction(BigInteger num, BigInteger den)
Create aBigFractiongiven the numerator and denominator asBigInteger. TheBigFractionis reduced to lowest terms.- Parameters:
 num- the numerator, must not benull.den- the denominator, must not benull.- Throws:
 MathIllegalArgumentException- if the denominator is zero.NullArgumentException- if either of the arguments is null
 
- 
BigFraction
public BigFraction(double value) throws MathIllegalArgumentExceptionCreate a fraction given the double value.This constructor behaves differently from
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.Since this conversion is exact and since double numbers are sometimes approximated, the fraction created may seem strange in some cases. For example, calling
new BigFraction(1.0 / 3.0)does not 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).- Parameters:
 value- the double value to convert to a fraction.- Throws:
 MathIllegalArgumentException- if value is NaN or infinite- See Also:
 BigFraction(double, double, int)
 
- 
BigFraction
public BigFraction(double value, double epsilon, int maxIterations) throws MathIllegalStateExceptionCreate 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:
 MathIllegalStateException- if the continued fraction failed to converge.- See Also:
 BigFraction(double)
 
- 
BigFraction
public BigFraction(double value, int maxDenominator) throws MathIllegalStateExceptionCreate 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:
 MathIllegalStateException- if the continued fraction failed to converge.
 
- 
BigFraction
public BigFraction(int num)
Create a
BigFractionequivalent to the passedint, ie "num / 1".- Parameters:
 num- the numerator.
 
- 
BigFraction
public BigFraction(int num, int den)Create a
BigFractiongiven the numerator and denominator as simpleint. TheBigFractionis reduced to lowest terms.- Parameters:
 num- the numerator.den- the denominator.
 
- 
BigFraction
public BigFraction(long num)
Create a
BigFractionequivalent to the passed long, ie "num / 1".- Parameters:
 num- the numerator.
 
- 
BigFraction
public BigFraction(long num, long den)Create a
BigFractiongiven the numerator and denominator as simplelong. TheBigFractionis reduced to lowest terms.- Parameters:
 num- the numerator.den- the denominator.
 
 - 
 
- 
Method Detail
- 
getReducedFraction
public static BigFraction getReducedFraction(int numerator, int denominator)
Creates a
BigFractioninstance 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:
 ArithmeticException- if the denominator iszero.
 
- 
abs
public BigFraction abs()
Returns the absolute value of this
BigFraction.- Returns:
 - the absolute value as a 
BigFraction. 
 
- 
add
public BigFraction add(BigInteger bg) throws NullArgumentException
Adds the value of this fraction to the passed
BigInteger, returning the result in reduced form.- Parameters:
 bg- theBigIntegerto add, must'nt benull.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- if theBigIntegerisnull.
 
- 
add
public BigFraction add(int i)
Adds the value of this fraction to the passed
integer, returning the result in reduced form.- Parameters:
 i- theintegerto add.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
add
public BigFraction add(long l)
Adds the value of this fraction to the passed
long, returning the result in reduced form.- Parameters:
 l- thelongto add.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
add
public BigFraction add(BigFraction fraction)
Adds the value of this fraction to another, returning the result in reduced form.
- Specified by:
 addin interfaceFieldElement<BigFraction>- Parameters:
 fraction- theBigFractionto add, must not benull.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- if theBigFractionisnull.
 
- 
bigDecimalValue
public BigDecimal bigDecimalValue()
Gets the fraction as a
BigDecimal. This calculates the fraction as the numerator divided by denominator.- Returns:
 - the fraction as a 
BigDecimal. - Throws:
 ArithmeticException- if the exact quotient does not have a terminating decimal expansion.- See Also:
 BigDecimal
 
- 
bigDecimalValue
public BigDecimal bigDecimalValue(int roundingMode)
Gets the fraction as a
BigDecimalfollowing the passed rounding mode. This calculates the fraction as the numerator divided by denominator.- Parameters:
 roundingMode- rounding mode to apply. seeBigDecimalconstants.- Returns:
 - the fraction as a 
BigDecimal. - Throws:
 IllegalArgumentException- ifroundingModedoes not represent a valid rounding mode.- See Also:
 BigDecimal
 
- 
bigDecimalValue
public BigDecimal bigDecimalValue(int scale, int roundingMode)
Gets the fraction as a
BigDecimalfollowing the passed scale and rounding mode. This calculates the fraction as the numerator divided by denominator.- Parameters:
 scale- scale of theBigDecimalquotient to be returned. seeBigDecimalfor more information.roundingMode- rounding mode to apply. seeBigDecimalconstants.- Returns:
 - the fraction as a 
BigDecimal. - See Also:
 BigDecimal
 
- 
compareTo
public int compareTo(BigFraction object)
Compares this object to another based on size.
- Specified by:
 compareToin interfaceComparable<BigFraction>- Parameters:
 object- the object to compare to, must not benull.- Returns:
 - -1 if this is less than 
object, +1 if this is greater thanobject, 0 if they are equal. - See Also:
 Comparable.compareTo(java.lang.Object)
 
- 
divide
public BigFraction divide(BigInteger bg)
Divide the value of this fraction by the passed
BigInteger, iethis * 1 / bg, returning the result in reduced form.- Parameters:
 bg- theBigIntegerto divide by, must not benull- Returns:
 - a 
BigFractioninstance with the resulting values - Throws:
 NullArgumentException- if theBigIntegerisnullMathRuntimeException- if the fraction to divide by is zero
 
- 
divide
public BigFraction divide(int i)
Divide the value of this fraction by the passed
int, iethis * 1 / i, returning the result in reduced form.- Parameters:
 i- theintto divide by- Returns:
 - a 
BigFractioninstance with the resulting values - Throws:
 MathRuntimeException- if the fraction to divide by is zero
 
- 
divide
public BigFraction divide(long l)
Divide the value of this fraction by the passed
long, iethis * 1 / l, returning the result in reduced form.- Parameters:
 l- thelongto divide by- Returns:
 - a 
BigFractioninstance with the resulting values - Throws:
 MathRuntimeException- if the fraction to divide by is zero
 
- 
divide
public BigFraction divide(BigFraction fraction)
Divide the value of this fraction by another, returning the result in reduced form.
- Specified by:
 dividein interfaceFieldElement<BigFraction>- Parameters:
 fraction- Fraction to divide by, must not benull.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- if thefractionisnull.MathRuntimeException- if the fraction to divide by is zero
 
- 
doubleValue
public double doubleValue()
Gets the fraction as a
double. This calculates the fraction as the numerator divided by denominator.- Specified by:
 doubleValuein classNumber- Returns:
 - the fraction as a 
double - See Also:
 Number.doubleValue()
 
- 
equals
public boolean equals(Object other)
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.
- Overrides:
 equalsin classObject- Parameters:
 other- fraction to test for equality to this fraction, can benull.- Returns:
 - true if two fractions are equal, false if object is
         
null, not an instance ofBigFraction, or not equal to this fraction instance. - See Also:
 Object.equals(java.lang.Object)
 
- 
floatValue
public float floatValue()
Gets the fraction as a
float. This calculates the fraction as the numerator divided by denominator.- Specified by:
 floatValuein classNumber- Returns:
 - the fraction as a 
float. - See Also:
 Number.floatValue()
 
- 
getDenominator
public BigInteger getDenominator()
Access the denominator as a
BigInteger.- Returns:
 - the denominator as a 
BigInteger. 
 
- 
getDenominatorAsInt
public int getDenominatorAsInt()
Access the denominator as a
int.- Returns:
 - the denominator as a 
int. 
 
- 
getDenominatorAsLong
public long getDenominatorAsLong()
Access the denominator as a
long.- Returns:
 - the denominator as a 
long. 
 
- 
getNumerator
public BigInteger getNumerator()
Access the numerator as a
BigInteger.- Returns:
 - the numerator as a 
BigInteger. 
 
- 
getNumeratorAsInt
public int getNumeratorAsInt()
Access the numerator as a
int.- Returns:
 - the numerator as a 
int. 
 
- 
getNumeratorAsLong
public long getNumeratorAsLong()
Access the numerator as a
long.- Returns:
 - the numerator as a 
long. 
 
- 
hashCode
public int hashCode()
Gets a hashCode for the fraction.
- Overrides:
 hashCodein classObject- Returns:
 - a hash code value for this object.
 - See Also:
 Object.hashCode()
 
- 
intValue
public int intValue()
Gets the fraction as an
int. This returns the whole number part of the fraction.- Specified by:
 intValuein classNumber- Returns:
 - the whole number fraction part.
 - See Also:
 Number.intValue()
 
- 
longValue
public long longValue()
Gets the fraction as a
long. This returns the whole number part of the fraction.- Specified by:
 longValuein classNumber- Returns:
 - the whole number fraction part.
 - See Also:
 Number.longValue()
 
- 
multiply
public BigFraction multiply(BigInteger bg)
Multiplies the value of this fraction by the passed
BigInteger, returning the result in reduced form.- Parameters:
 bg- theBigIntegerto multiply by.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- ifbgisnull.
 
- 
multiply
public BigFraction multiply(int i)
Multiply the value of this fraction by the passed
int, returning the result in reduced form.- Specified by:
 multiplyin interfaceFieldElement<BigFraction>- Parameters:
 i- theintto multiply by.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
multiply
public BigFraction multiply(long l)
Multiply the value of this fraction by the passed
long, returning the result in reduced form.- Parameters:
 l- thelongto multiply by.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
multiply
public BigFraction multiply(BigFraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
- Specified by:
 multiplyin interfaceFieldElement<BigFraction>- Parameters:
 fraction- Fraction to multiply by, must not benull.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- iffractionisnull.
 
- 
negate
public BigFraction negate()
Return the additive inverse of this fraction, returning the result in reduced form.
- Specified by:
 negatein interfaceFieldElement<BigFraction>- Returns:
 - the negation of this fraction.
 
 
- 
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. 
 
- 
pow
public BigFraction pow(int exponent)
Returns a
BigFractionwhose value is(this<sup>exponent</sup>), returning the result in reduced form.- Parameters:
 exponent- exponent to which thisBigFractionis to be raised.- Returns:
 - thisexponent.
 
 
- 
pow
public BigFraction pow(long exponent)
Returns a
BigFractionwhose value is (thisexponent), returning the result in reduced form.- Parameters:
 exponent- exponent to which thisBigFractionis to be raised.- Returns:
 - thisexponent as a 
BigFraction. 
 
- 
pow
public BigFraction pow(BigInteger exponent)
Returns a
BigFractionwhose value is (thisexponent), returning the result in reduced form.- Parameters:
 exponent- exponent to which thisBigFractionis to be raised.- Returns:
 - thisexponent as a 
BigFraction. 
 
- 
pow
public double pow(double exponent)
Returns a
doublewhose value is (thisexponent), returning the result in reduced form.- Parameters:
 exponent- exponent to which thisBigFractionis to be raised.- Returns:
 - thisexponent.
 
 
- 
reciprocal
public BigFraction reciprocal()
Return the multiplicative inverse of this fraction.
- Specified by:
 reciprocalin interfaceFieldElement<BigFraction>- Returns:
 - the reciprocal fraction.
 
 
- 
reduce
public BigFraction reduce()
Reduce this
BigFractionto its lowest terms.- Returns:
 - the reduced 
BigFraction. It doesn't change anything if the fraction can be reduced. 
 
- 
subtract
public BigFraction subtract(BigInteger bg)
Subtracts the value of an
BigIntegerfrom the value of thisBigFraction, returning the result in reduced form.- Parameters:
 bg- theBigIntegerto subtract, cannot benull.- Returns:
 - a 
BigFractioninstance with the resulting values. - Throws:
 NullArgumentException- if theBigIntegerisnull.
 
- 
subtract
public BigFraction subtract(int i)
Subtracts the value of an
integerfrom the value of thisBigFraction, returning the result in reduced form.- Parameters:
 i- theintegerto subtract.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
subtract
public BigFraction subtract(long l)
Subtracts the value of a
longfrom the value of thisBigFraction, returning the result in reduced form.- Parameters:
 l- thelongto subtract.- Returns:
 - a 
BigFractioninstance with the resulting values. 
 
- 
subtract
public BigFraction subtract(BigFraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
- Specified by:
 subtractin interfaceFieldElement<BigFraction>- Parameters:
 fraction-BigFractionto subtract, must not benull.- Returns:
 - a 
BigFractioninstance with the resulting values - Throws:
 NullArgumentException- if thefractionisnull.
 
- 
toString
public String toString()
Returns the
Stringrepresenting this fraction, ie "num / dem" or just "num" if the denominator is one.- Overrides:
 toStringin classObject- Returns:
 - a string representation of the fraction.
 - See Also:
 Object.toString()
 
- 
getField
public BigFractionField getField()
Get theFieldto which the instance belongs.- Specified by:
 getFieldin interfaceFieldElement<BigFraction>- Returns:
 Fieldto which the instance belongs
 
 - 
 
 -