Class Rational
java.lang.Object
de.uni_freiburg.informatik.ultimate.logic.Rational
- All Implemented Interfaces:
java.lang.Comparable<Rational>
public class Rational extends java.lang.Object implements java.lang.Comparable<Rational>
Class that represents a rational number num/denom,
where num and denom are BigInteger. This class also handles
infinity and NAN.
Internally, the numbers are always kept in reduced form; the
denominator is always kept non-negative. A zero denominator
with non-zero numerator indicates positive or negative infinity.
The number 0/0 is used to represent NAN (not a number).
Every operation that involves NAN gives NAN as result.
The non-obvious rules for ZERO, INFINITY, and NAN are:
ONE.div(NEGATIVE_INFINITY).isNegative() = false (no negative ZERO) ZERO.inverse() = POSITIVE_INFINITY POSITIVE_INFINITY.add(finite number) = POSITIVE_INFINITY. POSITIVE_INFINITY.add(NEGATIVE_INFINITY) = NAN ZERO.mul(POSITIVE_INFINITY) = ZERO.mul(NEGATIVE_INFINITY) = NAN number.div(ZERO) = POSITIVE_INFINITY.mul(number.signum()) NAN.isIntegral = POSITIVE_INFINITY.isIntegral = true NAN.floor() = NAN; POSITIVE_INFINITY.floor() = POSITIVE_INFINITY; NAN.frac() = POSITIVE_INFINITY.frac() = ZERO;This class only uses bigintegers if either numerator or denominator does not fit into an int.
- Author:
- Juergen Christ, Jochen Hoenicke
-
Field Summary
Fields Modifier and Type Field Description static RationalMONEThe number -1.static RationalNANNot a number.static RationalNEGATIVE_INFINITYNegative infinity.static RationalONEThe number 1.static RationalPOSITIVE_INFINITYPositive infinity.static RationalTWOThe number 2.static RationalZEROThe number 0. -
Method Summary
Modifier and Type Method Description Rationalabs()Compute the absolute of this rational.Rationaladd(Rational other)Return a new rational representingthis + other.Rationaladdmul(Rational fac1, Rational fac2)Computesthis + (fac1*fac2).Rationalceil()Return a new rational representing the smallest integral rational not smaller thanthis.intcompareTo(Rational o)java.math.BigIntegerdenominator()Get the denominator of this rational.Rationaldiv(Rational other)Return a new rational representingthis / other.booleanequals(java.lang.Object o)Rationalfloor()Return a new rational representing the biggest integral rational not bigger thanthis.Rationalfrac()Returns the fractional part of the rational, i.e.static intgcd(int a, int b)Calculates the greatest common divisor of two numbers.static longgcd(long a, long b)Calculates the greatest common divisor of two numbers.Rationalgcd(Rational other)Compute the gcd of two rationals (this and other).static java.math.BigIntegergcd(java.math.BigInteger i1, java.math.BigInteger i2)Compute the gcd of two BigInteger.inthashCode()Computes a hashcode.Rationalinverse()Returns a new rational representing the inverse ofthis.booleanisIntegral()Check whether this rational represents an integral value.booleanisNegative()Check whether this rational is negative.booleanisRational()Check whether this rational corresponds to a (finite) rational value.Rationalmul(Rational other)Return a new rational representingthis * other.Rationalmul(java.math.BigInteger other)Return a new rational representingthis * other.Rationalnegate()Returns a new rational equal to-this.java.math.BigIntegernumerator()Get the numerator of this rational.intsignum()Return the sign of this rational.Rationalsub(Rational other)Return a new rational representingthis - other.Rationalsubdiv(Rational s, Rational d)Computes(this - s) / d.java.lang.StringtoString()Get a string representation of this number.TermtoTerm(Sort sort)Creates a constant term containing this rational.static RationalvalueOf(long newnum, long newdenom)Construct a rational from two longs.static RationalvalueOf(java.math.BigInteger bignum, java.math.BigInteger bigdenom)Construct a rational from two bigints.
-
Field Details
-
POSITIVE_INFINITY
Positive infinity. Used to represent the result of 1/0. -
NAN
Not a number. Used to represent the result of 0/0. -
NEGATIVE_INFINITY
Negative infinity. Used to represent the result of -1/0. -
ZERO
The number 0. -
ONE
The number 1. -
MONE
The number -1. -
TWO
The number 2.
-
-
Method Details
-
valueOf
Construct a rational from two bigints. Use this method to create a rational number if the numerator or denominator may be to big to fit in an int. This method normalizes the rational number and does partial unification.- Parameters:
bignum- The numerator.bigdenom- The denominator.- Returns:
- a rational representing numerator/denominator.
-
valueOf
Construct a rational from two longs. Use this method to create a rational number. This method normalizes the rational number and may return a previously created one. This method does not work if called with value Long.MIN_VALUE.- Parameters:
newnum- The numerator.newdenom- The denominator.- Returns:
- a rational representing numerator/denominator.
-
gcd
public static int gcd(int a, int b)Calculates the greatest common divisor of two numbers. Expects two nonnegative values.- Parameters:
a- First numberb- Second Number- Returns:
- Greatest common divisor
-
gcd
public static long gcd(long a, long b)Calculates the greatest common divisor of two numbers.- Parameters:
a- First numberb- Second Number- Returns:
- Greatest common divisor
-
gcd
public static java.math.BigInteger gcd(java.math.BigInteger i1, java.math.BigInteger i2)Compute the gcd of two BigInteger. This is the same asi1.gcd(i2), but it is more efficient for small numbers.- Parameters:
i1- the first big integer.i2- the second big integer.- Returns:
- the gcd of i1 and i2.
-
add
Return a new rational representingthis + other.- Parameters:
other- Rational to add.- Returns:
- Sum of
thisandother.
-
addmul
Computesthis + (fac1*fac2).- Parameters:
fac1- one of the factors.fac2- the other factor.- Returns:
- the result of the computation.
-
subdiv
Computes(this - s) / d.- Parameters:
s- the rational to subtract.d- the divisor.- Returns:
- the result of the computation.
-
negate
Returns a new rational equal to-this.- Returns:
-this.
-
sub
Return a new rational representingthis - other.- Parameters:
other- Rational to subtract.- Returns:
- Difference of
thisandother.
-
mul
Return a new rational representingthis * other.- Parameters:
other- Rational to multiply.- Returns:
- Product of
thisandother.
-
mul
Return a new rational representingthis * other.- Parameters:
other- big integer to multiply.- Returns:
- Product of
thisandother.
-
div
Return a new rational representingthis / other.- Parameters:
other- Rational to divide.- Returns:
- Quotient of
thisandother.
-
gcd
Compute the gcd of two rationals (this and other). The gcd is the rational number, such that dividing this and other with the gcd will yield two co-prime integers. The gcd is always non-negative.- Parameters:
other- the second rational argument.- Returns:
- the gcd of this and other.
-
inverse
Returns a new rational representing the inverse ofthis.- Returns:
- Inverse of
this.
-
isNegative
public boolean isNegative()Check whether this rational is negative.- Returns:
trueiff this rational is negative.
-
signum
public int signum()Return the sign of this rational. The sign will be -1, 0, or 1 if this rational is negative, zero, or positive.- Returns:
- The sign of this rational.
-
compareTo
- Specified by:
compareToin interfacejava.lang.Comparable<Rational>
-
equals
public boolean equals(java.lang.Object o)- Overrides:
equalsin classjava.lang.Object
-
numerator
public java.math.BigInteger numerator()Get the numerator of this rational.- Returns:
- the numerator.
-
denominator
public java.math.BigInteger denominator()Get the denominator of this rational.- Returns:
- the denominator.
-
hashCode
public int hashCode()Computes a hashcode. The hashcode is computed as257 * numerator + denominatorif both fit into an integer and257 * numerator().hashCode() + denominator().hashCode()if big integers are necessary.- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hashcode.
-
toString
public java.lang.String toString()Get a string representation of this number. This isnumerator()+ "/" + denominator()except for infinity ("inf"), nan ("nan"), or minus infinity ("-inf").- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation.
-
isIntegral
public boolean isIntegral()Check whether this rational represents an integral value. Both infinity values are treated as integral.- Returns:
trueiff value is integral.
-
floor
Return a new rational representing the biggest integral rational not bigger thanthis.- Returns:
- Next smaller integer of
this.
-
frac
Returns the fractional part of the rational, i.e. the number this.sub(this.floor()).- Returns:
- Next smaller integer of
this.
-
ceil
Return a new rational representing the smallest integral rational not smaller thanthis.- Returns:
- Next bigger integer of
this.
-
abs
Compute the absolute of this rational.- Returns:
- Rational that is equal to the absolute value of this rational.
-
toTerm
Creates a constant term containing this rational.- Parameters:
sort- the sort of the constant term that should be created.- Returns:
- a constant term with this rational value.
- Throws:
SMTLIBException- if term is infinite or NaN, if the sort is not numeric, or if the term is not integral and the sort is Int.
-
isRational
public boolean isRational()Check whether this rational corresponds to a (finite) rational value. This function can be used to test for infinites and NaNs.- Returns:
- true if and only if this rational is not infinite or NaN.
-