Package org.sosy_lab.common.rationals
Class ExtendedRational
- java.lang.Object
-
- org.sosy_lab.common.rationals.ExtendedRational
-
- All Implemented Interfaces:
Comparable<ExtendedRational>
@Immutable public final class ExtendedRational extends Object implements Comparable<ExtendedRational>
This class represents "extended rational": rationals which allow for infinities, negative infinities and undefined numbers.Any operation on the numbers is guaranteed to never yield an exception.
Represented as wrapper around
Rational
class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExtendedRational.NumberType
-
Field Summary
Fields Modifier and Type Field Description static ExtendedRational
INFTY
static ExtendedRational
NaN
static ExtendedRational
NEG_INFTY
static ExtendedRational
ZERO
-
Constructor Summary
Constructors Constructor Description ExtendedRational(Rational pRational)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(ExtendedRational b)
ExtendedRational
divides(ExtendedRational b)
boolean
equals(@Nullable Object y)
Rational
getRational()
If the represented number is rational, return the wrapped object.int
hashCode()
boolean
isRational()
ExtendedRational
minus(ExtendedRational b)
ExtendedRational
negate()
static ExtendedRational
ofString(String s)
Reverses the effect oftoString()
.ExtendedRational
plus(ExtendedRational b)
ExtendedRational
reciprocal()
ExtendedRational
times(ExtendedRational b)
double
toDouble()
Returns rational converted to double.String
toString()
Returns one of TWO things.
-
-
-
Field Detail
-
INFTY
public static final ExtendedRational INFTY
-
NEG_INFTY
public static final ExtendedRational NEG_INFTY
-
ZERO
public static final ExtendedRational ZERO
-
NaN
public static final ExtendedRational NaN
-
-
Constructor Detail
-
ExtendedRational
public ExtendedRational(Rational pRational)
-
-
Method Detail
-
isRational
public boolean isRational()
-
getRational
public Rational getRational()
If the represented number is rational, return the wrapped object.- Throws:
UnsupportedOperationException
- in case the value is not rational
-
toDouble
public double toDouble()
Returns rational converted to double.The method works, because the Java Double class also supports Infinity/-Infinity/NaN.
-
toString
public String toString()
Returns one of TWO things. a) String of the form num/den if the number is rational. b) String representation of infinity/etc, consistent with theDouble
class.
-
ofString
public static ExtendedRational ofString(String s)
Reverses the effect oftoString()
. Supports 4 different formats, to be consistent with theDouble
class:"Infinity"
"-Infinity"
"NaN"
a/b
for some integersa
andb
a
for some integera
- Parameters:
s
- Input string,- Returns:
- New
ExtendedRational
. - Throws:
NumberFormatException
-s
is not a valid representation of ExtendedRational.
-
compareTo
public int compareTo(ExtendedRational b)
- Specified by:
compareTo
in interfaceComparable<ExtendedRational>
-
times
public ExtendedRational times(ExtendedRational b)
-
plus
public ExtendedRational plus(ExtendedRational b)
-
minus
public ExtendedRational minus(ExtendedRational b)
-
divides
public ExtendedRational divides(ExtendedRational b)
-
reciprocal
public ExtendedRational reciprocal()
-
negate
public ExtendedRational negate()
-
-