public class MutableRational extends java.lang.Object implements java.lang.Comparable<MutableRational>
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.Constructor and Description |
---|
MutableRational(java.math.BigInteger num,
java.math.BigInteger denom)
Create a new rational representing num/denom.
|
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.
|
Modifier and Type | Method and Description |
---|---|
MutableRational |
add(Rational other)
Add the rational other to this rational.
|
MutableRational |
addmul(Rational fac1,
java.math.BigInteger fac2)
Computes
this += (fac1*fac2) . |
MutableRational |
addmul(Rational fac1,
Rational fac2)
Computes
this += (fac1*fac2) . |
int |
compareTo(MutableRational o) |
int |
compareTo(Rational o)
Compares this mutable rational with the other.
|
java.math.BigInteger |
denominator()
Get the denominator of this rational.
|
MutableRational |
div(Rational other)
Divide this rational by the other and store the result in this.
|
boolean |
equals(java.lang.Object o)
Compares this mutable rational with the other.
|
int |
hashCode()
Computes a hashcode.
|
MutableRational |
inverse()
Compute the multiplicative inverse of this rational and store
it in this.
|
boolean |
isIntegral()
Check whether this rational represents an integral value.
|
boolean |
isNegative()
Check if this rational is negative.
|
MutableRational |
mul(Rational other)
Multiply this rational with other and store the result in this.
|
MutableRational |
negate()
Negate this rational, i.e., this = -this.
|
java.math.BigInteger |
numerator()
Get the numerator of this rational.
|
void |
setValue(long newnum,
long newdenom)
Set the value of this rational to newnum/newdenom.
|
void |
setValue(Rational value)
Set the value of this rational to value.
|
int |
signum()
Compute sign of this rational.
|
MutableRational |
sub(Rational other)
Subtract the other rational from this.
|
MutableRational |
subdiv(Rational s,
Rational d)
Computes
this = (this - s) / d . |
Rational |
toRational()
Convert this mutable rational into a immutable rational.
|
java.lang.String |
toString()
Get a string representation of this number.
|
public MutableRational(int num, int denom)
num
- the numerator.denom
- the denominator.public MutableRational(java.math.BigInteger num, java.math.BigInteger denom)
num
- the numerator.denom
- the denominator.public MutableRational(Rational r)
r
- the rational.public MutableRational(MutableRational r)
r
- the original rational.public void setValue(Rational value)
value
- the value to set to.public void setValue(long newnum, long newdenom)
newnum
- the new numerator.newdenom
- the new denominator.public MutableRational add(Rational other)
other
- the rational to add.public MutableRational negate()
public MutableRational sub(Rational other)
other
- the rational to subtract.public MutableRational mul(Rational other)
other
- the rational to multiply with.public MutableRational div(Rational other)
other
- the divisor.public MutableRational inverse()
public boolean isNegative()
this < 0
.public MutableRational addmul(Rational fac1, Rational fac2)
this += (fac1*fac2)
.fac1
- one of the factors.fac2
- the other factor.public MutableRational addmul(Rational fac1, java.math.BigInteger fac2)
this += (fac1*fac2)
.fac1
- one of the factors.fac2
- the other factor.public MutableRational subdiv(Rational s, Rational d)
this = (this - s) / d
.s
- the rational to subtract.d
- the divisor.public int compareTo(MutableRational o)
compareTo
in interface java.lang.Comparable<MutableRational>
public int compareTo(Rational o)
o
- the other rational.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- the other rational.public java.math.BigInteger numerator()
public java.math.BigInteger denominator()
public int hashCode()
257 * numerator + denominator
if both fit into an integer and
257 * numerator().hashCode() + denominator().hashCode()
if big
integers are necessary.hashCode
in class java.lang.Object
public java.lang.String toString()
numerator()+ "/" + denominator()
except for
infinity ("inf"
), nan ("nan"
), or minus
infinity ("-inf"
).toString
in class java.lang.Object
public boolean isIntegral()
true
iff value is integral.public Rational toRational()
public int signum()
compare(Rational.ZERO)
.