public class BigFraction extends java.lang.Number implements FieldElement<BigFraction>, java.lang.Comparable<BigFraction>, java.io.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(java.math.BigInteger num)
Create a
BigFraction equivalent to the passed BigInteger, ie
"num / 1". |
BigFraction(java.math.BigInteger num,
java.math.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(java.math.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.
|
java.math.BigDecimal |
bigDecimalValue()
Gets the fraction as a
BigDecimal . |
java.math.BigDecimal |
bigDecimalValue(int roundingMode)
Gets the fraction as a
BigDecimal following the passed
rounding mode. |
java.math.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(java.math.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(java.lang.Object other)
Test for the equality of two fractions.
|
float |
floatValue()
Gets the fraction as a float.
|
java.math.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. |
java.math.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(java.math.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(java.math.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 integer whose value is
(thisexponent), 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(java.math.BigInteger bg)
Subtracts the value of an
BigInteger from the value of this one,
returning the result in reduced form. |
BigFraction |
subtract(int i)
Subtracts the value of an integer from the value of this one,
returning the result in reduced form.
|
BigFraction |
subtract(long l)
Subtracts the value of an integer from the value of this one,
returning the result in reduced form.
|
java.lang.String |
toString()
Returns the
String representing this fraction, ie
"num / dem" or just "num" if the denominator is one. |
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(java.math.BigInteger num)
Create a BigFraction
equivalent to the passed BigInteger, ie
"num / 1".
num
- the numerator.public BigFraction(java.math.BigInteger num, java.math.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
..java.lang.ArithmeticException
- if the denominator is zero.public BigFraction(double value) throws java.lang.IllegalArgumentException
This constructor behaves differently from
BigFraction(double, double, int)
. It converts the
double value exactly, considering its internal bits representation.
This does work 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.java.lang.IllegalArgumentException
- 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'.java.lang.ArithmeticException
- if the denominator is zero
.public BigFraction abs()
Returns the absolute value of this BigFraction
.
BigFraction
.public BigFraction add(java.math.BigInteger bg)
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.java.lang.NullPointerException
- 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.java.lang.NullPointerException
- if the BigFraction
is null
.public java.math.BigDecimal bigDecimalValue()
Gets the fraction as a BigDecimal
. This calculates the
fraction as the numerator divided by denominator.
BigDecimal
.java.lang.ArithmeticException
- if the exact quotient does not have a terminating decimal
expansion.BigDecimal
public java.math.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
.java.lang.IllegalArgumentException
- if roundingMode does not represent a valid rounding
mode.BigDecimal
public java.math.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 java.lang.Comparable<BigFraction>
object
- the object to compare to, must not be null
.Comparable.compareTo(java.lang.Object)
public BigFraction divide(java.math.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 values.java.lang.NullPointerException
- if the BigInteger
is null
.java.lang.ArithmeticException
- if the fraction to divide by is zero.public 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 by.BigFraction
instance with the resulting values.java.lang.ArithmeticException
- if the fraction to divide by is zero.public 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 by.BigFraction
instance with the resulting values.java.lang.ArithmeticException
- if the fraction to divide by is zero.public 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.java.lang.NullPointerException
- if the fraction
is null
.java.lang.ArithmeticException
- if the fraction to divide by is zero.public double doubleValue()
Gets the fraction as a double. This calculates the fraction as the numerator divided by denominator.
doubleValue
in class java.lang.Number
Number.doubleValue()
public boolean equals(java.lang.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 java.lang.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 java.lang.Number
Number.floatValue()
public java.math.BigInteger getDenominator()
Access the denominator as a BigInteger
.
BigInteger
.public int getDenominatorAsInt()
Access the denominator as a int.
public long getDenominatorAsLong()
Access the denominator as a long.
public java.math.BigInteger getNumerator()
Access the numerator as a BigInteger
.
BigInteger
.public int getNumeratorAsInt()
Access the numerator as a int.
public long getNumeratorAsLong()
Access the numerator as a long.
public int hashCode()
Gets a hashCode for the fraction.
hashCode
in class java.lang.Object
Object.hashCode()
public int intValue()
Gets the fraction as an int. This returns the whole number part of the fraction.
intValue
in class java.lang.Number
Number.intValue()
public long longValue()
Gets the fraction as a long. This returns the whole number part of the fraction.
longValue
in class java.lang.Number
Number.longValue()
public BigFraction multiply(java.math.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.java.lang.NullPointerException
- 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.
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.java.lang.NullPointerException
- if fraction
is null
.public BigFraction negate()
Return the additive inverse of this fraction, returning the result in reduced form.
public double percentageValue()
Gets the fraction percentage as a double. This calculates the fraction as the numerator divided by denominator multiplied by 100.
public BigFraction pow(int exponent)
Returns a integer whose value is (thisexponent), returning the result in reduced form.
exponent
- exponent to which this BigInteger
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(java.math.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.
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(java.math.BigInteger bg)
Subtracts the value of an BigInteger
from the value of this one,
returning the result in reduced form.
bg
- the BigInteger
to subtract, cannot be null
.BigFraction
instance with the resulting values.java.lang.NullPointerException
- if the BigInteger
is null
.public BigFraction subtract(int i)
Subtracts the value of an integer from the value of this one, 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 an integer from the value of this one, returning the result in reduced form.
l
- the long to subtract.BigFraction
instance with the resulting values, or
this object if the long is zero.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 valuesjava.lang.NullPointerException
- if the fraction
is null
.public java.lang.String toString()
Returns the String
representing this fraction, ie
"num / dem" or just "num" if the denominator is one.
toString
in class java.lang.Object
Object.toString()
public BigFractionField getField()
Field
to which the instance belongs.getField
in interface FieldElement<BigFraction>
Field
to which the instance belongsCopyright © 2010 - 2020 Adobe. All Rights Reserved