Package de.tilman_neumann.jml.base
Class BigRational
java.lang.Object
java.lang.Number
de.tilman_neumann.jml.base.BigRational
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<BigRational>
public class BigRational extends java.lang.Number implements java.lang.Comparable<BigRational>
Big rational numbers with exact arithmetics.
- 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(java.math.BigInteger n)
Constructor for an integer.BigRational(java.math.BigInteger num, java.math.BigInteger den)
Constructor for a rational number. -
Method Summary
Modifier and Type Method Description BigRational
abs()
BigRational
add(BigRational b)
Computes the sum of this and the argument.byte
byteValue()
java.math.BigInteger
ceil()
int
compareTo(BigRational other)
int
compareTo(java.math.BigInteger other)
BigRational
divide(BigRational b)
Computes the fraction of this and the argument.BigRational
divide(java.math.BigInteger b)
Computes the fraction of this and the argument.double
doubleValue()
boolean
equals(java.lang.Object o)
boolean
equals(java.math.BigInteger other)
BigRational
expandTo(java.math.BigInteger newDenominator)
float
floatValue()
java.math.BigInteger
floor()
java.math.BigInteger
getDenominator()
java.math.BigInteger
getNumerator()
int
hashCode()
int
intValue()
boolean
isInteger()
boolean
isZero()
long
longValue()
BigRational
multiply(BigRational b)
Computes the product of this and the argument.BigRational
multiply(java.math.BigInteger b)
Computes the product of this and the argument.BigRational
negate()
BigRational
normalize()
BigRational
reciprocal()
java.math.BigInteger
round()
int
signum()
BigRational
subtract(BigRational b)
Computes the subtraction of this and the argument.java.math.BigDecimal
toBigDecimal(Precision decPrec)
java.math.BigDecimal
toBigDecimal(Scale decPrec)
Converts this to a BigDecimal with decPrec digits precision.java.lang.String
toString()
java.lang.String
toString(Scale decPrec)
Converts this into a string with the given decimal digits precision.Methods inherited from class java.lang.Number
shortValue
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Field Details
-
ZERO
-
ONE_HALF
-
ONE
-
-
Constructor Details
-
BigRational
public BigRational(java.math.BigInteger n)Constructor for an integer.- Parameters:
n
- Number in decimal representation.
-
BigRational
public BigRational(java.math.BigInteger num, java.math.BigInteger den)Constructor for a rational number.- Parameters:
num
- Numeratorden
- Denominator
-
-
Method Details
-
add
Computes the sum of this and the argument.- Parameters:
b
- Argument.- Returns:
- sum
-
subtract
Computes the subtraction of this and the argument.- Parameters:
b
- Argument.- Returns:
- subtraction
-
multiply
Computes the product of this and the argument.- Parameters:
b
- Argument.- Returns:
- product
-
multiply
Computes the product of this and the argument.- Parameters:
b
- Argument.- Returns:
- product
-
divide
Computes the fraction of this and the argument.- Parameters:
b
- Argument.- Returns:
- fraction
-
divide
Computes the fraction of this and the argument.- Parameters:
b
- Argument.- Returns:
- fraction
-
abs
- Returns:
- absolut value
-
negate
- Returns:
- copy of this with opposite sign
-
reciprocal
- Returns:
- 1/this
-
normalize
- Returns:
- this with gcd of numerator and denominator reduced to 1, and the denominator being positive.
-
expandTo
-
getNumerator
public java.math.BigInteger getNumerator()- Returns:
- The numerator of this number.
-
getDenominator
public java.math.BigInteger getDenominator()- Returns:
- The denominator of this number.
-
signum
public int signum()- Returns:
- <0/0/>0 if this is negative/zero/positive.
-
isZero
public boolean isZero()- Returns:
- true if this is zero, false otherwise
-
isInteger
public boolean isInteger()- Returns:
- true if this is integral.
-
floor
public java.math.BigInteger floor()- Returns:
- the nearest smaller-or-equal integer value.
-
ceil
public java.math.BigInteger ceil()- Returns:
- the nearest bigger-or-equal integer value.
-
round
public java.math.BigInteger round()- Returns:
- the nearest integer value. In case of a tie, the smaller integer value is returned (rounding mode HALF_DOWN).
-
compareTo
- Specified by:
compareTo
in interfacejava.lang.Comparable<BigRational>
-
compareTo
public int compareTo(java.math.BigInteger other) -
equals
public boolean equals(java.lang.Object o)- Overrides:
equals
in classjava.lang.Object
-
equals
public boolean equals(java.math.BigInteger other) -
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
byteValue
public byte byteValue()- Overrides:
byteValue
in classjava.lang.Number
- Returns:
- this as a byte (rounding may be necessary)
-
intValue
public int intValue()- Specified by:
intValue
in classjava.lang.Number
- Returns:
- this as an int (rounding may be necessary)
-
longValue
public long longValue()- Specified by:
longValue
in classjava.lang.Number
- Returns:
- this as a long integer (rounding may be necessary)
-
floatValue
public float floatValue()- Specified by:
floatValue
in classjava.lang.Number
- Returns:
- this as a float (rounding may be necessary)
-
doubleValue
public double doubleValue()- Specified by:
doubleValue
in classjava.lang.Number
- Returns:
- this as a double (rounding may be necessary)
-
toBigDecimal
-
toBigDecimal
Converts this to a BigDecimal with decPrec digits precision.- Parameters:
decPrec
- Precision in decimal digits.- Returns:
- this as a BigDecimal with the wanted precision.
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
- Returns:
- this as a (fractional) string
-
toString
Converts this into a string with the given decimal digits precision.- Parameters:
decPrec
- output precision in decimal digits.- Returns:
- this as a string with the wanted precision.
-