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 BigRationalONEstatic BigRationalONE_HALFstatic BigRationalZERO -
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 BigRationalabs()BigRationaladd(BigRational b)Computes the sum of this and the argument.bytebyteValue()java.math.BigIntegerceil()intcompareTo(BigRational other)intcompareTo(java.math.BigInteger other)BigRationaldivide(BigRational b)Computes the fraction of this and the argument.BigRationaldivide(java.math.BigInteger b)Computes the fraction of this and the argument.doubledoubleValue()booleanequals(java.lang.Object o)booleanequals(java.math.BigInteger other)BigRationalexpandTo(java.math.BigInteger newDenominator)floatfloatValue()java.math.BigIntegerfloor()java.math.BigIntegergetDenominator()java.math.BigIntegergetNumerator()inthashCode()intintValue()booleanisInteger()booleanisZero()longlongValue()BigRationalmultiply(BigRational b)Computes the product of this and the argument.BigRationalmultiply(java.math.BigInteger b)Computes the product of this and the argument.BigRationalnegate()BigRationalnormalize()BigRationalreciprocal()java.math.BigIntegerround()intsignum()BigRationalsubtract(BigRational b)Computes the subtraction of this and the argument.java.math.BigDecimaltoBigDecimal(Precision decPrec)java.math.BigDecimaltoBigDecimal(Scale decPrec)Converts this to a BigDecimal with decPrec digits precision.java.lang.StringtoString()java.lang.StringtoString(Scale decPrec)Converts this into a string with the given decimal digits precision.Methods inherited from class java.lang.Number
shortValueMethods 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:
compareToin interfacejava.lang.Comparable<BigRational>
-
compareTo
public int compareTo(java.math.BigInteger other) -
equals
public boolean equals(java.lang.Object o)- Overrides:
equalsin classjava.lang.Object
-
equals
public boolean equals(java.math.BigInteger other) -
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
byteValue
public byte byteValue()- Overrides:
byteValuein classjava.lang.Number- Returns:
- this as a byte (rounding may be necessary)
-
intValue
public int intValue()- Specified by:
intValuein classjava.lang.Number- Returns:
- this as an int (rounding may be necessary)
-
longValue
public long longValue()- Specified by:
longValuein classjava.lang.Number- Returns:
- this as a long integer (rounding may be necessary)
-
floatValue
public float floatValue()- Specified by:
floatValuein classjava.lang.Number- Returns:
- this as a float (rounding may be necessary)
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein 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:
toStringin 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.
-