Class MutableRational
java.lang.Object
de.uni_freiburg.informatik.ultimate.logic.MutableRational
- All Implemented Interfaces:
java.lang.Comparable<MutableRational>
public class MutableRational extends java.lang.Object implements java.lang.Comparable<MutableRational>
Mutable version of the
Rational class. All arithmetic
operations change the value of this object.
This class is intended to save some unneeded temporary objects in bigger
calculations. This should reduce the number of garbage collections such that
the program should run faster.- Author:
- Juergen Christ
-
Constructor Summary
Constructors Constructor Description MutableRational(int num, int denom)Create a new rational representing num/denom.MutableRational(MutableRational r)Create a new copy of a mutable rational.MutableRational(Rational r)Create a new mutable rational from a rational.MutableRational(java.math.BigInteger num, java.math.BigInteger denom)Create a new rational representing num/denom. -
Method Summary
Modifier and Type Method Description MutableRationaladd(Rational other)Add the rational other to this rational.MutableRationaladdmul(Rational fac1, Rational fac2)Computesthis += (fac1*fac2).MutableRationaladdmul(Rational fac1, java.math.BigInteger fac2)Computesthis += (fac1*fac2).intcompareTo(MutableRational o)intcompareTo(Rational o)Compares this mutable rational with the other.java.math.BigIntegerdenominator()Get the denominator of this rational.MutableRationaldiv(Rational other)Divide this rational by the other and store the result in this.booleanequals(java.lang.Object o)Compares this mutable rational with the other.inthashCode()Computes a hashcode.MutableRationalinverse()Compute the multiplicative inverse of this rational and store it in this.booleanisIntegral()Check whether this rational represents an integral value.booleanisNegative()Check if this rational is negative.MutableRationalmul(Rational other)Multiply this rational with other and store the result in this.MutableRationalnegate()Negate this rational, i.e., this = -this.java.math.BigIntegernumerator()Get the numerator of this rational.voidsetValue(long newnum, long newdenom)Set the value of this rational to newnum/newdenom.voidsetValue(Rational value)Set the value of this rational to value.intsignum()Compute sign of this rational.MutableRationalsub(Rational other)Subtract the other rational from this.MutableRationalsubdiv(Rational s, Rational d)Computesthis = (this - s) / d.RationaltoRational()Convert this mutable rational into a immutable rational.java.lang.StringtoString()Get a string representation of this number.
-
Constructor Details
-
MutableRational
public MutableRational(int num, int denom)Create a new rational representing num/denom.- Parameters:
num- the numerator.denom- the denominator.
-
MutableRational
public MutableRational(java.math.BigInteger num, java.math.BigInteger denom)Create a new rational representing num/denom.- Parameters:
num- the numerator.denom- the denominator.
-
MutableRational
Create a new mutable rational from a rational.- Parameters:
r- the rational.
-
MutableRational
Create a new copy of a mutable rational.- Parameters:
r- the original rational.
-
-
Method Details
-
setValue
Set the value of this rational to value.- Parameters:
value- the value to set to.
-
setValue
public void setValue(long newnum, long newdenom)Set the value of this rational to newnum/newdenom.- Parameters:
newnum- the new numerator.newdenom- the new denominator.
-
add
Add the rational other to this rational.- Parameters:
other- the rational to add.- Returns:
- this mutable rational.
-
negate
Negate this rational, i.e., this = -this.- Returns:
- this mutable rational.
-
sub
Subtract the other rational from this.- Parameters:
other- the rational to subtract.- Returns:
- this mutable rational.
-
mul
Multiply this rational with other and store the result in this.- Parameters:
other- the rational to multiply with.- Returns:
- this mutable rational.
-
div
Divide this rational by the other and store the result in this.- Parameters:
other- the divisor.- Returns:
- this mutable rational.
-
inverse
Compute the multiplicative inverse of this rational and store it in this.- Returns:
- this mutable rational.
-
isNegative
public boolean isNegative()Check if this rational is negative.- Returns:
- true iff
this < 0.
-
addmul
Computesthis += (fac1*fac2).- Parameters:
fac1- one of the factors.fac2- the other factor.- Returns:
- this mutable rational.
-
addmul
Computesthis += (fac1*fac2).- Parameters:
fac1- one of the factors.fac2- the other factor.- Returns:
- this mutable rational.
-
subdiv
Computesthis = (this - s) / d.- Parameters:
s- the rational to subtract.d- the divisor.- Returns:
- this mutable rational.
-
compareTo
- Specified by:
compareToin interfacejava.lang.Comparable<MutableRational>
-
compareTo
Compares this mutable rational with the other.- Parameters:
o- the other rational.- Returns:
- -1, if this < o; 1, if this > o; 0 if they are equal.
-
equals
public boolean equals(java.lang.Object o)Compares this mutable rational with the other. This works with MutableRational and Rational.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- the other rational.- Returns:
- true if this equals o, false otherwise.
-
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.
-
toRational
Convert this mutable rational into a immutable rational.- Returns:
- the rational.
-
signum
public int signum()Compute sign of this rational. This is equivalent tocompare(Rational.ZERO).- Returns:
- the sign of the rational, +1 for positive, 0 for 0 and -1 for negative.
-