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>
Rational class, throws
IllegalArgumentException 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:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns absolute value ofthis.intReturnthis / b.doublebooleanfloatgetDen()getNum()inthashCode()intintValue()booleanlongstatic RationalReturn a new instance equal tothis - b.negate()Return negation ofthis.static Rationalof(long l) Syntax sugar.static RationalSyntax 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 RationalReverses the effect oftoString().Return a new instance equal to the sum ofthisandb.Return reciprocal ofthis.intsignum()Returns -1, 0 or 1, representing the sign of the rational number.Multiply byb, return a new instance.toString()Returns string of the form num/den.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
-
ONE
-
NEG_ONE
-
-
Method Details
-
of
Create a new rational from a numerator and a denominator. -
ofLongs
Create a new rational from two longs. -
ofLong
Create a new rational equal to the given long. -
ofBigInteger
Create a new rational equal to the given BigInteger. -
ofString
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
Syntax sugar helper for creating Rationals.- See Also:
-
of
Syntax sugar.- See Also:
-
ofBigDecimal
Convert a given BigDecimal to Rational. -
times
Multiply byb, return a new instance. -
plus
Return a new instance equal to the sum ofthisandb. -
minus
Return a new instance equal tothis - b. -
divides
Returnthis / b. -
reciprocal
Return reciprocal ofthis.- Throws:
IllegalArgumentException- If invoked on zero.
-
negate
Return negation ofthis. -
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
isIntegral
public boolean isIntegral() -
getNum
-
getDen
-
signum
public int signum()Returns -1, 0 or 1, representing the sign of the rational number. -
abs
Returns absolute value ofthis. -
toString
Returns string of the form num/den. -
compareTo
- Specified by:
compareToin interfaceComparable<Rational>
-
equals
-
hashCode
public int hashCode() -
max
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-