public final class BigFraction extends Number implements Comparable<BigFraction>, NativeOperators<BigFraction>, Serializable
Modifier and Type | Field and Description |
---|---|
static BigFraction |
ONE
A fraction representing "1".
|
static BigFraction |
ZERO
A fraction representing "0".
|
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 scale,
RoundingMode roundingMode)
Gets the fraction as a
BigDecimal following the passed scale
and rounding mode. |
BigDecimal |
bigDecimalValue(RoundingMode roundingMode)
Gets the fraction as a
BigDecimal following the passed
rounding mode. |
int |
compareTo(BigFraction other)
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 . |
static BigFraction |
from(double value)
Create a fraction given the double value.
|
static BigFraction |
from(double value,
double epsilon,
int maxIterations)
Create a fraction given the double value and maximum error allowed.
|
static BigFraction |
from(double value,
int maxDenominator)
Create a fraction given the double value and maximum denominator.
|
BigInteger |
getDenominator()
Access the denominator as a
BigInteger . |
int |
getDenominatorAsInt()
Access the denominator as a
int . |
long |
getDenominatorAsLong()
Access the denominator as a
long . |
BigInteger |
getNumerator()
Access the numerator as a
BigInteger . |
int |
getNumeratorAsInt()
Access the numerator as a
int . |
long |
getNumeratorAsLong()
Access the numerator as a
long . |
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.
|
static BigFraction |
of(BigInteger num)
Create a
BigFraction equivalent to the passed BigInteger , ie
"num / 1". |
static BigFraction |
of(BigInteger num,
BigInteger den)
Create a
BigFraction given the numerator and denominator as
BigInteger . |
static BigFraction |
of(int num)
Create a
BigFraction equivalent to the passed int , ie
"num / 1". |
static BigFraction |
of(int num,
int den)
Create a
BigFraction given the numerator and denominator as simple
int . |
static BigFraction |
of(long num)
Create a
BigFraction equivalent to the passed long, ie "num / 1". |
static BigFraction |
of(long num,
long den)
Create a
BigFraction given the numerator and denominator as simple
long . |
BigFraction |
one() |
static BigFraction |
parse(String s)
Parses a string that would be produced by
toString()
and instantiates the corresponding object. |
BigFraction |
pow(BigInteger exponent)
Returns a
BigFraction whose value is
\(\mathit{this}^{\mathit{exponent}}\), returning the result in reduced form. |
double |
pow(double exponent)
Returns a
double whose value is
\(\mathit{this}^{\mathit{exponent}}\), 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
\(\mathit{this}^{\mathit{exponent}}\), returning the result in reduced form. |
BigFraction |
reciprocal()
Return the multiplicative inverse of this fraction.
|
int |
signum()
Retrieves the sign of this fraction.
|
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. |
BigFraction |
zero() |
byteValue, shortValue
public static final BigFraction ZERO
public static final BigFraction ONE
public static BigFraction of(BigInteger num)
Create a BigFraction
equivalent to the passed BigInteger
, ie
"num / 1".
num
- the numerator.public static BigFraction of(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
.ArithmeticException
- if the denominator is zero.public static BigFraction from(double value)
This factory method behaves differently from
from(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 from(1.0 / 3.0)
does not create
the fraction \( \frac{1}{3} \), but the fraction \( \frac{6004799503160661}{18014398509481984} \)
because the double number passed to the method is not exactly \( \frac{1}{3} \)
(which cannot be represented exactly in IEEE754).
value
- Value to convert to a fraction.IllegalArgumentException
- if the given value
is NaN or infinite.from(double,double,int)
public static BigFraction from(double value, double epsilon, int maxIterations)
References:
value
- 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.ArithmeticException
- if the continued fraction failed to converge.from(double,int)
public static BigFraction from(double value, int maxDenominator)
References:
value
- Value to convert to a fraction.maxDenominator
- Maximum allowed value for denominator.ArithmeticException
- if the continued fraction failed to converge.from(double,double,int)
public static BigFraction of(int num)
Create a BigFraction
equivalent to the passed int
, ie
"num / 1".
num
- the numerator.public static BigFraction of(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.ArithmeticException
- if den
is zero.public static BigFraction of(long num)
Create a BigFraction
equivalent to the passed long, ie "num / 1".
num
- the numerator.public static BigFraction of(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.ArithmeticException
- if den
is zero.public BigFraction abs()
Returns the absolute value of this BigFraction
.
BigFraction
.public BigFraction add(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.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 Addition<BigFraction>
fraction
- the BigFraction
to add, must not be null
.BigFraction
instance with the resulting values.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(RoundingMode 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.BigDecimal
.BigDecimal
public BigDecimal bigDecimalValue(int scale, RoundingMode 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.BigDecimal
.ArithmeticException
- if roundingMode
== RoundingMode.UNNECESSARY
and
the specified scale is insufficient to represent the result of the division exactly.BigDecimal
public int compareTo(BigFraction other)
Compares this object to another based on size.
compareTo
in interface Comparable<BigFraction>
other
- Object to compare to, must not be null
.object
, +1 if this is greater
than object
, 0 if they are equal.Comparable.compareTo(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 valuesArithmeticException
- if the value 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 valuesArithmeticException
- if the value 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 valuesArithmeticException
- if the value 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 NativeOperators<BigFraction>
fraction
- Fraction to divide by, must not be null
.BigFraction
instance with the resulting values.ArithmeticException
- 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.public BigFraction multiply(int i)
Multiply the value of this fraction by the passed int
, returning
the result in reduced form.
multiply
in interface NativeOperators<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 Multiplication<BigFraction>
fraction
- Fraction to multiply by, must not be null
.BigFraction
instance with the resulting values.public int signum()
public BigFraction negate()
Return the additive inverse of this fraction, returning the result in reduced form.
negate
in interface Addition<BigFraction>
public BigFraction pow(int exponent)
Returns a BigFraction
whose value is
(this<sup>exponent</sup>)
, returning the result in reduced form.
pow
in interface NativeOperators<BigFraction>
exponent
- exponent to which this BigFraction
is to be
raised.public BigFraction pow(long exponent)
Returns a BigFraction
whose value is
\(\mathit{this}^{\mathit{exponent}}\), 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
\(\mathit{this}^{\mathit{exponent}}\), 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
\(\mathit{this}^{\mathit{exponent}}\), 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 Multiplication<BigFraction>
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.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 NativeOperators<BigFraction>
fraction
- BigFraction
to subtract, must not be null
.BigFraction
instance with the resulting valuespublic 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 BigFraction zero()
zero
in interface Addition<BigFraction>
public BigFraction one()
one
in interface Multiplication<BigFraction>
public static BigFraction parse(String s)
toString()
and instantiates the corresponding object.s
- String representation.NumberFormatException
- if the string does not conform
to the specification.Copyright © 2017–2020 The Apache Software Foundation. All rights reserved.