Class BigRational
- All Implemented Interfaces:
Serializable,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).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static List<BigRational> private final BigDecimalprivate final BigDecimalstatic final BigRationalThe value 1 asBigRational.static final BigRationalThe value 10 asBigRational.static final BigRationalThe value 2 asBigRational.static final BigRationalThe value 0 asBigRational. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateBigRational(int value) privateBigRational(BigDecimal num, BigDecimal denom) -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns the absolute value of this rational number.add(int value) Calculates the addition (+) of this rational number and the specified argument.add(BigRational value) Calculates the addition (+) of this rational number and the specified argument.private BigRationaladd(BigDecimal value) add(BigInteger value) Calculates the addition (+) of this rational number and the specified argument.static BigRationalbernoulli(int n) Calculates the Bernoulli number for the specified index.private static BigRationalcalculateBernoulli(int n) intcompareTo(BigRational other) private static intcountDigits(BigInteger number) Calculates the decrement of this rational number (- 1).divide(int value) Calculates the division (/) of this rational number and the specified argument.divide(BigRational value) Calculates the division (/) of this rational number and the specified argument.private BigRationaldivide(BigDecimal value) divide(BigInteger value) Calculates the division (/) of this rational number and the specified argument.doublebooleanfloatReturns the fraction part of this rational number.Returns the denominator of this rational number as BigDecimal.Returns the denominator of this rational number as BigInteger.Returns the numerator of this rational number as BigDecimal.Returns the numerator of this rational number as BigInteger.inthashCode()Calculates the increment of this rational number (+ 1).Returns the integer part of this rational number.intintValue()booleanReturns whether this rational number is an integer number without fraction part.private booleanReturns whether this rational number is an integer number without fraction part.private booleanbooleanisZero()Returns whether this rational number is zero.longprivate BigRationalmax(BigRational value) Finds the maximum (larger) of two rational numbers.static BigRationalmax(BigRational... values) Returns the largest of the specified rational numbers.private BigRationalmin(BigRational value) Finds the minimum (smaller) of two rational numbers.static BigRationalmin(BigRational... values) Returns the smallest of the specified rational numbers.multiply(int value) Calculates the multiplication (*) of this rational number and the specified argument.multiply(BigRational value) Calculates the multiplication (*) of this rational number and the specified argument.private BigRationalmultiply(BigDecimal value) multiply(BigInteger value) Calculates the multiplication (*) of this rational number and the specified argument.negate()Negates this rational number (inverting the sign).private static BigRationalof(BigDecimal numerator, BigDecimal denominator) pow(int exponent) Calculates this rational number to the power (xy) of the specified argument.private intCalculates the reciprocal of this rational number (1/x).reduce()Reduces this rational number to the smallest numerator/denominator with the same value.intsignum()Returns the signum function of this rational number.subtract(int value) Calculates the subtraction (-) of this rational number and the specified argument.subtract(BigRational value) Calculates the subtraction (-) of this rational number and the specified argument.private BigRationalsubtract(BigDecimal value) subtract(BigInteger value) Calculates the subtraction (-) of this rational number and the specified argument.Returns this rational number as aBigDecimal.Returns this rational number as aBigDecimalwith the precision specified by theMathContext.doubletoDouble()Returns this rational number as a double value.floattoFloat()Returns this rational number as a float value.Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator".Returns a plain string representation of this rational number without any exponent.Returns the string representation of this rational number in the form "numerator/denominator".toString()static BigRationalvalueOf(boolean positive, String integerPart, String fractionPart, String fractionRepeatPart, String exponentPart) static BigRationalvalueOf(double value) Creates a rational number of the specified double value.static BigRationalvalueOf(int value) Creates a rational number of the specified int value.static BigRationalvalueOf(int numerator, int denominator) Creates a rational number of the specified numerator/denominator int values.static BigRationalvalueOf(int integer, int fractionNumerator, int fractionDenominator) Creates a rational number of the specified integer and fraction parts.static BigRationalCreates a rational number of the specified string representation.static BigRationalvalueOf(BigDecimal value) Creates a rational number of the specifiedBigDecimalvalue.static BigRationalvalueOf(BigDecimal numerator, BigDecimal denominator) Creates a rational number of the specified numerator/denominator BigDecimal values.static BigRationalvalueOf(BigInteger value) Creates a rational number of the specifiedBigIntegervalue.static BigRationalvalueOf(BigInteger numerator, BigInteger denominator) Creates a rational number of the specified numerator/denominator BigInteger values.private static BigRationalvalueOfSimple(String string) withPrecision(int precision) Returns a rational number with approximativelythisvalue and the specified precision.withScale(int scale) Returns a rational number with approximativelythisvalue and the specified scale.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
The value 0 asBigRational. -
ONE
The value 1 asBigRational. -
TWO
The value 2 asBigRational. -
TEN
The value 10 asBigRational. -
numerator
-
denominator
-
bernoulliCache
-
-
Constructor Details
-
BigRational
private BigRational(int value) -
BigRational
-
-
Method Details
-
getNumeratorBigInteger
Returns the numerator of this rational number as BigInteger.- Returns:
- the numerator as BigInteger
-
getNumerator
Returns the numerator of this rational number as BigDecimal.- Returns:
- the numerator as BigDecimal
-
getDenominatorBigInteger
Returns the denominator of this rational number as BigInteger.Guaranteed to not be 0.
Guaranteed to be positive.
- Returns:
- the denominator as BigInteger
-
getDenominator
Returns the denominator of this rational number as BigDecimal.Guaranteed to not be 0.
Guaranteed to be positive.
- Returns:
- the denominator as BigDecimal
-
reduce
Reduces this rational number to the smallest numerator/denominator with the same value.- Returns:
- the reduced rational number
-
integerPart
Returns the integer part of this rational number.Examples:
BigRational.valueOf(3.5).integerPart()returnsBigRational.valueOf(3)
- Returns:
- the integer part of this rational number
-
fractionPart
Returns the fraction part of this rational number.Examples:
BigRational.valueOf(3.5).integerPart()returnsBigRational.valueOf(0.5)
- Returns:
- the fraction part of this rational number
-
negate
Negates this rational number (inverting the sign).The result has no loss of precision.
Examples:
BigRational.valueOf(3.5).negate()returnsBigRational.valueOf(-3.5)
- Returns:
- the negated rational number
-
reciprocal
Calculates the reciprocal of this rational number (1/x).The result has no loss of precision.
Examples:
BigRational.valueOf(0.5).reciprocal()returnsBigRational.valueOf(2)BigRational.valueOf(-2).reciprocal()returnsBigRational.valueOf(-0.5)
- Returns:
- the reciprocal rational number
- Throws:
ArithmeticException- if this number is 0 (division by zero)
-
abs
Returns the absolute value of this rational number.The result has no loss of precision.
Examples:
BigRational.valueOf(-2).abs()returnsBigRational.valueOf(2)BigRational.valueOf(2).abs()returnsBigRational.valueOf(2)
- Returns:
- the absolute rational number (positive, or 0 if this rational is 0)
-
signum
public int signum()Returns the signum function of this rational number.- Returns:
- -1, 0 or 1 as the value of this rational number is negative, zero or positive.
-
increment
Calculates the increment of this rational number (+ 1).This is functionally identical to
this.add(BigRational.ONE)but slightly faster.The result has no loss of precision.
- Returns:
- the incremented rational number
-
decrement
Calculates the decrement of this rational number (- 1).This is functionally identical to
this.subtract(BigRational.ONE)but slightly faster.The result has no loss of precision.
- Returns:
- the decremented rational number
-
add
Calculates the addition (+) of this rational number and the specified argument.The result has no loss of precision.
- Parameters:
value- the rational number to add- Returns:
- the resulting rational number
-
add
-
add
Calculates the addition (+) of this rational number and the specified argument.This is functionally identical to
this.add(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- theBigIntegerto add- Returns:
- the resulting rational number
-
add
Calculates the addition (+) of this rational number and the specified argument.This is functionally identical to
this.add(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- the int value to add- Returns:
- the resulting rational number
-
subtract
Calculates the subtraction (-) of this rational number and the specified argument.The result has no loss of precision.
- Parameters:
value- the rational number to subtract- Returns:
- the resulting rational number
-
subtract
-
subtract
Calculates the subtraction (-) of this rational number and the specified argument.This is functionally identical to
this.subtract(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- theBigIntegerto subtract- Returns:
- the resulting rational number
-
subtract
Calculates the subtraction (-) of this rational number and the specified argument.This is functionally identical to
this.subtract(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- the int value to subtract- Returns:
- the resulting rational number
-
multiply
Calculates the multiplication (*) of this rational number and the specified argument.The result has no loss of precision.
- Parameters:
value- the rational number to multiply- Returns:
- the resulting rational number
-
multiply
-
multiply
Calculates the multiplication (*) of this rational number and the specified argument.This is functionally identical to
this.multiply(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- theBigIntegerto multiply- Returns:
- the resulting rational number
-
multiply
Calculates the multiplication (*) of this rational number and the specified argument.This is functionally identical to
this.multiply(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- the int value to multiply- Returns:
- the resulting rational number
-
divide
Calculates the division (/) of this rational number and the specified argument.The result has no loss of precision.
- Parameters:
value- the rational number to divide (0 is not allowed)- Returns:
- the resulting rational number
- Throws:
ArithmeticException- if the argument is 0 (division by zero)
-
divide
-
divide
Calculates the division (/) of this rational number and the specified argument.This is functionally identical to
this.divide(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- theBigIntegerto divide (0 is not allowed)- Returns:
- the resulting rational number
- Throws:
ArithmeticException- if the argument is 0 (division by zero)
-
divide
Calculates the division (/) of this rational number and the specified argument.This is functionally identical to
this.divide(BigRational.valueOf(value))but slightly faster.The result has no loss of precision.
- Parameters:
value- the int value to divide (0 is not allowed)- Returns:
- the resulting rational number
- Throws:
ArithmeticException- if the argument is 0 (division by zero)
-
isZero
public boolean isZero()Returns whether this rational number is zero.- Returns:
trueif this rational number is zero (0),falseif it is not zero
-
isPositive
private boolean isPositive() -
isInteger
public boolean isInteger()Returns whether this rational number is an integer number without fraction part.- Returns:
trueif this rational number is an integer number,falseif it has a fraction part
-
isIntegerInternal
private boolean isIntegerInternal()Returns whether this rational number is an integer number without fraction part.Will return
falseif this number is not reduced to the integer representation yet (e.g. 4/4 or 4/2)- Returns:
trueif this rational number is an integer number,falseif it has a fraction part- See Also:
-
pow
Calculates this rational number to the power (xy) of the specified argument.The result has no loss of precision.
- Parameters:
exponent- exponent to which this rational number is to be raised- Returns:
- the resulting rational number
-
min
Finds the minimum (smaller) of two rational numbers.- Parameters:
value- the rational number to compare with- Returns:
- the minimum rational number, either
thisor the argumentvalue
-
max
Finds the maximum (larger) of two rational numbers.- Parameters:
value- the rational number to compare with- Returns:
- the minimum rational number, either
thisor the argumentvalue
-
withPrecision
Returns a rational number with approximativelythisvalue and the specified precision.- Parameters:
precision- the precision (number of significant digits) of the calculated result, or 0 for unlimited precision- Returns:
- the calculated rational number with the specified precision
-
withScale
Returns a rational number with approximativelythisvalue and the specified scale.- Parameters:
scale- the scale (number of digits after the decimal point) of the calculated result- Returns:
- the calculated rational number with the specified scale
-
countDigits
-
precision
private int precision() -
toDouble
public double toDouble()Returns this rational number as a double value.If the rational number cannot be represented as double then one of the following results will be returned:
- >
Double.MAX_VALUEreturnsDouble.POSITIVE_INFINITY - <
-Double.MAX_VALUEreturnsDouble.NEGATIVE_INFINITY - <
Double.MIN_VALUEreturns+0.0 - >
-Double.MIN_VALUEreturns-0.0
- Returns:
- the double value
- >
-
toFloat
public float toFloat()Returns this rational number as a float value.If the rational number cannot be represented as float then one of the following results will be returned:
- >
Float.MAX_VALUEreturnsFloat.POSITIVE_INFINITY - <
-Float.MAX_VALUEreturnsFloat.NEGATIVE_INFINITY - <
Float.MIN_VALUEreturns+0.0f - >
-Float.MIN_VALUEreturns-0.0f
- Returns:
- the float value
- >
-
toBigDecimal
Returns this rational number as aBigDecimal.- Returns:
- the
BigDecimalvalue
-
toBigDecimal
Returns this rational number as aBigDecimalwith the precision specified by theMathContext.- Parameters:
mc- theMathContextspecifying the precision of the calculated result- Returns:
- the
BigDecimal
-
compareTo
- Specified by:
compareToin interfaceComparable<BigRational>
-
hashCode
public int hashCode() -
equals
-
toString
-
toPlainString
Returns a plain string representation of this rational number without any exponent.- Returns:
- the plain string representation
- See Also:
-
toRationalString
Returns the string representation of this rational number in the form "numerator/denominator".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)
- Returns:
- the rational number string representation in the form "numerator/denominator", or "0" if the rational number is 0.
- See Also:
-
toIntegerRationalString
Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator".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".- Returns:
- the integer and fraction rational string representation
- See Also:
-
valueOf
Creates a rational number of the specified int value.- Parameters:
value- the int value- Returns:
- the rational number
-
valueOf
Creates a rational number of the specified numerator/denominator int values.- Parameters:
numerator- the numerator int valuedenominator- the denominator int value (0 not allowed)- Returns:
- the rational number
- Throws:
ArithmeticException- if the denominator is 0 (division by zero)
-
valueOf
Creates a rational number of the specified integer and fraction parts.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).- Parameters:
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)- Returns:
- the rational number
- Throws:
ArithmeticException- if the fraction part denominator is 0 (division by zero), or if the fraction part numerator or denominator is negative
-
valueOf
Creates a rational number of the specified numerator/denominator BigInteger values.- Parameters:
numerator- the numeratorBigIntegervaluedenominator- the denominatorBigIntegervalue (0 not allowed)- Returns:
- the rational number
- Throws:
ArithmeticException- if the denominator is 0 (division by zero)
-
valueOf
Creates a rational number of the specifiedBigIntegervalue.- Parameters:
value- theBigIntegervalue- Returns:
- the rational number
-
valueOf
Creates a rational number of the specified double value.- Parameters:
value- the double value- Returns:
- the rational number
- Throws:
NumberFormatException- if the double value is Infinite or NaN.
-
valueOf
Creates a rational number of the specifiedBigDecimalvalue.- Parameters:
value- the double value- Returns:
- the rational number
-
valueOf
Creates a rational number of the specified string representation.The accepted string representations are:
- Output of
toString(): "integerPart.fractionPart" - Output of
toRationalString(): "numerator/denominator" - Output of
toString()ofBigDecimal,BigInteger,Integer, ... - Output of
toString()ofDouble,Float- except "Infinity", "-Infinity" and "NaN"
- Parameters:
string- the string representation to convert- Returns:
- the rational number
- Throws:
ArithmeticException- if the denominator is 0 (division by zero)
- Output of
-
valueOfSimple
-
valueOf
public static BigRational valueOf(boolean positive, String integerPart, String fractionPart, String fractionRepeatPart, String exponentPart) -
valueOf
Creates a rational number of the specified numerator/denominator BigDecimal values.- Parameters:
numerator- the numeratorBigDecimalvaluedenominator- the denominatorBigDecimalvalue (0 not allowed)- Returns:
- the rational number
- Throws:
ArithmeticException- if the denominator is 0 (division by zero)
-
of
-
min
Returns the smallest of the specified rational numbers.- Parameters:
values- the rational numbers to compare- Returns:
- the smallest rational number, 0 if no numbers are specified
-
max
Returns the largest of the specified rational numbers.- Parameters:
values- the rational numbers to compare- Returns:
- the largest rational number, 0 if no numbers are specified
- See Also:
-
bernoulli
Calculates the Bernoulli number for the specified index.This function calculates the first Bernoulli numbers and therefore
bernoulli(1)returns -0.5Note that
bernoulli(x)for all odd x > 1 returns 0- Parameters:
n- the index of the Bernoulli number to be calculated (starting at 0)- Returns:
- the Bernoulli number for the specified index
- Throws:
ArithmeticException- if x is lesser than 0
-
calculateBernoulli
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-