Package it.unich.jgmp
Class MPQ
- java.lang.Object
-
- java.lang.Number
-
- it.unich.jgmp.MPQ
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<MPQ>
public class MPQ extends java.lang.Number implements java.lang.Comparable<MPQ>
Multi-precision rational numbers. This class encapsulates thempq_t
data type, see the Rational Number Functions page of the GMP manual. In determining the names and signatures of the methods of theMPQ
class, we adopt the rules described in the documentation of theit.unich.jgmp
package, enriched with the following ones:- the function
mpq_canonicalize
is only used internally; - the functions in the category I/O of Rationals, and the macros
mpq_numref
andmpq_denref
are not exposed by theMPQ
class.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MPQ.MPQCleaner
Cleaning action for theMPQ
class.
-
Field Summary
Fields Modifier and Type Field Description private MpqT
mpqNative
The pointer to the nativempq_t
object.private static long
serialVersionUID
Version for serializability.
-
Constructor Summary
Constructors Modifier Constructor Description MPQ()
Build anMPQ
whose value is zero.MPQ(double op)
Build anMPQ
whose value isop
.MPQ(long num)
Build anMPQ
whose value isop
.MPQ(long num, long dem)
Build anMPQ
whose value is(num/dem)
.MPQ(MPF op)
Build anMPQ
whose value isop
.MPQ(MPQ op)
Build anMPQ
whose value isop
.MPQ(MPZ op)
Build anMPQ
whose value isop
.private
MPQ(MpqT pointer)
A private constructor which build anMPQ
starting from a pointer to its native data object.MPQ(java.lang.String str)
Build anMPQ
whose value is the number represented by the stringstr
in decimal base.MPQ(java.lang.String str, int base)
Build anMPQ
whose value is the number represented by the stringstr
in the specifiedbase
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MPQ
abs()
Return anMPQ
whose value is the absolute value ofthis
.MPQ
absAssign()
Set thisMPQ
to its absolute value.MPQ
absAssign(MPQ op)
Set thisMPQ
to the absolute value ofop
.MPQ
add(MPQ op)
Return anMPQ
whose value is(this + op)
.MPQ
addAssign(MPQ op)
Set thisMPQ
to(this + op)
MPQ
addAssign(MPQ op1, MPQ op2)
Set thisMPQ
to(op1 + op2)
.int
cmp(long num, long den)
Comparethis
with(num/dem)
.int
cmp(MPQ op)
Comparethis
withop
.int
cmp(MPZ op)
Comparethis
withop
.int
cmpUi(long num, long den)
Comparethis
with(num/dem)
.int
compareTo(MPQ op)
Compare thisMPQ
withop
.MPQ
div(MPQ op)
Return anMPQ
whose value is(this / op)
.MPQ
div2Exp(long b)
Return anMPQ
whose value is(this / 2b)
.MPQ
div2ExpAssign(long b)
Set thisMPQ
to(this / 2b)
.MPQ
div2ExpAssign(MPQ op, long b)
Set thisMPQ
to(op / 2b)
.MPQ
divAssign(MPQ op)
Set thisMPQ
to(this / op)
MPQ
divAssign(MPQ op1, MPQ op2)
Set thisMPQ
to(op1 / op2)
.double
doubleValue()
Convert thisMPQ
to a double, truncating if necessary.boolean
equal(MPQ op)
Return true ifthis
is equal toop
, false otherwise.boolean
equals(java.lang.Object obj)
Compare thisMPQ
with the objectop
for equality.float
floatValue()
Convert thisMPQ
to a float, truncating if necessary.double
getD()
Convert thisMPQ
to a double, truncating if necessary.MPZ
getDen()
Return the denominator ofthis
.MpqT
getNative()
Return the native pointer to the GMP object.MPZ
getNum()
Return the numerator ofthis
.java.lang.String
getStr(int base)
Return the String representation of thisMPQ
in the specifiedbase
, ornull
if the base is not valid.int
hashCode()
Return a hash code value for thisMPQ
.static MPQ
init()
Return anMPQ
whose value is zero.int
intValue()
Convert thisMPQ
to an int, truncating if necessary.MPQ
inv()
Return anMPQ
whose value is(1/this)
.MPQ
invAssign()
Set thisMPQ
to its inverse.MPQ
invAssign(MPQ op)
Set thisMPQ
to1/op
.boolean
isZero()
Return true if and only ifthis
MPQ is zero.long
longValue()
Convert thisMPQ
to an long, truncating if necessary.MPQ
mul(MPQ op)
Return anMPQ
whose value is(this * op)
.MPQ
mul2Exp(long b)
Return anMPQ
whose value is(this * 2b)
.MPQ
mul2ExpAssign(long b)
Set thisMPQ
to(this * 2b)
.MPQ
mul2ExpAssign(MPQ op, long b)
Set thisMPQ
to(op * 2b)
.MPQ
mulAssign(MPQ op)
Set thisMPQ
to(this * op)
MPQ
mulAssign(MPQ op1, MPQ op2)
Set thisMPQ
to(op1 * op2)
.MPQ
neg()
Return anMPQ
whose value is the quotient of(- this)
.MPQ
negAssign()
Set thisMPQ
to its opposite.MPQ
negAssign(MPQ op)
Set thisMPQ
to(- op)
.private void
readObject(java.io.ObjectInputStream in)
private void
readObjectNoData()
MPQ
set(double op)
Set thisMPQ
toop
.MPQ
set(long num, long den)
Set thisMPQ
to(<num/>den)
.MPQ
set(MPF op)
Set thisMPQ
toop
.MPQ
set(MPQ op)
Set thisMPQ
toop
.MPQ
set(MPZ op)
Set thisMPQ
toop
.int
set(java.lang.String str, int base)
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
.MPQ
setDen(MPZ den)
Set the denominator ofthis
to the valueden
.MPQ
setNum(MPZ num)
Set the numerator ofthis
to the valuenum
.MPQ
setUi(long num, long den)
Set thisMPQ
to(num/den)
.MPQ
setValue(double op)
Set thisMPQ
to opop
.MPQ
setValue(long op)
Set thisMPQ
toop
.MPQ
setValue(MPF op)
Set thisMPQ
toop
.MPQ
setValue(MPQ op)
Set thisMPQ
toop
.MPQ
setValue(java.lang.String str)
Set thisMPQ
to the value represented by the stringstr
in decimal base.MPQ
setValue(java.lang.String str, int base)
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
.int
sgn()
Return+1
if(this > 0)
,0
if(this = 0)
and-1
if(this < 0)
.MPQ
sub(MPQ op)
Return anMPQ
whose value is(this - op)
.MPQ
subAssign(MPQ op)
Set thisMPQ
to(this - op)
MPQ
subAssign(MPQ op1, MPQ op2)
Set thisMPQ
to(op1 - op2)
.MPQ
swap(MPQ op)
Swap the value of thisMPQ
with the value ofop
.java.lang.String
toString()
Convert thisMPQ
to its decimal string representation.java.lang.String
toString(int base)
Convert thisMPQ
to its string representation in the specifiedbase
, ornull
if the base is not valid.private void
writeObject(java.io.ObjectOutputStream out)
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Version for serializability.- See Also:
- Constant Field Values
-
mpqNative
private transient MpqT mpqNative
The pointer to the nativempq_t
object.
-
-
Constructor Detail
-
MPQ
private MPQ(MpqT pointer)
A private constructor which build anMPQ
starting from a pointer to its native data object. The native object needs to be already initialized.
-
MPQ
public MPQ()
Build anMPQ
whose value is zero.
-
MPQ
public MPQ(MPQ op)
Build anMPQ
whose value isop
.
-
MPQ
public MPQ(MPZ op)
Build anMPQ
whose value isop
.
-
MPQ
public MPQ(long num, long dem)
Build anMPQ
whose value is(num/dem)
.
-
MPQ
public MPQ(long num)
Build anMPQ
whose value isop
.
-
MPQ
public MPQ(double op)
Build anMPQ
whose value isop
. There is no rounding, this conversion is exact.- Throws:
java.lang.ArithmeticException
- ifop
is not a finite number.
-
MPQ
public MPQ(MPF op)
Build anMPQ
whose value isop
. There is no rounding, this conversion is exact.
-
MPQ
public MPQ(java.lang.String str, int base)
Build anMPQ
whose value is the number represented by the stringstr
in the specifiedbase
. See the GMP functionmpq_set_str
.- Throws:
java.lang.NumberFormatException
- if eitherbase
is not valid orstr
is not a valid string in the specifiedbase
.
-
MPQ
public MPQ(java.lang.String str)
Build anMPQ
whose value is the number represented by the stringstr
in decimal base. See the GMP functionmpq_set_str
.- Throws:
java.lang.NumberFormatException
- ifstr
is not a valid number representation in decimal base.
-
-
Method Detail
-
getNative
public MpqT getNative()
Return the native pointer to the GMP object.
-
init
public static MPQ init()
Return anMPQ
whose value is zero.
-
set
public MPQ set(long num, long den)
Set thisMPQ
to(<num/>den)
.- Returns:
- this
MPQ
.
-
setUi
public MPQ setUi(long num, long den)
Set thisMPQ
to(num/den)
.- Returns:
- this
MPQ
.
-
set
public int set(java.lang.String str, int base)
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
. See the GMP functionmpq_set_str
.- Returns:
- 0 if the operation succeeded, -1 otherwise. In the latter case,
this
is not altered.
-
getD
public double getD()
Convert thisMPQ
to a double, truncating if necessary. If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur.
-
set
public MPQ set(double op)
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
. - Throws:
java.lang.ArithmeticException
- ifop
is not a finite number. In this case,this
is not altered.
-
set
public MPQ set(MPF op)
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
.
-
getStr
public java.lang.String getStr(int base)
Return the String representation of thisMPQ
in the specifiedbase
, ornull
if the base is not valid. See the GMP functionmpq_get_str
.
-
divAssign
public MPQ divAssign(MPQ op1, MPQ op2)
Set thisMPQ
to(op1 / op2)
.- Returns:
- this
MPQ
. - Throws:
java.lang.ArithmeticException
- ifop2
is zero.
-
divAssign
public MPQ divAssign(MPQ op)
Set thisMPQ
to(this / op)
- Returns:
- this
MPQ
- Throws:
java.lang.ArithmeticException
- ifop
is zero.
-
div
public MPQ div(MPQ op)
Return anMPQ
whose value is(this / op)
.- Throws:
java.lang.ArithmeticException
- ifop
is zero.
-
mul2ExpAssign
public MPQ mul2ExpAssign(MPQ op, long b)
Set thisMPQ
to(op * 2b)
.- Returns:
- this
MPQ
.
-
mul2ExpAssign
public MPQ mul2ExpAssign(long b)
Set thisMPQ
to(this * 2b)
.- Returns:
- this
MPQ
.
-
mul2Exp
public MPQ mul2Exp(long b)
Return anMPQ
whose value is(this * 2b)
.
-
div2ExpAssign
public MPQ div2ExpAssign(MPQ op, long b)
Set thisMPQ
to(op / 2b)
.- Returns:
- this
MPQ
.
-
div2ExpAssign
public MPQ div2ExpAssign(long b)
Set thisMPQ
to(this / 2b)
.- Returns:
- this
MPQ
.
-
div2Exp
public MPQ div2Exp(long b)
Return anMPQ
whose value is(this / 2b)
.
-
negAssign
public MPQ negAssign()
Set thisMPQ
to its opposite.- Returns:
- this
MPQ
.
-
neg
public MPQ neg()
Return anMPQ
whose value is the quotient of(- this)
.
-
absAssign
public MPQ absAssign(MPQ op)
Set thisMPQ
to the absolute value ofop
.- Returns:
- this
MPQ
.
-
absAssign
public MPQ absAssign()
Set thisMPQ
to its absolute value.- Returns:
- this
MPQ
.
-
abs
public MPQ abs()
Return anMPQ
whose value is the absolute value ofthis
.
-
invAssign
public MPQ invAssign(MPQ op)
Set thisMPQ
to1/op
.- Returns:
- this
MPQ
. - Throws:
java.lang.ArithmeticException
- ifop
is zero.
-
invAssign
public MPQ invAssign()
Set thisMPQ
to its inverse.- Returns:
- this
MPQ
. - Throws:
java.lang.ArithmeticException
- ifop
is zero.
-
inv
public MPQ inv()
Return anMPQ
whose value is(1/this)
.- Throws:
java.lang.ArithmeticException
- ifop
is zero.
-
cmp
public int cmp(MPQ op)
Comparethis
withop
. Return a positive value if(this > op)
, zero if(this = op)
, or a negative value if(this < op)
.
-
cmp
public int cmp(MPZ op)
Comparethis
withop
. Return a positive value if(this > op)
, zero ifthis = op
, or a negative value ifthis < op
.
-
cmp
public int cmp(long num, long den)
Comparethis
with(num/dem)
. Return a positive value if(this > num/dem)
, zero if(this = num/dem)
, or a negative value if(this < num/dem)
.
-
cmpUi
public int cmpUi(long num, long den)
Comparethis
with(num/dem)
. Return a positive value if(this > num/dem)
, zero if(this = num/dem)
, or a negative value if(this < num/dem)
.
-
sgn
public int sgn()
Return+1
if(this > 0)
,0
if(this = 0)
and-1
if(this < 0)
.
-
equal
public boolean equal(MPQ op)
Return true ifthis
is equal toop
, false otherwise. Althoughcmp
can be used for the same purpose, this method should be faster.
-
getNum
public MPZ getNum()
Return the numerator ofthis
.
-
getDen
public MPZ getDen()
Return the denominator ofthis
.
-
isZero
public boolean isZero()
Return true if and only ifthis
MPQ is zero.
-
setValue
public MPQ setValue(long op)
Set thisMPQ
toop
.- Returns:
- this
MPQ
.
-
setValue
public MPQ setValue(double op)
Set thisMPQ
to opop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
. - Throws:
java.lang.ArithmeticException
- ifop
is not a finite number. In this case,this
is not altered.
-
setValue
public MPQ setValue(MPF op)
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
.
-
setValue
public MPQ setValue(java.lang.String str, int base)
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
. See the GMP functionmpq_set_str
.- Returns:
- this
MPQ
. - Throws:
java.lang.NumberFormatException
- if eitherbase
is not valid orstr
is not a valid number representation in the specified base. In this case,this
is not altered.
-
setValue
public MPQ setValue(java.lang.String str)
Set thisMPQ
to the value represented by the stringstr
in decimal base. See the GMP functionmpq_set_str
.- Returns:
- this
MPQ
. - Throws:
java.lang.NumberFormatException
- ifstr
is not a valid number representation in decimal base.
-
compareTo
public int compareTo(MPQ op)
Compare thisMPQ
withop
. Return a positive value if(this > op)
, zero if(this = op)
, or a negative value if(this < op)
. This order is compatible with equality.- Specified by:
compareTo
in interfacejava.lang.Comparable<MPQ>
-
equals
public boolean equals(java.lang.Object obj)
Compare thisMPQ
with the objectop
for equality. It returnstrue
if and only ifop
is anMPQ
with the same value ofthis
.- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
Return a hash code value for thisMPQ
.- Overrides:
hashCode
in classjava.lang.Object
-
intValue
public int intValue()
Convert thisMPQ
to an int, truncating if necessary.- Specified by:
intValue
in classjava.lang.Number
-
longValue
public long longValue()
Convert thisMPQ
to an long, truncating if necessary.- Specified by:
longValue
in classjava.lang.Number
-
doubleValue
public double doubleValue()
Convert thisMPQ
to a double, truncating if necessary. If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur.- Specified by:
doubleValue
in classjava.lang.Number
-
floatValue
public float floatValue()
Convert thisMPQ
to a float, truncating if necessary.- Specified by:
floatValue
in classjava.lang.Number
-
toString
public java.lang.String toString(int base)
Convert thisMPQ
to its string representation in the specifiedbase
, ornull
if the base is not valid. See the GMP functionmpq_get_str
.- Throws:
java.lang.IllegalArgumentException
- if the base is not valid.
-
toString
public java.lang.String toString()
Convert thisMPQ
to its decimal string representation.- Overrides:
toString
in classjava.lang.Object
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
readObjectNoData
private void readObjectNoData() throws java.io.ObjectStreamException
- Throws:
java.io.ObjectStreamException
-
-