public final class BigFraction extends Number implements Comparable<BigFraction>, NativeOperators<BigFraction>, Serializable
The number is expressed as the quotient p/q
of two BigInteger
s,
a numerator p
and a non-zero denominator q
.
This class is immutable. Rational number
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 fraction.
|
BigFraction |
add(BigFraction value)
Adds the specified
value to this fraction, returning
the result in reduced form. |
BigFraction |
add(BigInteger value)
Adds the specified
value to this fraction, returning
the result in reduced form. |
BigFraction |
add(int value)
Adds the specified
value to this fraction, returning
the result in reduced form. |
BigFraction |
add(long value)
Adds the specified
value to this fraction, returning
the result in reduced form. |
BigDecimal |
bigDecimalValue()
Returns the
BigDecimal representation of this fraction. |
BigDecimal |
bigDecimalValue(int scale,
RoundingMode roundingMode)
Returns the
BigDecimal representation of this fraction. |
BigDecimal |
bigDecimalValue(RoundingMode roundingMode)
Returns the
BigDecimal representation of this fraction. |
int |
compareTo(BigFraction other)
Compares this object with the specified object for order using the signed magnitude.
|
BigFraction |
divide(BigFraction value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
divide(BigInteger value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
divide(int value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
divide(long value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
double |
doubleValue()
Returns the
double value closest to this fraction. |
boolean |
equals(Object other)
Test for equality with another object.
|
float |
floatValue()
Returns the
float value closest to this fraction. |
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 an
int . |
long |
getDenominatorAsLong()
Access the denominator as a
long . |
BigInteger |
getNumerator()
Access the numerator as a
BigInteger . |
int |
getNumeratorAsInt()
Access the numerator as an
int . |
long |
getNumeratorAsLong()
Access the numerator as a
long . |
int |
hashCode() |
int |
intValue()
Returns the whole number part of the fraction.
|
long |
longValue()
Returns the whole number part of the fraction.
|
BigFraction |
multiply(BigFraction value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
multiply(BigInteger value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
multiply(int value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
multiply(long value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
BigFraction |
negate() |
static BigFraction |
of(BigInteger num)
Create a fraction given the numerator.
|
static BigFraction |
of(BigInteger num,
BigInteger den)
Create a fraction given the numerator and denominator.
|
static BigFraction |
of(int num)
Create a fraction given the numerator.
|
static BigFraction |
of(int num,
int den)
Create a fraction given the numerator and denominator.
|
static BigFraction |
of(long num)
Create a fraction given the numerator.
|
static BigFraction |
of(long num,
long den)
Create a fraction given the numerator and denominator.
|
BigFraction |
one() |
static BigFraction |
parse(String s)
Returns a
BigFraction instance representing the specified string s . |
BigFraction |
pow(int exponent)
Returns a
BigFraction whose value is
thisexponent , returning the result in reduced form. |
BigFraction |
reciprocal() |
int |
signum()
Retrieves the sign of this fraction.
|
BigFraction |
subtract(BigFraction value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
BigFraction |
subtract(BigInteger value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
BigFraction |
subtract(int value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
BigFraction |
subtract(long value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
String |
toString()
Returns the
String representing this fraction. |
BigFraction |
zero() |
byteValue, shortValue
public static final BigFraction ZERO
public static final BigFraction ONE
public static BigFraction from(double value)
This factory method behaves differently to the method
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.IllegalArgumentException
- if the given value
is NaN or infinite;
epsilon
is not positive; or maxIterations < 1
.ArithmeticException
- if the continued fraction failed to converge.public static BigFraction from(double value, int maxDenominator)
References:
Note: The magnitude of the maxDenominator
is used allowing use of
Integer.MIN_VALUE
for a supported maximum denominator of 231.
value
- Value to convert to a fraction.maxDenominator
- Maximum allowed value for denominator.IllegalArgumentException
- if the given value
is NaN or infinite
or maxDenominator
is zero.ArithmeticException
- if the continued fraction failed to converge.public static BigFraction of(int num)
1
.num
- the numerator.public static BigFraction of(long num)
1
.num
- Numerator.public static BigFraction of(BigInteger num)
1
.num
- Numerator.NullPointerException
- if numerator is null.public static BigFraction of(int num, int den)
num
- Numerator.den
- Denominator.ArithmeticException
- if den
is zero.public static BigFraction of(long num, long den)
num
- Numerator.den
- Denominator.ArithmeticException
- if den
is zero.public static BigFraction of(BigInteger num, BigInteger den)
num
- Numerator.den
- Denominator.NullPointerException
- if numerator or denominator are null.ArithmeticException
- if the denominator is zero.public static BigFraction parse(String s)
BigFraction
instance representing the specified string s
.
If s
is null
, then a NullPointerException
is thrown.
The string must be in a format compatible with that produced by
BigFraction.toString()
.
The format expects an integer optionally followed by a '/'
character and
and second integer. Leading and trailing spaces are allowed around each numeric part.
Each numeric part is parsed using BigInteger.BigInteger(String)
. The parts
are interpreted as the numerator and optional denominator of the fraction. If absent
the denominator is assumed to be "1".
Examples of valid strings and the equivalent BigFraction
are shown below:
"0" = BigFraction.of(0) "42" = BigFraction.of(42) "0 / 1" = BigFraction.of(0, 1) "1 / 3" = BigFraction.of(1, 3) "-4 / 13" = BigFraction.of(-4, 13)
Note: The fraction is returned in reduced form and the numerator and denominator
may not match the values in the input string. For this reason the result of
BigFraction.parse(s).toString().equals(s)
may not be true
.
s
- String representation.NullPointerException
- if the string is null.NumberFormatException
- if the string does not contain a parsable fraction.BigInteger.BigInteger(String)
,
toString()
public BigFraction zero()
zero
in interface Addition<BigFraction>
public BigFraction one()
one
in interface Multiplication<BigFraction>
public BigInteger getNumerator()
BigInteger
.BigInteger
.public int getNumeratorAsInt()
int
.int
.public long getNumeratorAsLong()
long
.long
.public BigInteger getDenominator()
BigInteger
.BigInteger
.public int getDenominatorAsInt()
int
.int
.public long getDenominatorAsLong()
long
.long
.public int signum()
public BigFraction abs()
public BigFraction negate()
negate
in interface Addition<BigFraction>
public BigFraction reciprocal()
Raises an exception if the fraction is equal to zero.
reciprocal
in interface Multiplication<BigFraction>
ArithmeticException
- if the current numerator is zero
public double doubleValue()
double
value closest to this fraction.doubleValue
in class Number
double
.public float floatValue()
float
value closest to this fraction.floatValue
in class Number
double
.public int intValue()
public long longValue()
public BigDecimal bigDecimalValue()
BigDecimal
representation of this fraction.
This calculates the fraction as numerator divided by denominator.BigDecimal
.ArithmeticException
- if the exact quotient does not have a terminating decimal
expansion.BigDecimal
public BigDecimal bigDecimalValue(RoundingMode roundingMode)
BigDecimal
representation of this fraction.
This calculates the fraction as numerator divided by denominator
following the passed rounding mode.roundingMode
- Rounding mode to apply.BigDecimal
.BigDecimal
public BigDecimal bigDecimalValue(int scale, RoundingMode roundingMode)
BigDecimal
representation of this fraction.
This calculates the fraction as numerator divided by denominator
following the passed scale and rounding mode.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 BigFraction add(int value)
value
to this fraction, returning
the result in reduced form.value
- Value to add.this + value
.public BigFraction add(long value)
value
to this fraction, returning
the result in reduced form.value
- Value to add.this + value
.public BigFraction add(BigInteger value)
value
to this fraction, returning
the result in reduced form.value
- Value to add.this + value
.public BigFraction add(BigFraction value)
value
to this fraction, returning
the result in reduced form.add
in interface Addition<BigFraction>
value
- Value to add.this + value
.public BigFraction subtract(int value)
value
from this fraction, returning
the result in reduced form.value
- Value to subtract.this - value
.public BigFraction subtract(long value)
value
from this fraction, returning
the result in reduced form.value
- Value to subtract.this - value
.public BigFraction subtract(BigInteger value)
value
from this fraction, returning
the result in reduced form.value
- Value to subtract.this - value
.public BigFraction subtract(BigFraction value)
value
from this fraction, returning
the result in reduced form.subtract
in interface NativeOperators<BigFraction>
value
- Value to subtract.this - value
.public BigFraction multiply(int value)
value
, returning
the result in reduced form.multiply
in interface NativeOperators<BigFraction>
value
- Value to multiply by.this * value
.public BigFraction multiply(long value)
value
, returning
the result in reduced form.value
- Value to multiply by.this * value
.public BigFraction multiply(BigInteger value)
value
, returning
the result in reduced form.value
- Value to multiply by.this * value
.public BigFraction multiply(BigFraction value)
value
, returning
the result in reduced form.multiply
in interface Multiplication<BigFraction>
value
- Value to multiply by.this * value
.public BigFraction divide(int value)
value
, returning
the result in reduced form.value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zeropublic BigFraction divide(long value)
value
, returning
the result in reduced form.value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zeropublic BigFraction divide(BigInteger value)
value
, returning
the result in reduced form.value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zeropublic BigFraction divide(BigFraction value)
value
, returning
the result in reduced form.divide
in interface NativeOperators<BigFraction>
value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zeropublic BigFraction pow(int exponent)
BigFraction
whose value is
thisexponent
, returning the result in reduced form.pow
in interface NativeOperators<BigFraction>
exponent
- exponent to which this BigFraction
is to be raised.thisexponent
.ArithmeticException
- if the intermediate result would overflow.public String toString()
String
representing this fraction.
Uses:
"0"
if numerator
is zero.
"numerator"
if denominator
is one.
"numerator / denominator"
for all other cases.
public int compareTo(BigFraction other)
compareTo
in interface Comparable<BigFraction>
other
- public boolean equals(Object other)
Fraction
then a
comparison is made of the sign and magnitude; otherwise false
is returned.Copyright © 2017–2022 The Apache Software Foundation. All rights reserved.