Class BigFraction
- All Implemented Interfaces:
Serializable
,Comparable<BigFraction>
,FieldElement<BigFraction>
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A test to determine if a series of fractions has converged. -
Field Summary
Modifier and TypeFieldDescriptionstatic final BigFraction
A fraction representing "4/5".static final BigFraction
A fraction representing "-1 / 1".static final BigFraction
A fraction representing "1".static final BigFraction
A fraction representing "1/5".static final BigFraction
A fraction representing "1/2".static final BigFraction
A fraction representing "1/4".static final BigFraction
A fraction representing "1/3".static final BigFraction
A fraction representing "3/5".static final BigFraction
A fraction representing "3/4".static final BigFraction
A fraction representing "2 / 1".static final BigFraction
A fraction representing "2/5".static final BigFraction
A fraction representing "2/4".static final BigFraction
A fraction representing "2/3".static final BigFraction
A fraction representing "0". -
Constructor Summary
ConstructorDescriptionBigFraction
(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, long maxDenominator) Create a fraction given the double value and maximum denominator.BigFraction
(int num) Create aBigFraction
equivalent to the passedint
, ie "num / 1".BigFraction
(int num, int den) Create aBigFraction
given the numerator and denominator as simpleint
.BigFraction
(long num) Create aBigFraction
equivalent to the passed long, ie "num / 1".BigFraction
(long num, long den) Create aBigFraction
given the numerator and denominator as simplelong
.BigFraction
(BigInteger num) Create aBigFraction
equivalent to the passedBigInteger
, ie "num / 1".BigFraction
(BigInteger num, BigInteger den) Create aBigFraction
given the numerator and denominator asBigInteger
. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns the absolute value of thisBigFraction
.add
(int i) Adds the value of this fraction to the passedinteger
, returning the result in reduced form.add
(long l) Adds the value of this fraction to the passedlong
, returning the result in reduced form.add
(BigInteger bg) Adds the value of this fraction to the passedBigInteger
, returning the result in reduced form.add
(BigFraction fraction) Adds the value of this fraction to another, returning the result in reduced form.Gets the fraction as aBigDecimal
.bigDecimalValue
(int scale, RoundingMode roundingMode) Gets the fraction as aBigDecimal
following the passed scale and rounding mode.bigDecimalValue
(RoundingMode roundingMode) Gets the fraction as aBigDecimal
following the passed rounding mode.int
compareTo
(BigFraction object) Compares this object to another based on size.static Pair<BigFraction,
Boolean> convergent
(double value, int maxConvergents, BigFraction.ConvergenceTest convergenceTest) Returns the last element of the series of convergent-steps to approximate the given value.static Stream<BigFraction>
convergents
(double value, int maxConvergents) Generate astream
of convergents from a real number.divide
(int i) Divide the value of this fraction by the passedint
, iethis * 1 / i
, returning the result in reduced form.divide
(long l) Divide the value of this fraction by the passedlong
, iethis * 1 / l
, returning the result in reduced form.divide
(BigInteger bg) Divide the value of this fraction by the passedBigInteger
, iethis * 1 / bg
, returning the result in reduced form.divide
(BigFraction fraction) Divide the value of this fraction by another, returning the result in reduced form.double
Gets the fraction as adouble
.boolean
Test for the equality of two fractions.float
Gets the fraction as afloat
.gcd
(BigFraction s) Rational number greatest common divisor.Access the denominator as aBigInteger
.int
Access the denominator as aint
.long
Access the denominator as along
.getField()
Get theField
to which the instance belongs.Access the numerator as aBigInteger
.int
Access the numerator as aint
.long
Access the numerator as along
.double
getReal()
Get the real value of the number.static BigFraction
getReducedFraction
(int numerator, int denominator) Creates aBigFraction
instance with the 2 parts of a fraction Y/Z.int
hashCode()
Gets a hashCode for the fraction.int
intValue()
Gets the fraction as anint
.boolean
Check if a fraction is an integer.lcm
(BigFraction s) Rational number least common multiple.long
Gets the fraction as along
.multiply
(int i) Multiply the value of this fraction by the passedint
, returning the result in reduced form.multiply
(long l) Multiply the value of this fraction by the passedlong
, returning the result in reduced form.multiply
(BigInteger bg) Multiplies the value of this fraction by the passedBigInteger
, returning the result in reduced form.multiply
(BigFraction fraction) Multiplies the value of this fraction by another, returning the result in reduced form.negate()
Return the additive inverse of this fraction, returning the result in reduced form.double
Gets the fraction percentage as adouble
.double
pow
(double exponent) Returns adouble
whose value is thisexponent, returning the result in reduced form.pow
(int exponent) Returns aBigFraction
whose value is(this<sup>exponent</sup>)
, returning the result in reduced form.pow
(long exponent) Returns aBigFraction
whose value is thisexponent, returning the result in reduced form.pow
(BigInteger exponent) Returns aBigFraction
whose value is thisexponent, returning the result in reduced form.Return the multiplicative inverse of this fraction.reduce()
Reduce thisBigFraction
to its lowest terms.int
signum()
Returns the signum function of thisBigFraction
.subtract
(int i) Subtracts the value of aninteger
from the value of thisBigFraction
, returning the result in reduced form.subtract
(long l) Subtracts the value of along
from the value of thisBigFraction
, returning the result in reduced form.subtract
(BigInteger bg) Subtracts the value of anBigInteger
from the value of thisBigFraction
, returning the result in reduced form.subtract
(BigFraction fraction) Subtracts the value of another fraction from the value of this one, returning the result in reduced form.toString()
Returns theString
representing 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 Details
-
TWO
A fraction representing "2 / 1". -
ONE
A fraction representing "1". -
ZERO
A fraction representing "0". -
MINUS_ONE
A fraction representing "-1 / 1". -
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".
-
-
Constructor Details
-
BigFraction
Create a
BigFraction
equivalent to the passedBigInteger
, ie "num / 1".- Parameters:
num
- the numerator.
-
BigFraction
Create aBigFraction
given the numerator and denominator asBigInteger
. TheBigFraction
is 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
Create 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
public BigFraction(double value, double epsilon, int maxIterations) throws MathIllegalStateException 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 withinepsilon
ofvalue
, in absolute terms.maxIterations
- maximum number of convergents.- Throws:
MathIllegalStateException
- if the continued fraction failed to converge.- See Also:
-
BigFraction
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:
MathIllegalStateException
- if the continued fraction failed to converge.
-
BigFraction
public BigFraction(int num) Create a
BigFraction
equivalent to the passedint
, ie "num / 1".- Parameters:
num
- the numerator.
-
BigFraction
public BigFraction(int num, int den) Create a
BigFraction
given the numerator and denominator as simpleint
. TheBigFraction
is reduced to lowest terms.- Parameters:
num
- the numerator.den
- the denominator.
-
BigFraction
public BigFraction(long num) Create a
BigFraction
equivalent to the passed long, ie "num / 1".- Parameters:
num
- the numerator.
-
BigFraction
public BigFraction(long num, long den) Create a
BigFraction
given the numerator and denominator as simplelong
. TheBigFraction
is reduced to lowest terms.- Parameters:
num
- the numerator.den
- the denominator.
-
-
Method Details
-
convergents
Generate astream
of convergents from a real number.- Parameters:
value
- value to approximatemaxConvergents
- maximum number of convergents.- Returns:
- stream of
BigFraction
convergents approximatingvalue
- Since:
- 2.1
-
convergent
public static Pair<BigFraction,Boolean> convergent(double value, int maxConvergents, BigFraction.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
convergenceTest
or after at mostmaxConvergents
elements. The returned Pair consists of that terminalBigFraction
and aBoolean
that indicates if it satisfies the given convergence tests. If the returned pair's value isfalse
the element at positionmaxConvergents
was 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:
getReal
in interfaceFieldElement<BigFraction>
- Returns:
- real value
-
getReducedFraction
Creates a
BigFraction
instance 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
Returns the absolute value of this
BigFraction
.- Returns:
- the absolute value as a
BigFraction
.
-
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 thisBigFraction
.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
BigFraction
- Since:
- 1.7
-
add
Adds the value of this fraction to the passed
BigInteger
, returning the result in reduced form.- Parameters:
bg
- theBigInteger
to add, must'nt benull
.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- if theBigInteger
isnull
.
-
add
Adds the value of this fraction to the passed
integer
, returning the result in reduced form.- Parameters:
i
- theinteger
to add.- Returns:
- a
BigFraction
instance with the resulting values.
-
add
Adds the value of this fraction to the passed
long
, returning the result in reduced form.- Parameters:
l
- thelong
to add.- Returns:
- a
BigFraction
instance with the resulting values.
-
add
Adds the value of this fraction to another, returning the result in reduced form.
- Specified by:
add
in interfaceFieldElement<BigFraction>
- Parameters:
fraction
- theBigFraction
to add, must not benull
.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- if theBigFraction
isnull
.
-
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:
-
bigDecimalValue
Gets the fraction as a
BigDecimal
following the passed rounding mode. This calculates the fraction as the numerator divided by denominator.- Parameters:
roundingMode
- rounding mode to apply. seeBigDecimal
constants.- Returns:
- the fraction as a
BigDecimal
. - Throws:
IllegalArgumentException
- ifroundingMode
does not represent a valid rounding mode.- See Also:
-
bigDecimalValue
Gets the fraction as a
BigDecimal
following the passed scale and rounding mode. This calculates the fraction as the numerator divided by denominator.- Parameters:
scale
- scale of theBigDecimal
quotient to be returned. seeBigDecimal
for more information.roundingMode
- rounding mode to apply. seeBigDecimal
constants.- Returns:
- the fraction as a
BigDecimal
. - See Also:
-
compareTo
Compares this object to another based on size.
- Specified by:
compareTo
in 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:
-
divide
Divide the value of this fraction by the passed
BigInteger
, iethis * 1 / bg
, returning the result in reduced form.- Parameters:
bg
- theBigInteger
to divide by, must not benull
- Returns:
- a
BigFraction
instance with the resulting values - Throws:
NullArgumentException
- if theBigInteger
isnull
MathRuntimeException
- if the fraction to divide by is zero
-
divide
Divide the value of this fraction by the passed
int
, iethis * 1 / i
, returning the result in reduced form.- Parameters:
i
- theint
to divide by- Returns:
- a
BigFraction
instance with the resulting values - Throws:
MathRuntimeException
- if the fraction to divide by is zero
-
divide
Divide the value of this fraction by the passed
long
, iethis * 1 / l
, returning the result in reduced form.- Parameters:
l
- thelong
to divide by- Returns:
- a
BigFraction
instance with the resulting values - Throws:
MathRuntimeException
- if the fraction to divide by is zero
-
divide
Divide the value of this fraction by another, returning the result in reduced form.
- Specified by:
divide
in interfaceFieldElement<BigFraction>
- Parameters:
fraction
- Fraction to divide by, must not benull
.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- if thefraction
isnull
.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:
doubleValue
in classNumber
- Returns:
- the fraction as a
double
- See Also:
-
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.
- Overrides:
equals
in 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:
-
floatValue
public float floatValue()Gets the fraction as a
float
. This calculates the fraction as the numerator divided by denominator.- Specified by:
floatValue
in classNumber
- Returns:
- the fraction as a
float
. - See Also:
-
gcd
Rational number greatest common divisor.- Parameters:
s
- fraction.- Returns:
- gcd(this, s).
- Since:
- 3.1
-
lcm
Rational number least common multiple.- Parameters:
s
- fraction.- Returns:
- lcm(this, s).
- Since:
- 3.1
-
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
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.
-
intValue
public int intValue()Gets the fraction as an
int
. This returns the whole number part of the fraction. -
longValue
public long longValue()Gets the fraction as a
long
. This returns the whole number part of the fraction. -
multiply
Multiplies the value of this fraction by the passed
BigInteger
, returning the result in reduced form.- Parameters:
bg
- theBigInteger
to multiply by.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- ifbg
isnull
.
-
multiply
Multiply the value of this fraction by the passed
int
, returning the result in reduced form.- Specified by:
multiply
in interfaceFieldElement<BigFraction>
- Parameters:
i
- theint
to multiply by.- Returns:
- a
BigFraction
instance with the resulting values.
-
multiply
Multiply the value of this fraction by the passed
long
, returning the result in reduced form.- Parameters:
l
- thelong
to multiply by.- Returns:
- a
BigFraction
instance with the resulting values.
-
multiply
Multiplies the value of this fraction by another, returning the result in reduced form.
- Specified by:
multiply
in interfaceFieldElement<BigFraction>
- Parameters:
fraction
- Fraction to multiply by, must not benull
.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- iffraction
isnull
.
-
negate
Return the additive inverse of this fraction, returning the result in reduced form.
- Specified by:
negate
in 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
Returns a
BigFraction
whose value is(this<sup>exponent</sup>)
, returning the result in reduced form.- Parameters:
exponent
- exponent to which thisBigFraction
is to be raised.- Returns:
- thisexponent
-
pow
Returns a
BigFraction
whose value is thisexponent, returning the result in reduced form.- Parameters:
exponent
- exponent to which thisBigFraction
is to be raised.- Returns:
- thisexponent as a
BigFraction
.
-
pow
Returns a
BigFraction
whose value is thisexponent, returning the result in reduced form.- Parameters:
exponent
- exponent to which thisBigFraction
is to be raised.- Returns:
- thisexponent as a
BigFraction
.
-
pow
public double pow(double exponent) Returns a
double
whose value is thisexponent, returning the result in reduced form.- Parameters:
exponent
- exponent to which thisBigFraction
is to be raised.- Returns:
- thisexponent
-
reciprocal
Return the multiplicative inverse of this fraction.
- Specified by:
reciprocal
in interfaceFieldElement<BigFraction>
- Returns:
- the reciprocal fraction.
-
reduce
Reduce this
BigFraction
to its lowest terms.- Returns:
- the reduced
BigFraction
. It doesn't change anything if the fraction can be reduced.
-
subtract
Subtracts the value of an
BigInteger
from the value of thisBigFraction
, returning the result in reduced form.- Parameters:
bg
- theBigInteger
to subtract, cannot benull
.- Returns:
- a
BigFraction
instance with the resulting values. - Throws:
NullArgumentException
- if theBigInteger
isnull
.
-
subtract
Subtracts the value of an
integer
from the value of thisBigFraction
, returning the result in reduced form.- Parameters:
i
- theinteger
to subtract.- Returns:
- a
BigFraction
instance with the resulting values.
-
subtract
Subtracts the value of a
long
from the value of thisBigFraction
, returning the result in reduced form.- Parameters:
l
- thelong
to subtract.- Returns:
- a
BigFraction
instance with the resulting values.
-
subtract
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
- Specified by:
subtract
in interfaceFieldElement<BigFraction>
- Parameters:
fraction
-BigFraction
to subtract, must not benull
.- Returns:
- a
BigFraction
instance with the resulting values - Throws:
NullArgumentException
- if thefraction
isnull
.
-
toString
Returns the
String
representing this fraction, ie "num / dem" or just "num" if the denominator is one. -
getField
Get theField
to which the instance belongs.- Specified by:
getField
in interfaceFieldElement<BigFraction>
- Returns:
Field
to which the instance belongs
-