Class BigRational
- java.lang.Object
-
- java.lang.Number
-
- ch.obermuhlner.math.big.BigRational
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<BigRational>
public class BigRational extends java.lang.Number implements java.lang.Comparable<BigRational>, java.io.Serializable
A rational number represented as a quotient of two values.Basic calculations with rational numbers (+ - * /) have no loss of precision. This allows to use
BigRationalas a replacement forBigDecimalif absolute accuracy is desired.The values are internally stored as
BigDecimal(for performance optimizations) but represented asBigInteger(for mathematical correctness) when accessed withgetNumeratorBigInteger()andgetDenominatorBigInteger().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
BigRationalvalue can be converted into an arbitraryprecision(number of significant digits) orscale(number of digits after the decimal point).- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.List<BigRational>bernoulliCacheprivate java.math.BigDecimaldenominatorprivate java.math.BigDecimalnumeratorstatic BigRationalONEThe value 1 asBigRational.static BigRationalTENThe value 10 asBigRational.static BigRationalTWOThe value 2 asBigRational.static BigRationalZEROThe value 0 asBigRational.
-
Constructor Summary
Constructors Modifier Constructor Description privateBigRational(int value)privateBigRational(java.math.BigDecimal num, java.math.BigDecimal denom)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigRationalabs()Returns the absolute value of this rational number.BigRationaladd(int value)Calculates the addition (+) of this rational number and the specified argument.BigRationaladd(BigRational value)Calculates the addition (+) of this rational number and the specified argument.private BigRationaladd(java.math.BigDecimal value)BigRationaladd(java.math.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(java.math.BigInteger number)BigRationaldecrement()Calculates the decrement of this rational number (- 1).BigRationaldivide(int value)Calculates the division (/) of this rational number and the specified argument.BigRationaldivide(BigRational value)Calculates the division (/) of this rational number and the specified argument.private BigRationaldivide(java.math.BigDecimal value)BigRationaldivide(java.math.BigInteger value)Calculates the division (/) of this rational number and the specified argument.doubledoubleValue()booleanequals(java.lang.Object obj)floatfloatValue()BigRationalfractionPart()Returns the fraction part of this rational number.java.math.BigDecimalgetDenominator()Returns the denominator of this rational number as BigDecimal.java.math.BigIntegergetDenominatorBigInteger()Returns the denominator of this rational number as BigInteger.java.math.BigDecimalgetNumerator()Returns the numerator of this rational number as BigDecimal.java.math.BigIntegergetNumeratorBigInteger()Returns the numerator of this rational number as BigInteger.inthashCode()BigRationalincrement()Calculates the increment of this rational number (+ 1).BigRationalintegerPart()Returns the integer part of this rational number.intintValue()booleanisInteger()Returns whether this rational number is an integer number without fraction part.private booleanisIntegerInternal()Returns whether this rational number is an integer number without fraction part.private booleanisPositive()booleanisZero()Returns whether this rational number is zero.longlongValue()private 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.BigRationalmultiply(int value)Calculates the multiplication (*) of this rational number and the specified argument.BigRationalmultiply(BigRational value)Calculates the multiplication (*) of this rational number and the specified argument.private BigRationalmultiply(java.math.BigDecimal value)BigRationalmultiply(java.math.BigInteger value)Calculates the multiplication (*) of this rational number and the specified argument.BigRationalnegate()Negates this rational number (inverting the sign).private static BigRationalof(java.math.BigDecimal numerator, java.math.BigDecimal denominator)BigRationalpow(int exponent)Calculates this rational number to the power (xy) of the specified argument.private intprecision()BigRationalreciprocal()Calculates the reciprocal of this rational number (1/x).BigRationalreduce()Reduces this rational number to the smallest numerator/denominator with the same value.intsignum()Returns the signum function of this rational number.BigRationalsubtract(int value)Calculates the subtraction (-) of this rational number and the specified argument.BigRationalsubtract(BigRational value)Calculates the subtraction (-) of this rational number and the specified argument.private BigRationalsubtract(java.math.BigDecimal value)BigRationalsubtract(java.math.BigInteger value)Calculates the subtraction (-) of this rational number and the specified argument.java.math.BigDecimaltoBigDecimal()Returns this rational number as aBigDecimal.java.math.BigDecimaltoBigDecimal(java.math.MathContext mc)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.java.lang.StringtoIntegerRationalString()Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator".java.lang.StringtoPlainString()Returns a plain string representation of this rational number without any exponent.java.lang.StringtoRationalString()Returns the string representation of this rational number in the form "numerator/denominator".java.lang.StringtoString()static BigRationalvalueOf(boolean positive, java.lang.String integerPart, java.lang.String fractionPart, java.lang.String fractionRepeatPart, java.lang.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 BigRationalvalueOf(java.lang.String string)Creates a rational number of the specified string representation.static BigRationalvalueOf(java.math.BigDecimal value)Creates a rational number of the specifiedBigDecimalvalue.static BigRationalvalueOf(java.math.BigDecimal numerator, java.math.BigDecimal denominator)Creates a rational number of the specified numerator/denominator BigDecimal values.static BigRationalvalueOf(java.math.BigInteger value)Creates a rational number of the specifiedBigIntegervalue.static BigRationalvalueOf(java.math.BigInteger numerator, java.math.BigInteger denominator)Creates a rational number of the specified numerator/denominator BigInteger values.private static BigRationalvalueOfSimple(java.lang.String string)BigRationalwithPrecision(int precision)Returns a rational number with approximativelythisvalue and the specified precision.BigRationalwithScale(int scale)Returns a rational number with approximativelythisvalue and the specified scale.
-
-
-
Field Detail
-
ZERO
public static final BigRational ZERO
The value 0 asBigRational.
-
ONE
public static final BigRational ONE
The value 1 asBigRational.
-
TWO
public static final BigRational TWO
The value 2 asBigRational.
-
TEN
public static final BigRational TEN
The value 10 asBigRational.
-
numerator
private final java.math.BigDecimal numerator
-
denominator
private final java.math.BigDecimal denominator
-
bernoulliCache
private static java.util.List<BigRational> bernoulliCache
-
-
Method Detail
-
getNumeratorBigInteger
public java.math.BigInteger getNumeratorBigInteger()
Returns the numerator of this rational number as BigInteger.- Returns:
- the numerator as BigInteger
-
getNumerator
public java.math.BigDecimal getNumerator()
Returns the numerator of this rational number as BigDecimal.- Returns:
- the numerator as BigDecimal
-
getDenominatorBigInteger
public java.math.BigInteger 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
public java.math.BigDecimal 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
public BigRational reduce()
Reduces this rational number to the smallest numerator/denominator with the same value.- Returns:
- the reduced rational number
-
integerPart
public BigRational 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
public BigRational 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
public BigRational 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
public BigRational 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:
java.lang.ArithmeticException- if this number is 0 (division by zero)
-
abs
public BigRational 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
public BigRational 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
public BigRational 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
public BigRational add(BigRational value)
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
private BigRational add(java.math.BigDecimal value)
-
add
public BigRational add(java.math.BigInteger value)
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
public BigRational add(int value)
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
public BigRational subtract(BigRational value)
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
private BigRational subtract(java.math.BigDecimal value)
-
subtract
public BigRational subtract(java.math.BigInteger value)
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
public BigRational subtract(int value)
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
public BigRational multiply(BigRational value)
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
private BigRational multiply(java.math.BigDecimal value)
-
multiply
public BigRational multiply(java.math.BigInteger value)
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
public BigRational multiply(int value)
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
public BigRational divide(BigRational value)
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:
java.lang.ArithmeticException- if the argument is 0 (division by zero)
-
divide
private BigRational divide(java.math.BigDecimal value)
-
divide
public BigRational divide(java.math.BigInteger value)
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:
java.lang.ArithmeticException- if the argument is 0 (division by zero)
-
divide
public BigRational divide(int value)
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:
java.lang.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:
isInteger()
-
pow
public BigRational pow(int exponent)
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
private BigRational min(BigRational value)
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
private BigRational max(BigRational value)
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
public BigRational withPrecision(int precision)
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
public BigRational withScale(int scale)
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
private static int countDigits(java.math.BigInteger number)
-
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
public java.math.BigDecimal toBigDecimal()
Returns this rational number as aBigDecimal.- Returns:
- the
BigDecimalvalue
-
toBigDecimal
public java.math.BigDecimal toBigDecimal(java.math.MathContext mc)
Returns this rational number as aBigDecimalwith the precision specified by theMathContext.- Parameters:
mc- theMathContextspecifying the precision of the calculated result- Returns:
- the
BigDecimal
-
compareTo
public int compareTo(BigRational other)
- Specified by:
compareToin interfacejava.lang.Comparable<BigRational>
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
toPlainString
public java.lang.String toPlainString()
Returns a plain string representation of this rational number without any exponent.- Returns:
- the plain string representation
- See Also:
BigDecimal.toPlainString()
-
toRationalString
public java.lang.String 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:
valueOf(String),valueOf(int, int)
-
toIntegerRationalString
public java.lang.String 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(int, int, int)
-
valueOf
public static BigRational valueOf(int value)
Creates a rational number of the specified int value.- Parameters:
value- the int value- Returns:
- the rational number
-
valueOf
public static BigRational valueOf(int numerator, int denominator)
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:
java.lang.ArithmeticException- if the denominator is 0 (division by zero)
-
valueOf
public static BigRational valueOf(int integer, int fractionNumerator, int fractionDenominator)
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:
java.lang.ArithmeticException- if the fraction part denominator is 0 (division by zero), or if the fraction part numerator or denominator is negative
-
valueOf
public static BigRational valueOf(java.math.BigInteger numerator, java.math.BigInteger denominator)
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:
java.lang.ArithmeticException- if the denominator is 0 (division by zero)
-
valueOf
public static BigRational valueOf(java.math.BigInteger value)
Creates a rational number of the specifiedBigIntegervalue.- Parameters:
value- theBigIntegervalue- Returns:
- the rational number
-
valueOf
public static BigRational valueOf(double value)
Creates a rational number of the specified double value.- Parameters:
value- the double value- Returns:
- the rational number
- Throws:
java.lang.NumberFormatException- if the double value is Infinite or NaN.
-
valueOf
public static BigRational valueOf(java.math.BigDecimal value)
Creates a rational number of the specifiedBigDecimalvalue.- Parameters:
value- the double value- Returns:
- the rational number
-
valueOf
public static BigRational valueOf(java.lang.String string)
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:
java.lang.ArithmeticException- if the denominator is 0 (division by zero)
- Output of
-
valueOfSimple
private static BigRational valueOfSimple(java.lang.String string)
-
valueOf
public static BigRational valueOf(boolean positive, java.lang.String integerPart, java.lang.String fractionPart, java.lang.String fractionRepeatPart, java.lang.String exponentPart)
-
valueOf
public static BigRational valueOf(java.math.BigDecimal numerator, java.math.BigDecimal denominator)
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:
java.lang.ArithmeticException- if the denominator is 0 (division by zero)
-
of
private static BigRational of(java.math.BigDecimal numerator, java.math.BigDecimal denominator)
-
min
public static BigRational min(BigRational... values)
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
public static BigRational max(BigRational... values)
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:
max(BigRational)
-
bernoulli
public static BigRational bernoulli(int n)
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:
java.lang.ArithmeticException- if x is lesser than 0
-
calculateBernoulli
private static BigRational calculateBernoulli(int n)
-
intValue
public int intValue()
- Specified by:
intValuein classjava.lang.Number
-
longValue
public long longValue()
- Specified by:
longValuein classjava.lang.Number
-
floatValue
public float floatValue()
- Specified by:
floatValuein classjava.lang.Number
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classjava.lang.Number
-
-