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 TypeMethodDescriptionvoid
abs()
Replaces this with |this|.void
Stores |arg| into this.void
Adds arg to this.void
Stores arg1+arg2 into this.void
Puts the fraction into normal form.clone()
Creates a new Mpq initialized with the value of this.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.int
Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.int
Compare the value of this and x.denRef()
Returns a reference to the denominator of this.void
Divides this by arg.void
Stores arg1/arg2 into this.void
div2exp
(int arg) Divides this by (2^arg).void
Stores arg1/(2^arg2) into this.void
Stores arg/this into this.double
Returns this as a double.boolean
Whether 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.int
hashCode()
Returns a hash of the value of this.void
inv()
Replaces this with 1/this.void
Stores 1/arg into this.boolean
Whether this equals op.boolean
Whether this and x have the same address in memory.void
Multiply this by arg.void
Stores arg1*arg2 into this.void
mul2exp
(int arg) Multiply this by 2^arg.void
Stores arg1*(2^arg2) into this.void
neg()
Replaces this with -this.void
Stores -arg into this.numRef()
Returns a reference to the numerator of this.void
set
(double d) Sets this to the double number.void
set
(int op) Sets this to the integer op.void
set
(int num, int den) Sets this to num/den.void
Copies the argument into this.void
Sets this to the integer op.void
Sets this to num/den.void
Sets this to the String s.void
Sets this to the String s expressed in base b.void
set
(BigInteger op) Sets this to the integer op.void
set
(BigInteger num, BigInteger den) Sets this to num/den.void
setDen
(int op) Changes the denominator of this.void
Changes the denominator of this.void
setDen
(BigInteger op) Changes the denominator of this.void
setNum
(int op) Changes the numerator of this.void
Changes the numerator of this.void
setNum
(BigInteger op) Changes the numerator of this.int
sgn()
Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.int
size()
Returns the size (in GMP limbs) of the internal representation of this.void
Subtracts arg from this.void
Stores arg1-arg2 into this.void
Replaces 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
MpzRef
reference) 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:
clone
in 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:
compareTo
in 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.
-