Class Fraction
- java.lang.Object
-
- java.lang.Number
-
- org.hipparchus.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.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFraction.ConvergenceTestA test to determine if a series of fractions has converged.
-
Field Summary
Fields Modifier and Type Field Description static FractionFOUR_FIFTHSA fraction representing "4/5".static FractionMINUS_ONEA fraction representing "-1 / 1".static FractionONEA fraction representing "1".static FractionONE_FIFTHA fraction representing "1/5".static FractionONE_HALFA fraction representing "1/2".static FractionONE_QUARTERA fraction representing "1/4".static FractionONE_THIRDA fraction representing "1/3".static FractionTHREE_FIFTHSA fraction representing "3/5".static FractionTHREE_QUARTERSA fraction representing "3/4".static FractionTWOA fraction representing "2 / 1".static FractionTWO_FIFTHSA fraction representing "2/5".static FractionTWO_QUARTERSA fraction representing "2/4".static FractionTWO_THIRDSA fraction representing "2/3".static FractionZEROA fraction representing "0".
-
Constructor Summary
Constructors Constructor Description Fraction(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Fractionabs()Returns the absolute value of this fraction.Fractionadd(int i)Add an integer to the fraction.Fractionadd(Fraction fraction)Adds the value of this fraction to another, returning the result in reduced form.intcompareTo(Fraction object)Compares this object to another based on size.static Pair<Fraction,Boolean>convergent(double value, int maxConvergents, Fraction.ConvergenceTest convergenceTest)Returns the last element of the series of convergent-steps to approximate the given value.static Stream<Fraction>convergents(double value, int maxConvergents)Generate astreamof convergents from a real number.Fractiondivide(int i)Divide the fraction by an integer.Fractiondivide(Fraction fraction)Divide the value of this fraction by another.doubledoubleValue()Gets the fraction as adouble.booleanequals(Object other)Test for the equality of two fractions.floatfloatValue()Gets the fraction as afloat.Fractiongcd(Fraction s)Rational number greatest common divisor.intgetDenominator()Access the denominator.FractionFieldgetField()Get theFieldto which the instance belongs.intgetNumerator()Access the numerator.doublegetReal()Get the real value of the number.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.booleanisInteger()Check if a fraction is an integer.Fractionlcm(Fraction s)Rational number least common multiple.longlongValue()Gets the fraction as along.Fractionmultiply(int i)Multiply the fraction by an integer.Fractionmultiply(Fraction fraction)Multiplies the value of this fraction by another, returning the result in reduced form.Fractionnegate()Return the additive inverse of this fraction.doublepercentageValue()Gets the fraction percentage as adouble.Fractionreciprocal()Return the multiplicative inverse of this fraction.intsignum()Returns the signum function of this fraction.Fractionsubtract(int i)Subtract an integer from the fraction.Fractionsubtract(Fraction 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
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.hipparchus.FieldElement
isZero
-
-
-
-
Field Detail
-
TWO
public static final Fraction TWO
A fraction representing "2 / 1".
-
ONE
public static final Fraction ONE
A fraction representing "1".
-
ZERO
public static final Fraction ZERO
A fraction representing "0".
-
FOUR_FIFTHS
public static final Fraction FOUR_FIFTHS
A fraction representing "4/5".
-
ONE_FIFTH
public static final Fraction ONE_FIFTH
A fraction representing "1/5".
-
ONE_HALF
public static final Fraction ONE_HALF
A fraction representing "1/2".
-
ONE_QUARTER
public static final Fraction ONE_QUARTER
A fraction representing "1/4".
-
ONE_THIRD
public static final Fraction ONE_THIRD
A fraction representing "1/3".
-
THREE_FIFTHS
public static final Fraction THREE_FIFTHS
A fraction representing "3/5".
-
THREE_QUARTERS
public static final Fraction THREE_QUARTERS
A fraction representing "3/4".
-
TWO_FIFTHS
public static final Fraction TWO_FIFTHS
A fraction representing "2/5".
-
TWO_QUARTERS
public static final Fraction TWO_QUARTERS
A fraction representing "2/4".
-
TWO_THIRDS
public static final Fraction TWO_THIRDS
A fraction representing "2/3".
-
MINUS_ONE
public static final Fraction MINUS_ONE
A fraction representing "-1 / 1".
-
-
Constructor Detail
-
Fraction
public Fraction(double value) throws MathIllegalStateExceptionCreate a fraction given the double value.- Parameters:
value- the double value to convert to a fraction.- Throws:
MathIllegalStateException- if the continued fraction failed to converge.
-
Fraction
public Fraction(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.
-
Fraction
public Fraction(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
-
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:
MathRuntimeException- if the denominator iszero
-
-
Method Detail
-
convergents
public static Stream<Fraction> convergents(double value, int maxConvergents)
Generate astreamof convergents from a real number.- Parameters:
value- value to approximatemaxConvergents- maximum number of convergents.- Returns:
- stream of
Fractionconvergents approximatingvalue - Since:
- 2.1
-
convergent
public static Pair<Fraction,Boolean> convergent(double value, int maxConvergents, Fraction.ConvergenceTest convergenceTest)
Returns the last element of the series of convergent-steps to approximate the given value.The series terminates either at the first step that satisfies the given
convergenceTestor after at mostmaxConvergentselements. The returned Pair consists of that terminalFractionand aBooleanthat indicates if it satisfies the given convergence tests. If the returned pair's value isfalsethe element at positionmaxConvergentswas examined but failed to satisfy theconvergenceTest. A caller can then decide to accept the result nevertheless or to discard it. This method is usually faster thanconvergents(double, int)if only the terminal element is of interest.- Parameters:
value- value to approximatemaxConvergents- maximum number of convergents to examineconvergenceTest- the test if the series has converged at a step- Returns:
- the pair of last element of the series of convergents and a boolean indicating if that element satisfies the specified convergent test
-
getReal
public double getReal()
Get the real value of the number.- Specified by:
getRealin interfaceFieldElement<Fraction>- Returns:
- real value
-
isInteger
public boolean isInteger()
Check if a fraction is an integer.- Returns:
- true of fraction is an integer
-
signum
public int signum()
Returns the signum function of this fraction.The return value is -1 if the specified value is negative; 0 if the specified value is zero; and 1 if the specified value is positive.
- Returns:
- the signum function of this fraction
- Since:
- 1.7
-
abs
public Fraction abs()
Returns the absolute value of this fraction.- Returns:
- the absolute value.
-
compareTo
public int compareTo(Fraction object)
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
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.
-
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
-
gcd
public Fraction gcd(Fraction s)
Rational number greatest common divisor.- Parameters:
s- fraction.- Returns:
- gcd(this, s).
- Since:
- 3.1
-
lcm
public Fraction lcm(Fraction s)
Rational number least common multiple.- Parameters:
s- fraction.- Returns:
- lcm(this, s).
- Since:
- 3.1
-
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
public Fraction negate()
Return the additive inverse of this fraction.- Specified by:
negatein interfaceFieldElement<Fraction>- Returns:
- the negation of this fraction.
-
reciprocal
public Fraction reciprocal()
Return the multiplicative inverse of this fraction.- Specified by:
reciprocalin interfaceFieldElement<Fraction>- Returns:
- the reciprocal fraction
-
add
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.- 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 isnullMathRuntimeException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
add
public Fraction add(int i)
Add an integer to the fraction.- Parameters:
i- theintegerto add.- Returns:
- this + i
-
subtract
public Fraction subtract(Fraction fraction)
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 isnullMathRuntimeException- if the resulting numerator or denominator cannot be represented in anint.
-
subtract
public Fraction subtract(int i)
Subtract an integer from the fraction.- Parameters:
i- theintegerto subtract.- Returns:
- this - i
-
multiply
public Fraction multiply(Fraction fraction)
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 isnullMathRuntimeException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
multiply
public Fraction multiply(int i)
Multiply the fraction by an integer.- Specified by:
multiplyin interfaceFieldElement<Fraction>- Parameters:
i- theintegerto multiply by.- Returns:
- this * i
-
divide
public Fraction divide(Fraction fraction)
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 isnullMathRuntimeException- if the fraction to divide by is zeroMathRuntimeException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
divide
public Fraction divide(int i)
Divide the fraction by an integer.- Parameters:
i- theintegerto divide by.- Returns:
- this * i
-
percentageValue
public double percentageValue()
Gets the fraction percentage as adouble. This calculates the fraction as the numerator divided by denominator multiplied by 100.- Returns:
- the fraction percentage as a
double.
-
getReducedFraction
public static Fraction getReducedFraction(int numerator, int denominator)
Creates aFractioninstance 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:
MathRuntimeException- if the denominator iszero
-
toString
public String toString()
Returns theStringrepresenting 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 FractionField getField()
Get theFieldto which the instance belongs.- Specified by:
getFieldin interfaceFieldElement<Fraction>- Returns:
Fieldto which the instance belongs
-
-