Package de.tilman_neumann.jml.base
Class BigRational
- java.lang.Object
-
- java.lang.Number
-
- de.tilman_neumann.jml.base.BigRational
-
- All Implemented Interfaces:
Serializable,Comparable<BigRational>
public class BigRational extends Number implements Comparable<BigRational>
Big rational numbers with exact arithmetics.- Author:
- Tilman Neumann
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static BigRationalONEstatic BigRationalONE_HALFstatic BigRationalZERO
-
Constructor Summary
Constructors Constructor Description BigRational(BigInteger n)Constructor for an integer.BigRational(BigInteger num, BigInteger den)Constructor for a rational number.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BigRationalabs()BigRationaladd(BigRational b)Computes the sum of this and the argument.bytebyteValue()BigIntegerceilInt()intcompareTo(BigRational other)intcompareTo(BigInteger other)BigRationaldivide(BigRational b)Computes the fraction of this and the argument.BigRationaldivide(BigInteger b)Computes the fraction of this and the argument.doubledoubleValue()booleanequals(Object o)booleanequals(BigInteger other)BigRationalexpandTo(BigInteger newDenominator)floatfloatValue()BigIntegerfloorInt()BigIntegergetDenominator()BigIntegergetNumerator()inthashCode()intintValue()longlongValue()BigRationalmultiply(BigRational b)Computes the product of this and the argument.BigRationalmultiply(BigInteger b)Computes the product of this and the argument.BigRationalnegate()BigRationalnormalize()BigRationalreciprocal()intsignum()BigRationalsubtract(BigRational b)Computes the subtraction of this and the argument.BigDecimaltoBigDecimal(Precision decPrec)BigDecimaltoBigDecimal(Scale decPrec)Converts this to a BigDecimal with decPrec digits precision.StringtoString()StringtoString(Scale decPrec)Converts this into a string with the given decimal digits precision.-
Methods inherited from class java.lang.Number
shortValue
-
-
-
-
Field Detail
-
ZERO
public static final BigRational ZERO
-
ONE_HALF
public static final BigRational ONE_HALF
-
ONE
public static final BigRational ONE
-
-
Constructor Detail
-
BigRational
public BigRational(BigInteger n)
Constructor for an integer.- Parameters:
n- Number in decimal representation.
-
BigRational
public BigRational(BigInteger num, BigInteger den)
Constructor for a rational number.- Parameters:
num- Numeratorden- Denominator
-
-
Method Detail
-
add
public BigRational add(BigRational b)
Computes the sum of this and the argument.- Parameters:
b- Argument.- Returns:
- sum
-
subtract
public BigRational subtract(BigRational b)
Computes the subtraction of this and the argument.- Parameters:
b- Argument.- Returns:
- subtraction
-
multiply
public BigRational multiply(BigInteger b)
Computes the product of this and the argument.- Parameters:
b- Argument.- Returns:
- product
-
multiply
public BigRational multiply(BigRational b)
Computes the product of this and the argument.- Parameters:
b- Argument.- Returns:
- product
-
divide
public BigRational divide(BigInteger b)
Computes the fraction of this and the argument.- Parameters:
b- Argument.- Returns:
- fraction
-
divide
public BigRational divide(BigRational b)
Computes the fraction of this and the argument.- Parameters:
b- Argument.- Returns:
- fraction
-
abs
public BigRational abs()
- Returns:
- absolut value
-
negate
public BigRational negate()
- Returns:
- copy of this with opposite sign
-
reciprocal
public BigRational reciprocal()
- Returns:
- 1/this
-
normalize
public BigRational normalize()
- Returns:
- this with gcd of numerator and denominator reduced to 1.
-
expandTo
public BigRational expandTo(BigInteger newDenominator)
-
getNumerator
public BigInteger getNumerator()
- Returns:
- The numerator of this number.
-
getDenominator
public BigInteger getDenominator()
- Returns:
- The denominator of this number.
-
signum
public int signum()
- Returns:
- <0/0/>0 if this is negative/zero/positive.
-
floorInt
public BigInteger floorInt()
- Returns:
- the nearest smaller-or-equal integer value.
-
ceilInt
public BigInteger ceilInt()
- Returns:
- the nearest bigger-or-equal integer value.
-
compareTo
public int compareTo(BigRational other)
- Specified by:
compareToin interfaceComparable<BigRational>
-
compareTo
public int compareTo(BigInteger other)
-
equals
public boolean equals(BigInteger other)
-
byteValue
public byte byteValue()
-
intValue
public int intValue()
-
longValue
public long longValue()
-
floatValue
public float floatValue()
- Specified by:
floatValuein classNumber- Returns:
- this as a float (rounding may be necessary)
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classNumber- Returns:
- this as a double (rounding may be necessary)
-
toBigDecimal
public BigDecimal toBigDecimal(Precision decPrec)
-
toBigDecimal
public BigDecimal toBigDecimal(Scale decPrec)
Converts this to a BigDecimal with decPrec digits precision. Because we can access numerator and denominator directly, implementation in this class is more efficient than as BigFloat.valueOf(BigRational).- Parameters:
decPrec- Precision in decimal digits.- Returns:
- this as a big float with the wanted precision.
-
toString
public String toString()
-
-