Package it.unich.jgmp
Class MPQ
java.lang.Object
java.lang.Number
it.unich.jgmp.MPQ
- All Implemented Interfaces:
Serializable
,Comparable<MPQ>
Multi-precision rational numbers. This class encapsulates the
mpq_t
data type, see the
Rational Number Functions page of the GMP manual. In determining
the names and signatures of the methods of the MPQ
class, we adopt
the rules described in the documentation of the it.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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Cleaning action for theMPQ
class. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MpqT
The pointer to the nativempq_t
object.private static final long
Version for serializability. -
Constructor Summary
ConstructorsModifierConstructorDescriptionMPQ()
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)
.Build anMPQ
whose value isop
.Build anMPQ
whose value isop
.Build anMPQ
whose value isop
.private
A private constructor which build anMPQ
starting from a pointer to its native data object.Build anMPQ
whose value is the number represented by the stringstr
in decimal base.Build anMPQ
whose value is the number represented by the stringstr
in the specifiedbase
. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Return anMPQ
whose value is the absolute value ofthis
.Set thisMPQ
to its absolute value.Set thisMPQ
to the absolute value ofop
.Return anMPQ
whose value is(this + op)
.Set thisMPQ
to(this + op)
Set thisMPQ
to(op1 + op2)
.int
cmp
(long num, long den) Comparethis
with(num/dem)
.int
Comparethis
withop
.int
Comparethis
withop
.int
cmpUi
(long num, long den) Comparethis
with(num/dem)
.int
Compare thisMPQ
withop
.Return anMPQ
whose value is(this / op)
.div2Exp
(long b) Return anMPQ
whose value is(this / 2b)
.div2ExpAssign
(long b) Set thisMPQ
to(this / 2b)
.div2ExpAssign
(MPQ op, long b) Set thisMPQ
to(op / 2b)
.Set thisMPQ
to(this / op)
Set thisMPQ
to(op1 / op2)
.double
Convert thisMPQ
to a double, truncating if necessary.boolean
Return true ifthis
is equal toop
, false otherwise.boolean
Compare thisMPQ
with the objectop
for equality.float
Convert thisMPQ
to a float, truncating if necessary.double
getD()
Convert thisMPQ
to a double, truncating if necessary.getDen()
Return the denominator ofthis
.Return the native pointer to the GMP object.getNum()
Return the numerator ofthis
.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.inv()
Return anMPQ
whose value is(1/this)
.Set thisMPQ
to its inverse.Set thisMPQ
to1/op
.boolean
isZero()
Return true if and only ifthis
MPQ is zero.long
Convert thisMPQ
to an long, truncating if necessary.Return anMPQ
whose value is(this * op)
.mul2Exp
(long b) Return anMPQ
whose value is(this * 2b)
.mul2ExpAssign
(long b) Set thisMPQ
to(this * 2b)
.mul2ExpAssign
(MPQ op, long b) Set thisMPQ
to(op * 2b)
.Set thisMPQ
to(this * op)
Set thisMPQ
to(op1 * op2)
.neg()
Return anMPQ
whose value is the quotient of(- this)
.Set thisMPQ
to its opposite.Set thisMPQ
to(- op)
.private void
private void
set
(double op) Set thisMPQ
toop
.set
(long num, long den) Set thisMPQ
to(<num/>den)
.Set thisMPQ
toop
.Set thisMPQ
toop
.Set thisMPQ
toop
.int
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
.Set the denominator ofthis
to the valueden
.Set the numerator ofthis
to the valuenum
.setUi
(long num, long den) Set thisMPQ
to(num/den)
.setValue
(double op) Set thisMPQ
to opop
.setValue
(long op) Set thisMPQ
toop
.Set thisMPQ
toop
.Set thisMPQ
toop
.Set thisMPQ
to the value represented by the stringstr
in decimal 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)
.Return anMPQ
whose value is(this - op)
.Set thisMPQ
to(this - op)
Set thisMPQ
to(op1 - op2)
.Swap the value of thisMPQ
with the value ofop
.toString()
Convert thisMPQ
to its decimal string representation.toString
(int base) Convert thisMPQ
to its string representation in the specifiedbase
, ornull
if the base is not valid.private void
Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDVersion for serializability.- See Also:
-
mpqNative
The pointer to the nativempq_t
object.
-
-
Constructor Details
-
MPQ
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
Build anMPQ
whose value isop
. -
MPQ
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:
ArithmeticException
- ifop
is not a finite number.
-
MPQ
Build anMPQ
whose value isop
. There is no rounding, this conversion is exact. -
MPQ
Build anMPQ
whose value is the number represented by the stringstr
in the specifiedbase
. See the GMP functionmpq_set_str
.- Throws:
NumberFormatException
- if eitherbase
is not valid orstr
is not a valid string in the specifiedbase
.
-
MPQ
Build anMPQ
whose value is the number represented by the stringstr
in decimal base. See the GMP functionmpq_set_str
.- Throws:
NumberFormatException
- ifstr
is not a valid number representation in decimal base.
-
-
Method Details
-
getNative
Return the native pointer to the GMP object. -
init
Return anMPQ
whose value is zero. -
set
Set thisMPQ
toop
.- Returns:
- this
MPQ
.
-
set
Set thisMPQ
toop
.- Returns:
- this
MPQ
.
-
set
Set thisMPQ
to(<num/>den)
.- Returns:
- this
MPQ
.
-
setUi
Set thisMPQ
to(num/den)
.- Returns:
- this
MPQ
.
-
set
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.
-
swap
Swap the value of thisMPQ
with the value ofop
.- Returns:
- this
MPQ
.
-
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
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
. - Throws:
ArithmeticException
- ifop
is not a finite number. In this case,this
is not altered.
-
set
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
.
-
getStr
Return the String representation of thisMPQ
in the specifiedbase
, ornull
if the base is not valid. See the GMP functionmpq_get_str
. -
addAssign
Set thisMPQ
to(op1 + op2)
.- Returns:
- this
MPQ
.
-
addAssign
Set thisMPQ
to(this + op)
- Returns:
- this
MPQ
-
add
Return anMPQ
whose value is(this + op)
. -
subAssign
Set thisMPQ
to(op1 - op2)
.- Returns:
- this
MPQ
.
-
subAssign
Set thisMPQ
to(this - op)
- Returns:
- this
MPQ
-
sub
Return anMPQ
whose value is(this - op)
. -
mulAssign
Set thisMPQ
to(op1 * op2)
.- Returns:
- this
MPQ
.
-
mulAssign
Set thisMPQ
to(this * op)
- Returns:
- this
MPQ
-
mul
Return anMPQ
whose value is(this * op)
. -
divAssign
Set thisMPQ
to(op1 / op2)
.- Returns:
- this
MPQ
. - Throws:
ArithmeticException
- ifop2
is zero.
-
divAssign
Set thisMPQ
to(this / op)
- Returns:
- this
MPQ
- Throws:
ArithmeticException
- ifop
is zero.
-
div
Return anMPQ
whose value is(this / op)
.- Throws:
ArithmeticException
- ifop
is zero.
-
mul2ExpAssign
Set thisMPQ
to(op * 2b)
.- Returns:
- this
MPQ
.
-
mul2ExpAssign
Set thisMPQ
to(this * 2b)
.- Returns:
- this
MPQ
.
-
mul2Exp
Return anMPQ
whose value is(this * 2b)
. -
div2ExpAssign
Set thisMPQ
to(op / 2b)
.- Returns:
- this
MPQ
.
-
div2ExpAssign
Set thisMPQ
to(this / 2b)
.- Returns:
- this
MPQ
.
-
div2Exp
Return anMPQ
whose value is(this / 2b)
. -
negAssign
Set thisMPQ
to(- op)
.- Returns:
- this
MPQ
.
-
negAssign
Set thisMPQ
to its opposite.- Returns:
- this
MPQ
.
-
neg
Return anMPQ
whose value is the quotient of(- this)
. -
absAssign
Set thisMPQ
to the absolute value ofop
.- Returns:
- this
MPQ
.
-
absAssign
Set thisMPQ
to its absolute value.- Returns:
- this
MPQ
.
-
abs
Return anMPQ
whose value is the absolute value ofthis
. -
invAssign
Set thisMPQ
to1/op
.- Returns:
- this
MPQ
. - Throws:
ArithmeticException
- ifop
is zero.
-
invAssign
Set thisMPQ
to its inverse.- Returns:
- this
MPQ
. - Throws:
ArithmeticException
- ifop
is zero.
-
inv
Return anMPQ
whose value is(1/this)
.- Throws:
ArithmeticException
- ifop
is zero.
-
cmp
Comparethis
withop
. Return a positive value if(this > op)
, zero if(this = op)
, or a negative value if(this < op)
. -
cmp
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
Return true ifthis
is equal toop
, false otherwise. Althoughcmp
can be used for the same purpose, this method should be faster. -
getNum
Return the numerator ofthis
. -
getDen
Return the denominator ofthis
. -
setNum
Set the numerator ofthis
to the valuenum
. -
setDen
Set the denominator ofthis
to the valueden
. -
isZero
public boolean isZero()Return true if and only ifthis
MPQ is zero. -
setValue
Set thisMPQ
toop
.- Returns:
- this
MPQ
.
-
setValue
Set thisMPQ
toop
.- Returns:
- this
MPQ
.
-
setValue
Set thisMPQ
to opop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
. - Throws:
ArithmeticException
- ifop
is not a finite number. In this case,this
is not altered.
-
setValue
Set thisMPQ
toop
. There is no rounding, this conversion is exact.- Returns:
- this
MPQ
.
-
setValue
Set thisMPQ
to the number represented by the stringstr
in the specifiedbase
. See the GMP functionmpq_set_str
.- Returns:
- this
MPQ
. - Throws:
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
Set thisMPQ
to the value represented by the stringstr
in decimal base. See the GMP functionmpq_set_str
.- Returns:
- this
MPQ
. - Throws:
NumberFormatException
- ifstr
is not a valid number representation in decimal base.
-
compareTo
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 interfaceComparable<MPQ>
-
equals
Compare thisMPQ
with the objectop
for equality. It returnstrue
if and only ifop
is anMPQ
with the same value ofthis
. -
hashCode
public int hashCode()Return a hash code value for thisMPQ
. -
intValue
public int intValue()Convert thisMPQ
to an int, truncating if necessary. -
longValue
public long longValue()Convert thisMPQ
to an long, truncating if necessary. -
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 classNumber
-
floatValue
public float floatValue()Convert thisMPQ
to a float, truncating if necessary.- Specified by:
floatValue
in classNumber
-
toString
Convert thisMPQ
to its string representation in the specifiedbase
, ornull
if the base is not valid. See the GMP functionmpq_get_str
.- Throws:
IllegalArgumentException
- if the base is not valid.
-
toString
Convert thisMPQ
to its decimal string representation. -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-
readObjectNoData
- Throws:
ObjectStreamException
-