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 BigRational
ONE
static BigRational
ONE_HALF
static BigRational
ZERO
-
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 BigRational
abs()
BigRational
add(BigRational b)
Computes the sum of this and the argument.byte
byteValue()
BigInteger
ceilInt()
int
compareTo(BigRational other)
int
compareTo(BigInteger other)
BigRational
divide(BigRational b)
Computes the fraction of this and the argument.BigRational
divide(BigInteger b)
Computes the fraction of this and the argument.double
doubleValue()
boolean
equals(Object o)
boolean
equals(BigInteger other)
BigRational
expandTo(BigInteger newDenominator)
float
floatValue()
BigInteger
floorInt()
BigInteger
getDenominator()
BigInteger
getNumerator()
int
hashCode()
int
intValue()
long
longValue()
BigRational
multiply(BigRational b)
Computes the product of this and the argument.BigRational
multiply(BigInteger b)
Computes the product of this and the argument.BigRational
negate()
BigRational
normalize()
BigRational
reciprocal()
int
signum()
BigRational
subtract(BigRational b)
Computes the subtraction of this and the argument.BigDecimal
toBigDecimal(Precision decPrec)
BigDecimal
toBigDecimal(Scale decPrec)
Converts this to a BigDecimal with decPrec digits precision.String
toString()
String
toString(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:
compareTo
in 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:
floatValue
in classNumber
- Returns:
- this as a float (rounding may be necessary)
-
doubleValue
public double doubleValue()
- Specified by:
doubleValue
in 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()
-
-