Class Mpq
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Mpq>
This class wraps mpq_t GMP objects.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMpq()Constructs a rational initialized to 0/1.Mpq(double d) Constructs a rational equal to the double d.Mpq(int op) Constructs a rational equal to the integer op.Mpq(int num, int den) Constructs a rational equal to num/den.Constructs a copy of op.Constructs a rational equal to the integer op.Constructs a rational equal to num/den.Constructs a rational from the String s.Constructs a rational from the String s expressed in base b.Mpq(BigInteger op) Constructs a rational equal to the integer op.Mpq(BigInteger num, BigInteger den) Constructs a rational equal to num/den. -
Method Summary
Modifier and TypeMethodDescriptionvoidabs()Replaces this with |this|.voidStores |arg| into this.voidAdds arg to this.voidStores arg1+arg2 into this.voidPuts the fraction into normal form.clone()Creates a new Mpq initialized with the value of this.intcmp(int num, int den) Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than num/den.intReturns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.intCompare the value of this and x.denRef()Returns a reference to the denominator of this.voidDivides this by arg.voidStores arg1/arg2 into this.voiddiv2exp(int arg) Divides this by (2^arg).voidStores arg1/(2^arg2) into this.voidStores arg/this into this.doubleReturns this as a double.booleanWhether x is a Mpq and has the same value as this.getDen()Returns a copy of the denominator of this.getNum()Returns a copy of the numerator of this.inthashCode()Returns a hash of the value of this.voidinv()Replaces this with 1/this.voidStores 1/arg into this.booleanWhether this equals op.booleanWhether this and x have the same address in memory.voidMultiply this by arg.voidStores arg1*arg2 into this.voidmul2exp(int arg) Multiply this by 2^arg.voidStores arg1*(2^arg2) into this.voidneg()Replaces this with -this.voidStores -arg into this.numRef()Returns a reference to the numerator of this.voidset(double d) Sets this to the double number.voidset(int op) Sets this to the integer op.voidset(int num, int den) Sets this to num/den.voidCopies the argument into this.voidSets this to the integer op.voidSets this to num/den.voidSets this to the String s.voidSets this to the String s expressed in base b.voidset(BigInteger op) Sets this to the integer op.voidset(BigInteger num, BigInteger den) Sets this to num/den.voidsetDen(int op) Changes the denominator of this.voidChanges the denominator of this.voidsetDen(BigInteger op) Changes the denominator of this.voidsetNum(int op) Changes the numerator of this.voidChanges the numerator of this.voidsetNum(BigInteger op) Changes the numerator of this.intsgn()Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.intsize()Returns the size (in GMP limbs) of the internal representation of this.voidSubtracts arg from this.voidStores arg1-arg2 into this.voidReplaces this with arg-this.toString()Returns a String representation of this in base 10.toString(int b) Returns a String representation of this in base b.
-
Constructor Details
-
Mpq
public Mpq()Constructs a rational initialized to 0/1. -
Mpq
Constructs a copy of op. -
Mpq
Constructs a rational equal to the integer op. -
Mpq
Constructs a rational equal to the integer op. -
Mpq
public Mpq(int op) Constructs a rational equal to the integer op. -
Mpq
public Mpq(double d) Constructs a rational equal to the double d.There is no rounding, the conversion is exact.
-
Mpq
Constructs a rational from the String s expressed in base b.The String can have a fraction / symbol.
The result is canonicalized.
- See Also:
-
Mpq
Constructs a rational from the String s.The String can have a fraction / symbol.
The result is canonicalized.
- See Also:
-
Mpq
Constructs a rational equal to num/den.The result is canonicalized.
-
Mpq
Constructs a rational equal to num/den.The result is canonicalized.
-
Mpq
public Mpq(int num, int den) Constructs a rational equal to num/den.The result is canonicalized.
-
-
Method Details
-
canonicalize
public void canonicalize()Puts the fraction into normal form.Removes common factors in the numerator and denominator, and makes the denominator positive.
All arithmetic functions assume and return canonical fractions. However, pure assignment functions (including separate assignments of denominators and numerators, directly or through a
MpzRefreference) do not canonicalize the assigned object, and so, the function must be called by the user before arithmetic operations can be performed. -
clone
Creates a new Mpq initialized with the value of this.- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
set
Copies the argument into this. -
set
Sets this to the integer op. -
set
Sets this to the integer op. -
set
public void set(int op) Sets this to the integer op. -
set
Sets this to num/den.canonicalize()must be called manually if the fraction is not canonical. -
set
Sets this to num/den.canonicalize()must be called manually if the fraction is not canonical. -
set
public void set(int num, int den) Sets this to num/den.canonicalize()must be called manually if the fraction is not canonical. -
set
public void set(double d) Sets this to the double number.There is no rounding: the conversion is exact.
-
set
Sets this to the String s expressed in base b.The String can have a fraction / symbol.
canonicalize()must be called manually if the fraction is not canonical.- See Also:
-
set
Sets this to the String s.The String can have a fraction / symbol.
canonicalize()must be called manually if the fraction is not canonical.- See Also:
-
setNum
Changes the numerator of this.canonicalize()must be called manually if the fraction is not canonical. -
setNum
public void setNum(int op) Changes the numerator of this.canonicalize()must be called manually if the fraction is not canonical. -
setNum
Changes the numerator of this.canonicalize()must be called manually if the fraction is not canonical. -
setDen
Changes the denominator of this.canonicalize()must be called manually if the fraction is not canonical. -
setDen
public void setDen(int op) Changes the denominator of this.canonicalize()must be called manually if the fraction is not canonical. -
setDen
Changes the denominator of this.canonicalize()must be called manually if the fraction is not canonical. -
doubleValue
public double doubleValue()Returns this as a double.If this cannot be represented as a double, the result is truncated.
Overflows cause infinities to be returned.
-
toString
Returns a String representation of this in base b.- Parameters:
b- must be between 2 and 62.
-
toString
Returns a String representation of this in base 10. -
getNum
Returns a copy of the numerator of this. -
getDen
Returns a copy of the denominator of this. -
numRef
Returns a reference to the numerator of this. -
denRef
Returns a reference to the denominator of this. -
add
Stores arg1+arg2 into this. -
add
Adds arg to this. -
sub
Stores arg1-arg2 into this. -
sub
Subtracts arg from this. -
subRev
Replaces this with arg-this. -
mul
Stores arg1*arg2 into this. -
mul
Multiply this by arg. -
mul2exp
Stores arg1*(2^arg2) into this. -
mul2exp
public void mul2exp(int arg) Multiply this by 2^arg. -
div
Stores arg1/arg2 into this. -
div
Divides this by arg. -
divRev
Stores arg/this into this. -
div2exp
Stores arg1/(2^arg2) into this. -
div2exp
public void div2exp(int arg) Divides this by (2^arg). -
neg
Stores -arg into this. -
neg
public void neg()Replaces this with -this. -
abs
Stores |arg| into this. -
abs
public void abs()Replaces this with |this|. -
inv
Stores 1/arg into this. -
inv
public void inv()Replaces this with 1/this. -
size
public int size()Returns the size (in GMP limbs) of the internal representation of this. -
cmp
Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op. -
cmp
public int cmp(int num, int den) Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than num/den.den must be positive.
-
sgn
public int sgn()Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative. -
isEqual
Whether this equals op.This method is faster than calling cmp, as fractions are assumed to be canonical.
-
equals
Whether x is a Mpq and has the same value as this. -
compareTo
Compare the value of this and x.- Specified by:
compareToin interfaceComparable<Mpq>
-
hashCode
public int hashCode()Returns a hash of the value of this. -
isPhysicalEqual
Whether this and x have the same address in memory.
-