Class BigRational
- All Implemented Interfaces:
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)
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:
I do not claim ownership of this code, it is the intellectual property of @obermuhlner.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigRationalThe value 1 asBigRational.static final BigRationalThe value 0 asBigRational. -
Method Summary
Modifier and TypeMethodDescriptionadd(BigRational value) Calculates the addition (+) of this rational number and the specified argument.add(BigInteger value) Calculates the addition (+) of this rational number and the specified argument.intcompareTo(BigRational other) 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.divide(BigInteger value) Calculates the division (/) of this rational number and the specified argument.booleanReturns the denominator of this rational number as BigDecimal.Returns the numerator of this rational number as BigDecimal.inthashCode()booleanisZero()Returns whether this rational number is zero.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.multiply(BigInteger value) Calculates the multiplication (*) of this rational number and the specified argument.negate()Negates this rational number (inverting the sign).pow(int exponent) Calculates this rational number to the power (xy) of the specified argument.Calculates the reciprocal of this rational number (1/x).subtract(BigRational 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.toString()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(BigDecimal value) Creates a rational number of the specifiedBigDecimalvalue.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.
-
Field Details
-
ZERO
The value 0 asBigRational. -
ONE
The value 1 asBigRational.
-
-
Method Details
-
getNumerator
Returns the numerator of this rational number as BigDecimal.- Returns:
- the numerator as 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
-
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)
-
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
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
-
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
-
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
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
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
-
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
-
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
-
equals
-
toString
-
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 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 specifiedBigDecimalvalue.- Parameters:
value- the double value- Returns:
- the rational number
-