public class BigRational extends java.lang.Object implements java.lang.Comparable<BigRational>
Basic calculations with rational numbers (+ - * /) have no loss of precision.
This allows to use BigRational
as a replacement for BigDecimal
if absolute accuracy is desired.
The values are internally stored as BigDecimal
(for performance optimizations) but represented
as BigInteger
(for mathematical correctness)
when accessed with getNumeratorBigInteger()
and getDenominatorBigInteger()
.
The following basic calculations have no loss of precision:
The following calculations are special cases of the ones listed above and have no loss of precision:
Any BigRational
value can be converted into an arbitrary precision
(number of significant digits)
or scale
(number of digits after the decimal point).
Modifier and Type | Field and Description |
---|---|
static BigRational |
ONE
The value 1 as
BigRational . |
static BigRational |
TEN
The value 10 as
BigRational . |
static BigRational |
TWO
The value 2 as
BigRational . |
static BigRational |
ZERO
The value 0 as
BigRational . |
Modifier and Type | Method and Description |
---|---|
BigRational |
abs()
Returns the absolute value of this rational number.
|
BigRational |
add(java.math.BigInteger value)
Calculates the addition (+) of this rational number and the specified argument.
|
BigRational |
add(BigRational value)
Calculates the addition (+) of this rational number and the specified argument.
|
BigRational |
add(int value)
Calculates the addition (+) of this rational number and the specified argument.
|
static BigRational |
bernoulli(int n)
Calculates the Bernoulli number for the specified index.
|
int |
compareTo(BigRational other) |
BigRational |
decrement()
Calculates the decrement of this rational number (- 1).
|
BigRational |
divide(java.math.BigInteger value)
Calculates the division (/) of this rational number and the specified argument.
|
BigRational |
divide(BigRational value)
Calculates the division (/) of this rational number and the specified argument.
|
BigRational |
divide(int value)
Calculates the division (/) of this rational number and the specified argument.
|
boolean |
equals(java.lang.Object obj) |
BigRational |
fractionPart()
Returns the fraction part of this rational number.
|
java.math.BigDecimal |
getDenominator()
Returns the denominator of this rational number as BigDecimal.
|
java.math.BigInteger |
getDenominatorBigInteger()
Returns the denominator of this rational number as BigInteger.
|
java.math.BigDecimal |
getNumerator()
Returns the numerator of this rational number as BigDecimal.
|
java.math.BigInteger |
getNumeratorBigInteger()
Returns the numerator of this rational number as BigInteger.
|
int |
hashCode() |
BigRational |
increment()
Calculates the increment of this rational number (+ 1).
|
BigRational |
integerPart()
Returns the integer part of this rational number.
|
boolean |
isInteger()
Returns whether this rational number is an integer number without fraction part.
|
boolean |
isZero()
Returns whether this rational number is zero.
|
static BigRational |
max(BigRational... values)
Returns the largest of the specified rational numbers.
|
static BigRational |
min(BigRational... values)
Returns the smallest of the specified rational numbers.
|
BigRational |
multiply(java.math.BigInteger value)
Calculates the multiplication (*) of this rational number and the specified argument.
|
BigRational |
multiply(BigRational value)
Calculates the multiplication (*) of this rational number and the specified argument.
|
BigRational |
multiply(int value)
Calculates the multiplication (*) of this rational number and the specified argument.
|
BigRational |
negate()
Negates this rational number (inverting the sign).
|
BigRational |
pow(int exponent)
Calculates this rational number to the power (xy) of the specified argument.
|
BigRational |
reciprocal()
Calculates the reciprocal of this rational number (1/x).
|
BigRational |
reduce()
Reduces this rational number to the smallest numerator/denominator with the same value.
|
int |
signum()
Returns the signum function of this rational number.
|
BigRational |
subtract(java.math.BigInteger value)
Calculates the subtraction (-) of this rational number and the specified argument.
|
BigRational |
subtract(BigRational value)
Calculates the subtraction (-) of this rational number and the specified argument.
|
BigRational |
subtract(int value)
Calculates the subtraction (-) of this rational number and the specified argument.
|
java.math.BigDecimal |
toBigDecimal()
Returns this rational number as a
BigDecimal . |
java.math.BigDecimal |
toBigDecimal(java.math.MathContext mc)
Returns this rational number as a
BigDecimal with the precision specified by the MathContext . |
double |
toDouble()
Returns this rational number as a double value.
|
float |
toFloat()
Returns this rational number as a float value.
|
java.lang.String |
toIntegerRationalString()
Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator".
|
java.lang.String |
toPlainString()
Returns a plain string representation of this rational number without any exponent.
|
java.lang.String |
toRationalString()
Returns the string representation of this rational number in the form "numerator/denominator".
|
java.lang.String |
toString() |
static BigRational |
valueOf(java.math.BigDecimal value)
Creates a rational number of the specified
BigDecimal value. |
static BigRational |
valueOf(java.math.BigDecimal numerator,
java.math.BigDecimal denominator)
Creates a rational number of the specified numerator/denominator BigDecimal values.
|
static BigRational |
valueOf(java.math.BigInteger value)
Creates a rational number of the specified
BigInteger value. |
static BigRational |
valueOf(java.math.BigInteger numerator,
java.math.BigInteger denominator)
Creates a rational number of the specified numerator/denominator BigInteger values.
|
static BigRational |
valueOf(boolean positive,
java.lang.String integerPart,
java.lang.String fractionPart,
java.lang.String fractionRepeatPart,
java.lang.String exponentPart) |
static BigRational |
valueOf(double value)
Creates a rational number of the specified double value.
|
static BigRational |
valueOf(int value)
Creates a rational number of the specified int value.
|
static BigRational |
valueOf(int numerator,
int denominator)
Creates a rational number of the specified numerator/denominator int values.
|
static BigRational |
valueOf(int integer,
int fractionNumerator,
int fractionDenominator)
Creates a rational number of the specified integer and fraction parts.
|
static BigRational |
valueOf(java.lang.String string)
Creates a rational number of the specified string representation.
|
BigRational |
withPrecision(int precision)
Returns a rational number with approximatively
this value and the specified precision. |
BigRational |
withScale(int scale)
Returns a rational number with approximatively
this value and the specified scale. |
public static final BigRational ZERO
BigRational
.public static final BigRational ONE
BigRational
.public static final BigRational TWO
BigRational
.public static final BigRational TEN
BigRational
.public java.math.BigInteger getNumeratorBigInteger()
public java.math.BigDecimal getNumerator()
public java.math.BigInteger getDenominatorBigInteger()
Guaranteed to not be 0.
Guaranteed to be positive.
public java.math.BigDecimal getDenominator()
Guaranteed to not be 0.
Guaranteed to be positive.
public BigRational reduce()
public BigRational integerPart()
Examples:
BigRational.valueOf(3.5).integerPart()
returns BigRational.valueOf(3)
public BigRational fractionPart()
Examples:
BigRational.valueOf(3.5).integerPart()
returns BigRational.valueOf(0.5)
public BigRational negate()
The result has no loss of precision.
Examples:
BigRational.valueOf(3.5).negate()
returns BigRational.valueOf(-3.5)
public BigRational reciprocal()
The result has no loss of precision.
Examples:
BigRational.valueOf(0.5).reciprocal()
returns BigRational.valueOf(2)
BigRational.valueOf(-2).reciprocal()
returns BigRational.valueOf(-0.5)
java.lang.ArithmeticException
- if this number is 0 (division by zero)public BigRational abs()
The result has no loss of precision.
Examples:
BigRational.valueOf(-2).abs()
returns BigRational.valueOf(2)
BigRational.valueOf(2).abs()
returns BigRational.valueOf(2)
public int signum()
public BigRational increment()
This is functionally identical to
this.add(BigRational.ONE)
but slightly faster.
The result has no loss of precision.
public BigRational decrement()
This is functionally identical to
this.subtract(BigRational.ONE)
but slightly faster.
The result has no loss of precision.
public BigRational add(BigRational value)
The result has no loss of precision.
value
- the rational number to addpublic BigRational add(java.math.BigInteger value)
This is functionally identical to
this.add(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the BigInteger
to addpublic BigRational add(int value)
This is functionally identical to
this.add(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the int value to addpublic BigRational subtract(BigRational value)
The result has no loss of precision.
value
- the rational number to subtractpublic BigRational subtract(java.math.BigInteger value)
This is functionally identical to
this.subtract(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the BigInteger
to subtractpublic BigRational subtract(int value)
This is functionally identical to
this.subtract(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the int value to subtractpublic BigRational multiply(BigRational value)
The result has no loss of precision.
value
- the rational number to multiplypublic BigRational multiply(java.math.BigInteger value)
This is functionally identical to
this.multiply(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the BigInteger
to multiplypublic BigRational multiply(int value)
This is functionally identical to
this.multiply(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the int value to multiplypublic BigRational divide(BigRational value)
The result has no loss of precision.
value
- the rational number to divide (0 is not allowed)java.lang.ArithmeticException
- if the argument is 0 (division by zero)public BigRational divide(java.math.BigInteger value)
This is functionally identical to
this.divide(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the BigInteger
to divide (0 is not allowed)java.lang.ArithmeticException
- if the argument is 0 (division by zero)public BigRational divide(int value)
This is functionally identical to
this.divide(BigRational.valueOf(value))
but slightly faster.
The result has no loss of precision.
value
- the int value to divide (0 is not allowed)java.lang.ArithmeticException
- if the argument is 0 (division by zero)public boolean isZero()
true
if this rational number is zero (0), false
if it is not zeropublic boolean isInteger()
true
if this rational number is an integer number, false
if it has a fraction partpublic BigRational pow(int exponent)
The result has no loss of precision.
exponent
- exponent to which this rational number is to be raisedpublic BigRational withPrecision(int precision)
this
value and the specified precision.precision
- the precision (number of significant digits) of the calculated result, or 0 for unlimited precisionpublic BigRational withScale(int scale)
this
value and the specified scale.scale
- the scale (number of digits after the decimal point) of the calculated resultpublic double toDouble()
public float toFloat()
public java.math.BigDecimal toBigDecimal()
BigDecimal
.BigDecimal
valuepublic java.math.BigDecimal toBigDecimal(java.math.MathContext mc)
BigDecimal
with the precision specified by the MathContext
.mc
- the MathContext
specifying the precision of the calculated resultBigDecimal
public int compareTo(BigRational other)
compareTo
in interface java.lang.Comparable<BigRational>
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toPlainString()
BigDecimal.toPlainString()
public java.lang.String toRationalString()
The resulting string is a valid input of the valueOf(String)
method.
Examples:
BigRational.valueOf(0.5).toRationalString()
returns "1/2"
BigRational.valueOf(2).toRationalString()
returns "2"
BigRational.valueOf(4, 4).toRationalString()
returns "4/4"
(not reduced)valueOf(String)
,
valueOf(int, int)
public java.lang.String toIntegerRationalString()
The integer part is omitted if it is 0 (when this absolute rational number is smaller than 1).
The fraction part is omitted it it is 0 (when this rational number is an integer).
If this rational number is 0, then "0" is returned.
Example: BigRational.valueOf(3.5).toIntegerRationalString()
returns "3 1/2"
.
valueOf(int, int, int)
public static BigRational valueOf(int value)
value
- the int valuepublic static BigRational valueOf(int numerator, int denominator)
numerator
- the numerator int valuedenominator
- the denominator int value (0 not allowed)java.lang.ArithmeticException
- if the denominator is 0 (division by zero)public static BigRational valueOf(int integer, int fractionNumerator, int fractionDenominator)
Useful to create numbers like 3 1/2 (= three and a half = 3.5) by calling
BigRational.valueOf(3, 1, 2)
.
To create a negative rational only the integer part argument is allowed to be negative:
to create -3 1/2 (= minus three and a half = -3.5) call BigRational.valueOf(-3, 1, 2)
.
integer
- the integer part int valuefractionNumerator
- the fraction part numerator int value (negative not allowed)fractionDenominator
- the fraction part denominator int value (0 or negative not allowed)java.lang.ArithmeticException
- if the fraction part denominator is 0 (division by zero),
or if the fraction part numerator or denominator is negativepublic static BigRational valueOf(java.math.BigInteger numerator, java.math.BigInteger denominator)
numerator
- the numerator BigInteger
valuedenominator
- the denominator BigInteger
value (0 not allowed)java.lang.ArithmeticException
- if the denominator is 0 (division by zero)public static BigRational valueOf(java.math.BigInteger value)
BigInteger
value.value
- the BigInteger
valuepublic static BigRational valueOf(double value)
value
- the double valuejava.lang.NumberFormatException
- if the double value is Infinite or NaN.public static BigRational valueOf(java.math.BigDecimal value)
BigDecimal
value.value
- the double valuepublic static BigRational valueOf(java.lang.String string)
The accepted string representations are:
toString()
: "integerPart.fractionPart"toRationalString()
: "numerator/denominator"toString()
of BigDecimal
, BigInteger
, Integer
, ...toString()
of Double
, Float
- except "Infinity", "-Infinity" and "NaN"string
- the string representation to convertjava.lang.ArithmeticException
- if the denominator is 0 (division by zero)public static BigRational valueOf(boolean positive, java.lang.String integerPart, java.lang.String fractionPart, java.lang.String fractionRepeatPart, java.lang.String exponentPart)
public static BigRational valueOf(java.math.BigDecimal numerator, java.math.BigDecimal denominator)
numerator
- the numerator BigDecimal
valuedenominator
- the denominator BigDecimal
value (0 not allowed)java.lang.ArithmeticException
- if the denominator is 0 (division by zero)public static BigRational min(BigRational... values)
values
- the rational numbers to comparepublic static BigRational max(BigRational... values)
values
- the rational numbers to comparemax(BigRational)
public static BigRational bernoulli(int n)
This function calculates the first Bernoulli numbers and therefore bernoulli(1)
returns -0.5
Note that bernoulli(x)
for all odd x > 1 returns 0
n
- the index of the Bernoulli number to be calculated (starting at 0)java.lang.ArithmeticException
- if x is lesser than 0