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_canonicalizeis only used internally; - the functions in the category I/O of Rationals, and the macros
mpq_numrefandmpq_denrefare not exposed by theMPQclass.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCleaning action for theMPQclass. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MpqTThe pointer to the nativempq_tobject.private static final longVersion for serializability. -
Constructor Summary
ConstructorsModifierConstructorDescriptionMPQ()Build anMPQwhose value is zero.MPQ(double op) Build anMPQwhose value isop.MPQ(long num) Build anMPQwhose value isop.MPQ(long num, long dem) Build anMPQwhose value is(num/dem).Build anMPQwhose value isop.Build anMPQwhose value isop.Build anMPQwhose value isop.privateA private constructor which build anMPQstarting from a pointer to its native data object.Build anMPQwhose value is the number represented by the stringstrin decimal base.Build anMPQwhose value is the number represented by the stringstrin the specifiedbase. -
Method Summary
Modifier and TypeMethodDescriptionabs()Return anMPQwhose value is the absolute value ofthis.Set thisMPQto its absolute value.Set thisMPQto the absolute value ofop.Return anMPQwhose value is(this + op).Set thisMPQto(this + op)Set thisMPQto(op1 + op2).intcmp(long num, long den) Comparethiswith(num/dem).intComparethiswithop.intComparethiswithop.intcmpUi(long num, long den) Comparethiswith(num/dem).intCompare thisMPQwithop.Return anMPQwhose value is(this / op).div2Exp(long b) Return anMPQwhose value is(this / 2b).div2ExpAssign(long b) Set thisMPQto(this / 2b).div2ExpAssign(MPQ op, long b) Set thisMPQto(op / 2b).Set thisMPQto(this / op)Set thisMPQto(op1 / op2).doubleConvert thisMPQto a double, truncating if necessary.booleanReturn true ifthisis equal toop, false otherwise.booleanCompare thisMPQwith the objectopfor equality.floatConvert thisMPQto a float, truncating if necessary.doublegetD()Convert thisMPQto 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 thisMPQin the specifiedbase, ornullif the base is not valid.inthashCode()Return a hash code value for thisMPQ.static MPQinit()Return anMPQwhose value is zero.intintValue()Convert thisMPQto an int, truncating if necessary.inv()Return anMPQwhose value is(1/this).Set thisMPQto its inverse.Set thisMPQto1/op.booleanisZero()Return true if and only ifthisMPQ is zero.longConvert thisMPQto an long, truncating if necessary.Return anMPQwhose value is(this * op).mul2Exp(long b) Return anMPQwhose value is(this * 2b).mul2ExpAssign(long b) Set thisMPQto(this * 2b).mul2ExpAssign(MPQ op, long b) Set thisMPQto(op * 2b).Set thisMPQto(this * op)Set thisMPQto(op1 * op2).neg()Return anMPQwhose value is the quotient of(- this).Set thisMPQto its opposite.Set thisMPQto(- op).private voidprivate voidset(double op) Set thisMPQtoop.set(long num, long den) Set thisMPQto(<num/>den).Set thisMPQtoop.Set thisMPQtoop.Set thisMPQtoop.intSet thisMPQto the number represented by the stringstrin the specifiedbase.Set the denominator ofthisto the valueden.Set the numerator ofthisto the valuenum.setUi(long num, long den) Set thisMPQto(num/den).setValue(double op) Set thisMPQto opop.setValue(long op) Set thisMPQtoop.Set thisMPQtoop.Set thisMPQtoop.Set thisMPQto the value represented by the stringstrin decimal base.Set thisMPQto the number represented by the stringstrin the specifiedbase.intsgn()Return+1if(this > 0),0if(this = 0)and-1if(this < 0).Return anMPQwhose value is(this - op).Set thisMPQto(this - op)Set thisMPQto(op1 - op2).Swap the value of thisMPQwith the value ofop.toString()Convert thisMPQto its decimal string representation.toString(int base) Convert thisMPQto its string representation in the specifiedbase, ornullif the base is not valid.private voidMethods inherited from class Number
byteValue, shortValue
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDVersion for serializability.- See Also:
-
mpqNative
The pointer to the nativempq_tobject.
-
-
Constructor Details
-
MPQ
A private constructor which build anMPQstarting from a pointer to its native data object. The native object needs to be already initialized. -
MPQ
public MPQ()Build anMPQwhose value is zero. -
MPQ
Build anMPQwhose value isop. -
MPQ
Build anMPQwhose value isop. -
MPQ
public MPQ(long num, long dem) Build anMPQwhose value is(num/dem). -
MPQ
public MPQ(long num) Build anMPQwhose value isop. -
MPQ
public MPQ(double op) Build anMPQwhose value isop. There is no rounding, this conversion is exact.- Throws:
ArithmeticException- ifopis not a finite number.
-
MPQ
Build anMPQwhose value isop. There is no rounding, this conversion is exact. -
MPQ
Build anMPQwhose value is the number represented by the stringstrin the specifiedbase. See the GMP functionmpq_set_str.- Throws:
NumberFormatException- if eitherbaseis not valid orstris not a valid string in the specifiedbase.
-
MPQ
Build anMPQwhose value is the number represented by the stringstrin decimal base. See the GMP functionmpq_set_str.- Throws:
NumberFormatException- ifstris not a valid number representation in decimal base.
-
-
Method Details
-
getNative
Return the native pointer to the GMP object. -
init
Return anMPQwhose value is zero. -
set
-
set
-
set
-
setUi
-
set
Set thisMPQto the number represented by the stringstrin the specifiedbase. See the GMP functionmpq_set_str.- Returns:
- 0 if the operation succeeded, -1 otherwise. In the latter case,
thisis not altered.
-
swap
-
getD
public double getD()Convert thisMPQto 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 thisMPQtoop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ. - Throws:
ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
set
-
getStr
Return the String representation of thisMPQin the specifiedbase, ornullif the base is not valid. See the GMP functionmpq_get_str. -
addAssign
-
addAssign
-
add
-
subAssign
-
subAssign
-
sub
-
mulAssign
-
mulAssign
-
mul
-
divAssign
Set thisMPQto(op1 / op2).- Returns:
- this
MPQ. - Throws:
ArithmeticException- ifop2is zero.
-
divAssign
Set thisMPQto(this / op)- Returns:
- this
MPQ - Throws:
ArithmeticException- ifopis zero.
-
div
Return anMPQwhose value is(this / op).- Throws:
ArithmeticException- ifopis zero.
-
mul2ExpAssign
-
mul2ExpAssign
-
mul2Exp
Return anMPQwhose value is(this * 2b). -
div2ExpAssign
-
div2ExpAssign
-
div2Exp
Return anMPQwhose value is(this / 2b). -
negAssign
-
negAssign
-
neg
Return anMPQwhose value is the quotient of(- this). -
absAssign
-
absAssign
-
abs
Return anMPQwhose value is the absolute value ofthis. -
invAssign
Set thisMPQto1/op.- Returns:
- this
MPQ. - Throws:
ArithmeticException- ifopis zero.
-
invAssign
Set thisMPQto its inverse.- Returns:
- this
MPQ. - Throws:
ArithmeticException- ifopis zero.
-
inv
Return anMPQwhose value is(1/this).- Throws:
ArithmeticException- ifopis zero.
-
cmp
Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). -
cmp
Comparethiswithop. Return a positive value if(this > op), zero ifthis = op, or a negative value ifthis < op. -
cmp
public int cmp(long num, long den) Comparethiswith(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) Comparethiswith(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+1if(this > 0),0if(this = 0)and-1if(this < 0). -
equal
Return true ifthisis equal toop, false otherwise. Althoughcmpcan be used for the same purpose, this method should be faster. -
getNum
Return the numerator ofthis. -
getDen
Return the denominator ofthis. -
setNum
-
setDen
-
isZero
public boolean isZero()Return true if and only ifthisMPQ is zero. -
setValue
-
setValue
-
setValue
Set thisMPQto opop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ. - Throws:
ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
setValue
-
setValue
Set thisMPQto the number represented by the stringstrin the specifiedbase. See the GMP functionmpq_set_str.- Returns:
- this
MPQ. - Throws:
NumberFormatException- if eitherbaseis not valid orstris not a valid number representation in the specified base. In this case,thisis not altered.
-
setValue
Set thisMPQto the value represented by the stringstrin decimal base. See the GMP functionmpq_set_str.- Returns:
- this
MPQ. - Throws:
NumberFormatException- ifstris not a valid number representation in decimal base.
-
compareTo
Compare thisMPQwithop. 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:
compareToin interfaceComparable<MPQ>
-
equals
-
hashCode
-
intValue
-
longValue
-
doubleValue
public double doubleValue()Convert thisMPQto 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:
doubleValuein classNumber
-
floatValue
public float floatValue()Convert thisMPQto a float, truncating if necessary.- Specified by:
floatValuein classNumber
-
toString
Convert thisMPQto its string representation in the specifiedbase, ornullif the base is not valid. See the GMP functionmpq_get_str.- Throws:
IllegalArgumentException- if the base is not valid.
-
toString
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-
readObjectNoData
- Throws:
ObjectStreamException
-