public class BigFraction extends Number implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable
Modifier and Type | Field and Description |
---|---|
static BigFraction |
FOUR_FIFTHS
A fraction representing "4/5".
|
static BigFraction |
MINUS_ONE
A fraction representing "-1 / 1".
|
static BigFraction |
ONE
A fraction representing "1".
|
static BigFraction |
ONE_FIFTH
A fraction representing "1/5".
|
static BigFraction |
ONE_HALF
A fraction representing "1/2".
|
static BigFraction |
ONE_QUARTER
A fraction representing "1/4".
|
static BigFraction |
ONE_THIRD
A fraction representing "1/3".
|
static BigFraction |
THREE_FIFTHS
A fraction representing "3/5".
|
static BigFraction |
THREE_QUARTERS
A fraction representing "3/4".
|
static BigFraction |
TWO
A fraction representing "2 / 1".
|
static BigFraction |
TWO_FIFTHS
A fraction representing "2/5".
|
static BigFraction |
TWO_QUARTERS
A fraction representing "2/4".
|
static BigFraction |
TWO_THIRDS
A fraction representing "2/3".
|
static BigFraction |
ZERO
A fraction representing "0".
|
Constructor and Description |
---|
BigFraction(BigInteger num)
Create a
BigFraction equivalent to the passed BigInteger , ie
"num / 1". |
BigFraction(BigInteger num,
BigInteger den)
Create a
BigFraction given the numerator and denominator as
BigInteger . |
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 a
BigFraction equivalent to the passed int , ie
"num / 1". |
BigFraction(int num,
int den)
Create a
BigFraction given the numerator and denominator as simple
int . |
BigFraction(long num)
Create a
BigFraction equivalent to the passed long, ie "num / 1". |
BigFraction(long num,
long den)
Create a
BigFraction given the numerator and denominator as simple
long . |
Modifier and Type | Method and Description |
---|---|
BigFraction |
abs()
Returns the absolute value of this
BigFraction . |
BigFraction |
add(BigFraction fraction)
Adds the value of this fraction to another, returning the result in
reduced form.
|
BigFraction |
add(BigInteger bg)
Adds the value of this fraction to the passed
BigInteger ,
returning the result in reduced form. |
BigFraction |
add(int i)
Adds the value of this fraction to the passed
integer , returning
the result in reduced form. |
BigFraction |
add(long l)
Adds the value of this fraction to the passed
long , returning
the result in reduced form. |
BigDecimal |
bigDecimalValue()
Gets the fraction as a
BigDecimal . |
BigDecimal |
bigDecimalValue(int roundingMode)
Gets the fraction as a
BigDecimal following the passed
rounding mode. |
BigDecimal |
bigDecimalValue(int scale,
int roundingMode)
Gets the fraction as a
BigDecimal following the passed scale
and rounding mode. |
int |
compareTo(BigFraction object)
Compares this object to another based on size.
|
BigFraction |
divide(BigFraction fraction)
Divide the value of this fraction by another, returning the result in
reduced form.
|
BigFraction |
divide(BigInteger bg)
Divide the value of this fraction by the passed
BigInteger ,
ie this * 1 / bg , returning the result in reduced form. |
BigFraction |
divide(int i)
Divide the value of this fraction by the passed
int , ie
this * 1 / i , returning the result in reduced form. |
BigFraction |
divide(long l)
Divide the value of this fraction by the passed
long , ie
this * 1 / l , returning the result in reduced form. |
double |
doubleValue()
Gets the fraction as a
double . |
boolean |
equals(Object other)
Test for the equality of two fractions.
|
float |
floatValue()
Gets the fraction as a
float . |
BigInteger |
getDenominator()
Access the denominator as a
BigInteger . |
int |
getDenominatorAsInt()
Access the denominator as a
int . |
long |
getDenominatorAsLong()
Access the denominator as a
long . |
BigFractionField |
getField()
Get the
Field to which the instance belongs. |
BigInteger |
getNumerator()
Access the numerator as a
BigInteger . |
int |
getNumeratorAsInt()
Access the numerator as a
int . |
long |
getNumeratorAsLong()
Access the numerator as a
long . |
static BigFraction |
getReducedFraction(int numerator,
int denominator)
Creates a
BigFraction 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 . |
BigFraction |
multiply(BigFraction fraction)
Multiplies the value of this fraction by another, returning the result in
reduced form.
|
BigFraction |
multiply(BigInteger bg)
Multiplies the value of this fraction by the passed
BigInteger , returning the result in reduced form. |
BigFraction |
multiply(int i)
Multiply the value of this fraction by the passed
int , returning
the result in reduced form. |
BigFraction |
multiply(long l)
Multiply the value of this fraction by the passed
long ,
returning the result in reduced form. |
BigFraction |
negate()
Return the additive inverse of this fraction, returning the result in
reduced form.
|
double |
percentageValue()
Gets the fraction percentage as a
double . |
BigFraction |
pow(BigInteger exponent)
Returns a
BigFraction whose value is
(thisexponent), returning the result in reduced form. |
double |
pow(double exponent)
Returns a
double whose value is
(thisexponent), returning the result in reduced form. |
BigFraction |
pow(int exponent)
Returns a
BigFraction whose value is
(this<sup>exponent</sup>) , returning the result in reduced form. |
BigFraction |
pow(long exponent)
Returns a
BigFraction whose value is
(thisexponent), returning the result in reduced form. |
BigFraction |
reciprocal()
Return the multiplicative inverse of this fraction.
|
BigFraction |
reduce()
Reduce this
BigFraction to its lowest terms. |
BigFraction |
subtract(BigFraction fraction)
Subtracts the value of another fraction from the value of this one,
returning the result in reduced form.
|
BigFraction |
subtract(BigInteger bg)
Subtracts the value of an
BigInteger from the value of this
BigFraction , returning the result in reduced form. |
BigFraction |
subtract(int i)
Subtracts the value of an
integer from the value of this
BigFraction , returning the result in reduced form. |
BigFraction |
subtract(long l)
Subtracts the value of a
long from the value of this
BigFraction , returning the result in reduced form. |
String |
toString()
Returns the
String representing this fraction, ie
"num / dem" or just "num" if the denominator is one. |
byteValue, shortValue
public static final BigFraction TWO
public static final BigFraction ONE
public static final BigFraction ZERO
public static final BigFraction MINUS_ONE
public static final BigFraction FOUR_FIFTHS
public static final BigFraction ONE_FIFTH
public static final BigFraction ONE_HALF
public static final BigFraction ONE_QUARTER
public static final BigFraction ONE_THIRD
public static final BigFraction THREE_FIFTHS
public static final BigFraction THREE_QUARTERS
public static final BigFraction TWO_FIFTHS
public static final BigFraction TWO_QUARTERS
public static final BigFraction TWO_THIRDS
public BigFraction(BigInteger num)
Create a BigFraction
equivalent to the passed BigInteger
, ie
"num / 1".
num
- the numerator.public BigFraction(BigInteger num, BigInteger den)
BigFraction
given the numerator and denominator as
BigInteger
. The BigFraction
is reduced to lowest terms.num
- the numerator, must not be null
.den
- the denominator, must not be null
.ZeroException
- if the denominator is zero.NullArgumentException
- if either of the arguments is nullpublic BigFraction(double value) throws MathIllegalArgumentException
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).
value
- the double value to convert to a fraction.MathIllegalArgumentException
- if value is NaN or infiniteBigFraction(double, double, int)
public BigFraction(double value, double epsilon, int maxIterations) throws FractionConversionException
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.FractionConversionException
- if the continued fraction failed to converge.BigFraction(double)
public BigFraction(double value, int maxDenominator) throws FractionConversionException
References:
value
- the double value to convert to a fraction.maxDenominator
- The maximum allowed value for denominator.FractionConversionException
- if the continued fraction failed to converge.public BigFraction(int num)
Create a BigFraction
equivalent to the passed int
, ie
"num / 1".
num
- the numerator.public BigFraction(int num, int den)
Create a BigFraction
given the numerator and denominator as simple
int
. The BigFraction
is reduced to lowest terms.
num
- the numerator.den
- the denominator.public BigFraction(long num)
Create a BigFraction
equivalent to the passed long, ie "num / 1".
num
- the numerator.public BigFraction(long num, long den)
Create a BigFraction
given the numerator and denominator as simple
long
. The BigFraction
is reduced to lowest terms.
num
- the numerator.den
- the denominator.public static BigFraction getReducedFraction(int numerator, int denominator)
Creates a BigFraction
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'.ArithmeticException
- if the denominator is zero
.public BigFraction abs()
Returns the absolute value of this BigFraction
.
BigFraction
.public BigFraction add(BigInteger bg) throws NullArgumentException
Adds the value of this fraction to the passed BigInteger
,
returning the result in reduced form.
bg
- the BigInteger
to add, must'nt be null
.BigFraction
instance with the resulting values.NullArgumentException
- if the BigInteger
is null
.public BigFraction add(int i)
Adds the value of this fraction to the passed integer
, returning
the result in reduced form.
i
- the integer
to add.BigFraction
instance with the resulting values.public BigFraction add(long l)
Adds the value of this fraction to the passed long
, returning
the result in reduced form.
l
- the long
to add.BigFraction
instance with the resulting values.public BigFraction add(BigFraction fraction)
Adds the value of this fraction to another, returning the result in reduced form.
add
in interface FieldElement<BigFraction>
fraction
- the BigFraction
to add, must not be null
.BigFraction
instance with the resulting values.NullArgumentException
- if the BigFraction
is null
.public BigDecimal bigDecimalValue()
Gets the fraction as a BigDecimal
. This calculates the
fraction as the numerator divided by denominator.
BigDecimal
.ArithmeticException
- if the exact quotient does not have a terminating decimal
expansion.BigDecimal
public BigDecimal bigDecimalValue(int roundingMode)
Gets the fraction as a BigDecimal
following the passed
rounding mode. This calculates the fraction as the numerator divided by
denominator.
roundingMode
- rounding mode to apply. see BigDecimal
constants.BigDecimal
.IllegalArgumentException
- if roundingMode
does not represent a valid rounding
mode.BigDecimal
public BigDecimal bigDecimalValue(int scale, int roundingMode)
Gets the fraction as a BigDecimal
following the passed scale
and rounding mode. This calculates the fraction as the numerator divided
by denominator.
scale
- scale of the BigDecimal
quotient to be returned.
see BigDecimal
for more information.roundingMode
- rounding mode to apply. see BigDecimal
constants.BigDecimal
.BigDecimal
public int compareTo(BigFraction object)
Compares this object to another based on size.
compareTo
in interface Comparable<BigFraction>
object
- the object to compare to, must not be null
.object
, +1 if this is greater
than object
, 0 if they are equal.Comparable.compareTo(java.lang.Object)
public BigFraction divide(BigInteger bg)
Divide the value of this fraction by the passed BigInteger
,
ie this * 1 / bg
, returning the result in reduced form.
bg
- the BigInteger
to divide by, must not be null
BigFraction
instance with the resulting valuesNullArgumentException
- if the BigInteger
is null
MathArithmeticException
- if the fraction to divide by is zeropublic BigFraction divide(int i)
Divide the value of this fraction by the passed int
, ie
this * 1 / i
, returning the result in reduced form.
i
- the int
to divide byBigFraction
instance with the resulting valuesMathArithmeticException
- if the fraction to divide by is zeropublic BigFraction divide(long l)
Divide the value of this fraction by the passed long
, ie
this * 1 / l
, returning the result in reduced form.
l
- the long
to divide byBigFraction
instance with the resulting valuesMathArithmeticException
- if the fraction to divide by is zeropublic BigFraction divide(BigFraction fraction)
Divide the value of this fraction by another, returning the result in reduced form.
divide
in interface FieldElement<BigFraction>
fraction
- Fraction to divide by, must not be null
.BigFraction
instance with the resulting values.NullArgumentException
- if the fraction
is null
.MathArithmeticException
- if the fraction to divide by is zeropublic double doubleValue()
Gets the fraction as a double
. This calculates the fraction as
the numerator divided by denominator.
doubleValue
in class Number
double
Number.doubleValue()
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.
equals
in class Object
other
- fraction to test for equality to this fraction, can be
null
.null
, not an instance of BigFraction
, or not
equal to this fraction instance.Object.equals(java.lang.Object)
public float floatValue()
Gets the fraction as a float
. This calculates the fraction as
the numerator divided by denominator.
floatValue
in class Number
float
.Number.floatValue()
public BigInteger getDenominator()
Access the denominator as a BigInteger
.
BigInteger
.public int getDenominatorAsInt()
Access the denominator as a int
.
int
.public long getDenominatorAsLong()
Access the denominator as a long
.
long
.public BigInteger getNumerator()
Access the numerator as a BigInteger
.
BigInteger
.public int getNumeratorAsInt()
Access the numerator as a int
.
int
.public long getNumeratorAsLong()
Access the numerator as a long
.
long
.public int hashCode()
Gets a hashCode for the fraction.
hashCode
in class Object
Object.hashCode()
public int intValue()
Gets the fraction as an int
. This returns the whole number part
of the fraction.
intValue
in class Number
Number.intValue()
public long longValue()
Gets the fraction as a long
. This returns the whole number part
of the fraction.
longValue
in class Number
Number.longValue()
public BigFraction multiply(BigInteger bg)
Multiplies the value of this fraction by the passed
BigInteger
, returning the result in reduced form.
bg
- the BigInteger
to multiply by.BigFraction
instance with the resulting values.NullArgumentException
- if bg
is null
.public BigFraction multiply(int i)
Multiply the value of this fraction by the passed int
, returning
the result in reduced form.
multiply
in interface FieldElement<BigFraction>
i
- the int
to multiply by.BigFraction
instance with the resulting values.public BigFraction multiply(long l)
Multiply the value of this fraction by the passed long
,
returning the result in reduced form.
l
- the long
to multiply by.BigFraction
instance with the resulting values.public BigFraction multiply(BigFraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
multiply
in interface FieldElement<BigFraction>
fraction
- Fraction to multiply by, must not be null
.BigFraction
instance with the resulting values.NullArgumentException
- if fraction
is null
.public BigFraction negate()
Return the additive inverse of this fraction, returning the result in reduced form.
negate
in interface FieldElement<BigFraction>
public double percentageValue()
Gets the fraction percentage as a double
. This calculates the
fraction as the numerator divided by denominator multiplied by 100.
double
.public BigFraction pow(int exponent)
Returns a BigFraction
whose value is
(this<sup>exponent</sup>)
, returning the result in reduced form.
exponent
- exponent to which this BigFraction
is to be
raised.public BigFraction pow(long exponent)
Returns a BigFraction
whose value is
(thisexponent), returning the result in reduced form.
exponent
- exponent to which this BigFraction
is to be raised.BigFraction
.public BigFraction pow(BigInteger exponent)
Returns a BigFraction
whose value is
(thisexponent), returning the result in reduced form.
exponent
- exponent to which this BigFraction
is to be raised.BigFraction
.public double pow(double exponent)
Returns a double
whose value is
(thisexponent), returning the result in reduced form.
exponent
- exponent to which this BigFraction
is to be raised.public BigFraction reciprocal()
Return the multiplicative inverse of this fraction.
reciprocal
in interface FieldElement<BigFraction>
public BigFraction reduce()
Reduce this BigFraction
to its lowest terms.
BigFraction
. It doesn't change anything if
the fraction can be reduced.public BigFraction subtract(BigInteger bg)
Subtracts the value of an BigInteger
from the value of this
BigFraction
, returning the result in reduced form.
bg
- the BigInteger
to subtract, cannot be null
.BigFraction
instance with the resulting values.NullArgumentException
- if the BigInteger
is null
.public BigFraction subtract(int i)
Subtracts the value of an integer
from the value of this
BigFraction
, returning the result in reduced form.
i
- the integer
to subtract.BigFraction
instance with the resulting values.public BigFraction subtract(long l)
Subtracts the value of a long
from the value of this
BigFraction
, returning the result in reduced form.
l
- the long
to subtract.BigFraction
instance with the resulting values.public BigFraction subtract(BigFraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
subtract
in interface FieldElement<BigFraction>
fraction
- BigFraction
to subtract, must not be null
.BigFraction
instance with the resulting valuesNullArgumentException
- if the fraction
is null
.public String toString()
Returns the String
representing this fraction, ie
"num / dem" or just "num" if the denominator is one.
toString
in class Object
Object.toString()
public BigFractionField getField()
Field
to which the instance belongs.getField
in interface FieldElement<BigFraction>
Field
to which the instance belongsCopyright © 2003–2016 The Apache Software Foundation. All rights reserved.