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_tdata type, see the Rational Number Functions page of the GMP manual. In determining the names and signatures of the methods of theMPQclass, we adopt the rules described in the documentation of theit.unich.jgmppackage, 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMPQ.MPQCleanerCleaning action for theMPQclass.
-
Field Summary
Fields Modifier and Type Field Description private MpqTmpqNativeThe pointer to the nativempq_tobject.private static longserialVersionUIDVersion for serializability.
-
Constructor Summary
Constructors Modifier Constructor Description MPQ()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).MPQ(MPF op)Build anMPQwhose value isop.MPQ(MPQ op)Build anMPQwhose value isop.MPQ(MPZ op)Build anMPQwhose value isop.privateMPQ(MpqT pointer)A private constructor which build anMPQstarting from a pointer to its native data object.MPQ(java.lang.String str)Build anMPQwhose value is the number represented by the stringstrin decimal base.MPQ(java.lang.String str, int base)Build anMPQwhose value is the number represented by the stringstrin the specifiedbase.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MPQabs()Return anMPQwhose value is the absolute value ofthis.MPQabsAssign()Set thisMPQto its absolute value.MPQabsAssign(MPQ op)Set thisMPQto the absolute value ofop.MPQadd(MPQ op)Return anMPQwhose value is(this + op).MPQaddAssign(MPQ op)Set thisMPQto(this + op)MPQaddAssign(MPQ op1, MPQ op2)Set thisMPQto(op1 + op2).intcmp(long num, long den)Comparethiswith(num/dem).intcmp(MPQ op)Comparethiswithop.intcmp(MPZ op)Comparethiswithop.intcmpUi(long num, long den)Comparethiswith(num/dem).intcompareTo(MPQ op)Compare thisMPQwithop.MPQdiv(MPQ op)Return anMPQwhose value is(this / op).MPQdiv2Exp(long b)Return anMPQwhose value is(this / 2b).MPQdiv2ExpAssign(long b)Set thisMPQto(this / 2b).MPQdiv2ExpAssign(MPQ op, long b)Set thisMPQto(op / 2b).MPQdivAssign(MPQ op)Set thisMPQto(this / op)MPQdivAssign(MPQ op1, MPQ op2)Set thisMPQto(op1 / op2).doubledoubleValue()Convert thisMPQto a double, truncating if necessary.booleanequal(MPQ op)Return true ifthisis equal toop, false otherwise.booleanequals(java.lang.Object obj)Compare thisMPQwith the objectopfor equality.floatfloatValue()Convert thisMPQto a float, truncating if necessary.doublegetD()Convert thisMPQto a double, truncating if necessary.MPZgetDen()Return the denominator ofthis.MpqTgetNative()Return the native pointer to the GMP object.MPZgetNum()Return the numerator ofthis.java.lang.StringgetStr(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.MPQinv()Return anMPQwhose value is(1/this).MPQinvAssign()Set thisMPQto its inverse.MPQinvAssign(MPQ op)Set thisMPQto1/op.booleanisZero()Return true if and only ifthisMPQ is zero.longlongValue()Convert thisMPQto an long, truncating if necessary.MPQmul(MPQ op)Return anMPQwhose value is(this * op).MPQmul2Exp(long b)Return anMPQwhose value is(this * 2b).MPQmul2ExpAssign(long b)Set thisMPQto(this * 2b).MPQmul2ExpAssign(MPQ op, long b)Set thisMPQto(op * 2b).MPQmulAssign(MPQ op)Set thisMPQto(this * op)MPQmulAssign(MPQ op1, MPQ op2)Set thisMPQto(op1 * op2).MPQneg()Return anMPQwhose value is the quotient of(- this).MPQnegAssign()Set thisMPQto its opposite.MPQnegAssign(MPQ op)Set thisMPQto(- op).private voidreadObject(java.io.ObjectInputStream in)private voidreadObjectNoData()MPQset(double op)Set thisMPQtoop.MPQset(long num, long den)Set thisMPQto(<num/>den).MPQset(MPF op)Set thisMPQtoop.MPQset(MPQ op)Set thisMPQtoop.MPQset(MPZ op)Set thisMPQtoop.intset(java.lang.String str, int base)Set thisMPQto the number represented by the stringstrin the specifiedbase.MPQsetDen(MPZ den)Set the denominator ofthisto the valueden.MPQsetNum(MPZ num)Set the numerator ofthisto the valuenum.MPQsetUi(long num, long den)Set thisMPQto(num/den).MPQsetValue(double op)Set thisMPQto opop.MPQsetValue(long op)Set thisMPQtoop.MPQsetValue(MPF op)Set thisMPQtoop.MPQsetValue(MPQ op)Set thisMPQtoop.MPQsetValue(java.lang.String str)Set thisMPQto the value represented by the stringstrin decimal base.MPQsetValue(java.lang.String str, int base)Set thisMPQto the number represented by the stringstrin the specifiedbase.intsgn()Return+1if(this > 0),0if(this = 0)and-1if(this < 0).MPQsub(MPQ op)Return anMPQwhose value is(this - op).MPQsubAssign(MPQ op)Set thisMPQto(this - op)MPQsubAssign(MPQ op1, MPQ op2)Set thisMPQto(op1 - op2).MPQswap(MPQ op)Swap the value of thisMPQwith the value ofop.java.lang.StringtoString()Convert thisMPQto its decimal string representation.java.lang.StringtoString(int base)Convert thisMPQto its string representation in the specifiedbase, ornullif the base is not valid.private voidwriteObject(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_tobject.
-
-
Constructor Detail
-
MPQ
private MPQ(MpqT pointer)
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
public MPQ(MPQ op)
Build anMPQwhose value isop.
-
MPQ
public MPQ(MPZ op)
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:
java.lang.ArithmeticException- ifopis not a finite number.
-
MPQ
public MPQ(MPF op)
Build anMPQwhose value isop. There is no rounding, this conversion is exact.
-
MPQ
public MPQ(java.lang.String str, int base)Build anMPQwhose value is the number represented by the stringstrin the specifiedbase. See the GMP functionmpq_set_str.- Throws:
java.lang.NumberFormatException- if eitherbaseis not valid orstris not a valid string in the specifiedbase.
-
MPQ
public MPQ(java.lang.String str)
Build anMPQwhose value is the number represented by the stringstrin decimal base. See the GMP functionmpq_set_str.- Throws:
java.lang.NumberFormatException- ifstris 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 anMPQwhose value is zero.
-
set
public MPQ set(long num, long den)
Set thisMPQto(<num/>den).- Returns:
- this
MPQ.
-
setUi
public MPQ setUi(long num, long den)
Set thisMPQto(num/den).- Returns:
- this
MPQ.
-
set
public int set(java.lang.String str, int base)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.
-
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
public MPQ set(double op)
Set thisMPQtoop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ. - Throws:
java.lang.ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
set
public MPQ set(MPF op)
Set thisMPQtoop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ.
-
getStr
public java.lang.String getStr(int base)
Return the String representation of thisMPQin the specifiedbase, ornullif the base is not valid. See the GMP functionmpq_get_str.
-
divAssign
public MPQ divAssign(MPQ op1, MPQ op2)
Set thisMPQto(op1 / op2).- Returns:
- this
MPQ. - Throws:
java.lang.ArithmeticException- ifop2is zero.
-
divAssign
public MPQ divAssign(MPQ op)
Set thisMPQto(this / op)- Returns:
- this
MPQ - Throws:
java.lang.ArithmeticException- ifopis zero.
-
div
public MPQ div(MPQ op)
Return anMPQwhose value is(this / op).- Throws:
java.lang.ArithmeticException- ifopis zero.
-
mul2ExpAssign
public MPQ mul2ExpAssign(MPQ op, long b)
Set thisMPQto(op * 2b).- Returns:
- this
MPQ.
-
mul2ExpAssign
public MPQ mul2ExpAssign(long b)
Set thisMPQto(this * 2b).- Returns:
- this
MPQ.
-
mul2Exp
public MPQ mul2Exp(long b)
Return anMPQwhose value is(this * 2b).
-
div2ExpAssign
public MPQ div2ExpAssign(MPQ op, long b)
Set thisMPQto(op / 2b).- Returns:
- this
MPQ.
-
div2ExpAssign
public MPQ div2ExpAssign(long b)
Set thisMPQto(this / 2b).- Returns:
- this
MPQ.
-
div2Exp
public MPQ div2Exp(long b)
Return anMPQwhose value is(this / 2b).
-
negAssign
public MPQ negAssign()
Set thisMPQto its opposite.- Returns:
- this
MPQ.
-
neg
public MPQ neg()
Return anMPQwhose value is the quotient of(- this).
-
absAssign
public MPQ absAssign(MPQ op)
Set thisMPQto the absolute value ofop.- Returns:
- this
MPQ.
-
absAssign
public MPQ absAssign()
Set thisMPQto its absolute value.- Returns:
- this
MPQ.
-
abs
public MPQ abs()
Return anMPQwhose value is the absolute value ofthis.
-
invAssign
public MPQ invAssign(MPQ op)
Set thisMPQto1/op.- Returns:
- this
MPQ. - Throws:
java.lang.ArithmeticException- ifopis zero.
-
invAssign
public MPQ invAssign()
Set thisMPQto its inverse.- Returns:
- this
MPQ. - Throws:
java.lang.ArithmeticException- ifopis zero.
-
inv
public MPQ inv()
Return anMPQwhose value is(1/this).- Throws:
java.lang.ArithmeticException- ifopis zero.
-
cmp
public int cmp(MPQ op)
Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op).
-
cmp
public int cmp(MPZ op)
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
public boolean equal(MPQ op)
Return true ifthisis equal toop, false otherwise. Althoughcmpcan 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 ifthisMPQ is zero.
-
setValue
public MPQ setValue(long op)
Set thisMPQtoop.- Returns:
- this
MPQ.
-
setValue
public MPQ setValue(double op)
Set thisMPQto opop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ. - Throws:
java.lang.ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
setValue
public MPQ setValue(MPF op)
Set thisMPQtoop. There is no rounding, this conversion is exact.- Returns:
- this
MPQ.
-
setValue
public MPQ setValue(java.lang.String str, int base)
Set thisMPQto the number represented by the stringstrin the specifiedbase. See the GMP functionmpq_set_str.- Returns:
- this
MPQ. - Throws:
java.lang.NumberFormatException- if eitherbaseis not valid orstris not a valid number representation in the specified base. In this case,thisis not altered.
-
setValue
public MPQ setValue(java.lang.String str)
Set thisMPQto the value represented by the stringstrin decimal base. See the GMP functionmpq_set_str.- Returns:
- this
MPQ. - Throws:
java.lang.NumberFormatException- ifstris not a valid number representation in decimal base.
-
compareTo
public int compareTo(MPQ op)
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 interfacejava.lang.Comparable<MPQ>
-
equals
public boolean equals(java.lang.Object obj)
Compare thisMPQwith the objectopfor equality. It returnstrueif and only ifopis anMPQwith the same value ofthis.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
Return a hash code value for thisMPQ.- Overrides:
hashCodein classjava.lang.Object
-
intValue
public int intValue()
Convert thisMPQto an int, truncating if necessary.- Specified by:
intValuein classjava.lang.Number
-
longValue
public long longValue()
Convert thisMPQto an long, truncating if necessary.- Specified by:
longValuein classjava.lang.Number
-
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 classjava.lang.Number
-
floatValue
public float floatValue()
Convert thisMPQto a float, truncating if necessary.- Specified by:
floatValuein classjava.lang.Number
-
toString
public java.lang.String toString(int base)
Convert thisMPQto its string representation in the specifiedbase, ornullif 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 thisMPQto its decimal string representation.- Overrides:
toStringin 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.IOExceptionjava.lang.ClassNotFoundException
-
readObjectNoData
private void readObjectNoData() throws java.io.ObjectStreamException- Throws:
java.io.ObjectStreamException
-
-