Package org.sosy_lab.common.rationals
Class Rational
- java.lang.Object
-
- java.lang.Number
-
- org.sosy_lab.common.rationals.Rational
-
- All Implemented Interfaces:
Serializable,Comparable<Rational>
@Immutable public final class Rational extends Number implements Comparable<Rational>
Rational class, throwsIllegalArgumentExceptionon unsupported operations (e.g.1/0).The Rational object is immutable. All arithmetic operations return new instances.
For performance and convenience, there is always only a single
Rationalinstance representing numbers 0, 1 and -1. These numbers can be compared using==operator.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Rationalabs()Returns absolute value ofthis.intcompareTo(Rational b)Rationaldivides(Rational b)Returnthis / b.doubledoubleValue()booleanequals(@Nullable Object y)floatfloatValue()BigIntegergetDen()BigIntegergetNum()inthashCode()intintValue()booleanisIntegral()longlongValue()static Rationalmax(Rational a, Rational b)Rationalminus(Rational b)Return a new instance equal tothis - b.Rationalnegate()Return negation ofthis.static Rationalof(long l)Syntax sugar.static Rationalof(String s)Syntax sugar helper for creating Rationals.static Rationalof(BigInteger numerator, BigInteger denominator)Create a new rational from a numerator and a denominator.static RationalofBigDecimal(BigDecimal decimal)Convert a given BigDecimal to Rational.static RationalofBigInteger(BigInteger numerator)Create a new rational equal to the given BigInteger.static RationalofLong(long numerator)Create a new rational equal to the given long.static RationalofLongs(long numerator, long denominator)Create a new rational from two longs.static RationalofString(String s)Reverses the effect oftoString().Rationalplus(Rational b)Return a new instance equal to the sum ofthisandb.Rationalreciprocal()Return reciprocal ofthis.intsignum()Returns -1, 0 or 1, representing the sign of the rational number.Rationaltimes(Rational b)Multiply byb, return a new instance.StringtoString()Returns string of the form num/den.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Method Detail
-
of
public static Rational of(BigInteger numerator, BigInteger denominator)
Create a new rational from a numerator and a denominator.
-
ofLongs
public static Rational ofLongs(long numerator, long denominator)
Create a new rational from two longs.
-
ofLong
public static Rational ofLong(long numerator)
Create a new rational equal to the given long.
-
ofBigInteger
public static Rational ofBigInteger(BigInteger numerator)
Create a new rational equal to the given BigInteger.
-
ofString
public static Rational ofString(String s)
Reverses the effect oftoString(). Supports 2 different formats: with slash (e.g.25/17) or without slash (e.g.5)- Parameters:
s- Input string- Throws:
NumberFormatException- iffsis not a valid representation of Rational.IllegalArgumentException- If the resulting rational is undefined (e.g. 0/0 or 1/0).
-
of
public static Rational of(String s)
Syntax sugar helper for creating Rationals.- See Also:
ofString(String)
-
of
public static Rational of(long l)
Syntax sugar.- See Also:
ofLong(long)
-
ofBigDecimal
public static Rational ofBigDecimal(BigDecimal decimal)
Convert a given BigDecimal to Rational.
-
reciprocal
public Rational reciprocal()
Return reciprocal ofthis.- Throws:
IllegalArgumentException- If invoked on zero.
-
negate
public Rational negate()
Return negation ofthis.
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classNumber
-
isIntegral
public boolean isIntegral()
-
getNum
public BigInteger getNum()
-
getDen
public BigInteger getDen()
-
signum
public int signum()
Returns -1, 0 or 1, representing the sign of the rational number.
-
abs
public Rational abs()
Returns absolute value ofthis.
-
toString
public String toString()
Returns string of the form num/den.
-
compareTo
public int compareTo(Rational b)
- Specified by:
compareToin interfaceComparable<Rational>
-
floatValue
public float floatValue()
- Specified by:
floatValuein classNumber
-
-