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, throwsIllegalArgumentException
on 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
Rational
instance 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 Rational
abs()
Returns absolute value ofthis
.int
compareTo(Rational b)
Rational
divides(Rational b)
Returnthis / b
.double
doubleValue()
boolean
equals(@Nullable Object y)
float
floatValue()
BigInteger
getDen()
BigInteger
getNum()
int
hashCode()
int
intValue()
boolean
isIntegral()
long
longValue()
static Rational
max(Rational a, Rational b)
Rational
minus(Rational b)
Return a new instance equal tothis - b
.Rational
negate()
Return negation ofthis
.static Rational
of(long l)
Syntax sugar.static Rational
of(String s)
Syntax sugar helper for creating Rationals.static Rational
of(BigInteger numerator, BigInteger denominator)
Create a new rational from a numerator and a denominator.static Rational
ofBigDecimal(BigDecimal decimal)
Convert a given BigDecimal to Rational.static Rational
ofBigInteger(BigInteger numerator)
Create a new rational equal to the given BigInteger.static Rational
ofLong(long numerator)
Create a new rational equal to the given long.static Rational
ofLongs(long numerator, long denominator)
Create a new rational from two longs.static Rational
ofString(String s)
Reverses the effect oftoString()
.Rational
plus(Rational b)
Return a new instance equal to the sum ofthis
andb
.Rational
reciprocal()
Return reciprocal ofthis
.int
signum()
Returns -1, 0 or 1, representing the sign of the rational number.Rational
times(Rational b)
Multiply byb
, return a new instance.String
toString()
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
- iffs
is 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:
doubleValue
in 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:
compareTo
in interfaceComparable<Rational>
-
floatValue
public float floatValue()
- Specified by:
floatValue
in classNumber
-
-