Package it.unich.jgmp

Class MPQ

All Implemented Interfaces:
Serializable, Comparable<MPQ>

public class MPQ extends Number implements 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 and mpq_denref are not exposed by the MPQ class.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Cleaning action for the MPQ class.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private MpqT
    The pointer to the native mpq_t object.
    private static final long
    Version for serializability.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    MPQ()
    Build an MPQ whose value is zero.
     
    MPQ(double op)
    Build an MPQ whose value is op.
     
    MPQ(long num)
    Build an MPQ whose value is op.
     
    MPQ(long num, long dem)
    Build an MPQ whose value is (num/dem).
     
    MPQ(MPF op)
    Build an MPQ whose value is op.
     
    MPQ(MPQ op)
    Build an MPQ whose value is op.
     
    MPQ(MPZ op)
    Build an MPQ whose value is op.
    private
    MPQ(MpqT pointer)
    A private constructor which build an MPQ starting from a pointer to its native data object.
     
    MPQ(String str)
    Build an MPQ whose value is the number represented by the string str in decimal base.
     
    MPQ(String str, int base)
    Build an MPQ whose value is the number represented by the string str in the specified base.
  • Method Summary

    Modifier and Type
    Method
    Description
    abs()
    Return an MPQ whose value is the absolute value of this.
    Set this MPQ to its absolute value.
    Set this MPQ to the absolute value of op.
    add(MPQ op)
    Return an MPQ whose value is (this + op).
    Set this MPQ to (this + op)
    addAssign(MPQ op1, MPQ op2)
    Set this MPQ to (op1 + op2).
    int
    cmp(long num, long den)
    Compare this with (num/dem).
    int
    cmp(MPQ op)
    Compare this with op.
    int
    cmp(MPZ op)
    Compare this with op.
    int
    cmpUi(long num, long den)
    Compare this with (num/dem).
    int
    Compare this MPQ with op.
    div(MPQ op)
    Return an MPQ whose value is (this / op).
    div2Exp(long b)
    Return an MPQ whose value is (this / 2b).
    div2ExpAssign(long b)
    Set this MPQ to (this / 2b).
    div2ExpAssign(MPQ op, long b)
    Set this MPQ to (op / 2b).
    Set this MPQ to (this / op)
    divAssign(MPQ op1, MPQ op2)
    Set this MPQ to (op1 / op2).
    double
    Convert this MPQ to a double, truncating if necessary.
    boolean
    equal(MPQ op)
    Return true if this is equal to op, false otherwise.
    boolean
    Compare this MPQ with the object op for equality.
    float
    Convert this MPQ to a float, truncating if necessary.
    double
    Convert this MPQ to a double, truncating if necessary.
    Return the denominator of this.
    Return the native pointer to the GMP object.
    Return the numerator of this.
    getStr(int base)
    Return the String representation of this MPQ in the specified base, or null if the base is not valid.
    int
    Return a hash code value for this MPQ.
    static MPQ
    Return an MPQ whose value is zero.
    int
    Convert this MPQ to an int, truncating if necessary.
    inv()
    Return an MPQ whose value is (1/this).
    Set this MPQ to its inverse.
    Set this MPQ to 1/op.
    boolean
    Return true if and only if this MPQ is zero.
    long
    Convert this MPQ to an long, truncating if necessary.
    mul(MPQ op)
    Return an MPQ whose value is (this * op).
    mul2Exp(long b)
    Return an MPQ whose value is (this * 2b).
    mul2ExpAssign(long b)
    Set this MPQ to (this * 2b).
    mul2ExpAssign(MPQ op, long b)
    Set this MPQ to (op * 2b).
    Set this MPQ to (this * op)
    mulAssign(MPQ op1, MPQ op2)
    Set this MPQ to (op1 * op2).
    neg()
    Return an MPQ whose value is the quotient of (- this).
    Set this MPQ to its opposite.
    Set this MPQ to (- op).
    private void
     
    private void
     
    set(double op)
    Set this MPQ to op.
    set(long num, long den)
    Set this MPQ to (<num/>den).
    set(MPF op)
    Set this MPQ to op.
    set(MPQ op)
    Set this MPQ to op.
    set(MPZ op)
    Set this MPQ to op.
    int
    set(String str, int base)
    Set this MPQ to the number represented by the string str in the specified base.
    setDen(MPZ den)
    Set the denominator of this to the value den.
    setNum(MPZ num)
    Set the numerator of this to the value num.
    setUi(long num, long den)
    Set this MPQ to (num/den).
    setValue(double op)
    Set this MPQ to op op.
    setValue(long op)
    Set this MPQ to op.
    Set this MPQ to op.
    Set this MPQ to op.
    Set this MPQ to the value represented by the string str in decimal base.
    setValue(String str, int base)
    Set this MPQ to the number represented by the string str in the specified base.
    int
    sgn()
    Return +1 if (this > 0), 0 if (this = 0) and -1 if (this < 0).
    sub(MPQ op)
    Return an MPQ whose value is (this - op).
    Set this MPQ to (this - op)
    subAssign(MPQ op1, MPQ op2)
    Set this MPQ to (op1 - op2).
    swap(MPQ op)
    Swap the value of this MPQ with the value of op.
    Convert this MPQ to its decimal string representation.
    toString(int base)
    Convert this MPQ to its string representation in the specified base, or null if the base is not valid.
    private void
     

    Methods inherited from class java.lang.Number

    byteValue, shortValue

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Version for serializability.
      See Also:
    • mpqNative

      private transient MpqT mpqNative
      The pointer to the native mpq_t object.
  • Constructor Details

    • MPQ

      private MPQ(MpqT pointer)
      A private constructor which build an MPQ starting from a pointer to its native data object. The native object needs to be already initialized.
    • MPQ

      public MPQ()
      Build an MPQ whose value is zero.
    • MPQ

      public MPQ(MPQ op)
      Build an MPQ whose value is op.
    • MPQ

      public MPQ(MPZ op)
      Build an MPQ whose value is op.
    • MPQ

      public MPQ(long num, long dem)
      Build an MPQ whose value is (num/dem).
    • MPQ

      public MPQ(long num)
      Build an MPQ whose value is op.
    • MPQ

      public MPQ(double op)
      Build an MPQ whose value is op. There is no rounding, this conversion is exact.
      Throws:
      ArithmeticException - if op is not a finite number.
    • MPQ

      public MPQ(MPF op)
      Build an MPQ whose value is op. There is no rounding, this conversion is exact.
    • MPQ

      public MPQ(String str, int base)
      Build an MPQ whose value is the number represented by the string str in the specified base. See the GMP function mpq_set_str.
      Throws:
      NumberFormatException - if either base is not valid or str is not a valid string in the specified base.
    • MPQ

      public MPQ(String str)
      Build an MPQ whose value is the number represented by the string str in decimal base. See the GMP function mpq_set_str.
      Throws:
      NumberFormatException - if str is not a valid number representation in decimal base.
  • Method Details

    • getNative

      public MpqT getNative()
      Return the native pointer to the GMP object.
    • init

      public static MPQ init()
      Return an MPQ whose value is zero.
    • set

      public MPQ set(MPQ op)
      Set this MPQ to op.
      Returns:
      this MPQ.
    • set

      public MPQ set(MPZ op)
      Set this MPQ to op.
      Returns:
      this MPQ.
    • set

      public MPQ set(long num, long den)
      Set this MPQ to (<num/>den).
      Returns:
      this MPQ.
    • setUi

      public MPQ setUi(long num, long den)
      Set this MPQ to (num/den).
      Returns:
      this MPQ.
    • set

      public int set(String str, int base)
      Set this MPQ to the number represented by the string str in the specified base. See the GMP function mpq_set_str.
      Returns:
      0 if the operation succeeded, -1 otherwise. In the latter case, this is not altered.
    • swap

      public MPQ swap(MPQ op)
      Swap the value of this MPQ with the value of op.
      Returns:
      this MPQ.
    • getD

      public double getD()
      Convert this MPQ 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 this MPQ to op. There is no rounding, this conversion is exact.
      Returns:
      this MPQ.
      Throws:
      ArithmeticException - if op is not a finite number. In this case, this is not altered.
    • set

      public MPQ set(MPF op)
      Set this MPQ to op. There is no rounding, this conversion is exact.
      Returns:
      this MPQ.
    • getStr

      public String getStr(int base)
      Return the String representation of this MPQ in the specified base, or null if the base is not valid. See the GMP function mpq_get_str.
    • addAssign

      public MPQ addAssign(MPQ op1, MPQ op2)
      Set this MPQ to (op1 + op2).
      Returns:
      this MPQ.
    • addAssign

      public MPQ addAssign(MPQ op)
      Set this MPQ to (this + op)
      Returns:
      this MPQ
    • add

      public MPQ add(MPQ op)
      Return an MPQ whose value is (this + op).
    • subAssign

      public MPQ subAssign(MPQ op1, MPQ op2)
      Set this MPQ to (op1 - op2).
      Returns:
      this MPQ.
    • subAssign

      public MPQ subAssign(MPQ op)
      Set this MPQ to (this - op)
      Returns:
      this MPQ
    • sub

      public MPQ sub(MPQ op)
      Return an MPQ whose value is (this - op).
    • mulAssign

      public MPQ mulAssign(MPQ op1, MPQ op2)
      Set this MPQ to (op1 * op2).
      Returns:
      this MPQ.
    • mulAssign

      public MPQ mulAssign(MPQ op)
      Set this MPQ to (this * op)
      Returns:
      this MPQ
    • mul

      public MPQ mul(MPQ op)
      Return an MPQ whose value is (this * op).
    • divAssign

      public MPQ divAssign(MPQ op1, MPQ op2)
      Set this MPQ to (op1 / op2).
      Returns:
      this MPQ.
      Throws:
      ArithmeticException - if op2 is zero.
    • divAssign

      public MPQ divAssign(MPQ op)
      Set this MPQ to (this / op)
      Returns:
      this MPQ
      Throws:
      ArithmeticException - if op is zero.
    • div

      public MPQ div(MPQ op)
      Return an MPQ whose value is (this / op).
      Throws:
      ArithmeticException - if op is zero.
    • mul2ExpAssign

      public MPQ mul2ExpAssign(MPQ op, long b)
      Set this MPQ to (op * 2b).
      Returns:
      this MPQ.
    • mul2ExpAssign

      public MPQ mul2ExpAssign(long b)
      Set this MPQ to (this * 2b).
      Returns:
      this MPQ.
    • mul2Exp

      public MPQ mul2Exp(long b)
      Return an MPQ whose value is (this * 2b).
    • div2ExpAssign

      public MPQ div2ExpAssign(MPQ op, long b)
      Set this MPQ to (op / 2b).
      Returns:
      this MPQ.
    • div2ExpAssign

      public MPQ div2ExpAssign(long b)
      Set this MPQ to (this / 2b).
      Returns:
      this MPQ.
    • div2Exp

      public MPQ div2Exp(long b)
      Return an MPQ whose value is (this / 2b).
    • negAssign

      public MPQ negAssign(MPQ op)
      Set this MPQ to (- op).
      Returns:
      this MPQ.
    • negAssign

      public MPQ negAssign()
      Set this MPQ to its opposite.
      Returns:
      this MPQ.
    • neg

      public MPQ neg()
      Return an MPQ whose value is the quotient of (- this).
    • absAssign

      public MPQ absAssign(MPQ op)
      Set this MPQ to the absolute value of op.
      Returns:
      this MPQ.
    • absAssign

      public MPQ absAssign()
      Set this MPQ to its absolute value.
      Returns:
      this MPQ.
    • abs

      public MPQ abs()
      Return an MPQ whose value is the absolute value of this.
    • invAssign

      public MPQ invAssign(MPQ op)
      Set this MPQ to 1/op.
      Returns:
      this MPQ.
      Throws:
      ArithmeticException - if op is zero.
    • invAssign

      public MPQ invAssign()
      Set this MPQ to its inverse.
      Returns:
      this MPQ.
      Throws:
      ArithmeticException - if op is zero.
    • inv

      public MPQ inv()
      Return an MPQ whose value is (1/this).
      Throws:
      ArithmeticException - if op is zero.
    • cmp

      public int cmp(MPQ op)
      Compare this with op. Return a positive value if (this > op), zero if (this = op), or a negative value if (this < op).
    • cmp

      public int cmp(MPZ op)
      Compare this with op. Return a positive value if (this > op), zero if this = op, or a negative value if this < op.
    • cmp

      public int cmp(long num, long den)
      Compare this 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)
      Compare this 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 if this is equal to op, false otherwise. Although cmp can be used for the same purpose, this method should be faster.
    • getNum

      public MPZ getNum()
      Return the numerator of this.
    • getDen

      public MPZ getDen()
      Return the denominator of this.
    • setNum

      public MPQ setNum(MPZ num)
      Set the numerator of this to the value num.
    • setDen

      public MPQ setDen(MPZ den)
      Set the denominator of this to the value den.
    • isZero

      public boolean isZero()
      Return true if and only if this MPQ is zero.
    • setValue

      public MPQ setValue(MPQ op)
      Set this MPQ to op.
      Returns:
      this MPQ.
    • setValue

      public MPQ setValue(long op)
      Set this MPQ to op.
      Returns:
      this MPQ.
    • setValue

      public MPQ setValue(double op)
      Set this MPQ to op op. There is no rounding, this conversion is exact.
      Returns:
      this MPQ.
      Throws:
      ArithmeticException - if op is not a finite number. In this case, this is not altered.
    • setValue

      public MPQ setValue(MPF op)
      Set this MPQ to op. There is no rounding, this conversion is exact.
      Returns:
      this MPQ.
    • setValue

      public MPQ setValue(String str, int base)
      Set this MPQ to the number represented by the string str in the specified base. See the GMP function mpq_set_str.
      Returns:
      this MPQ.
      Throws:
      NumberFormatException - if either base is not valid or str is not a valid number representation in the specified base. In this case, this is not altered.
    • setValue

      public MPQ setValue(String str)
      Set this MPQ to the value represented by the string str in decimal base. See the GMP function mpq_set_str.
      Returns:
      this MPQ.
      Throws:
      NumberFormatException - if str is not a valid number representation in decimal base.
    • compareTo

      public int compareTo(MPQ op)
      Compare this MPQ with op. 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 interface Comparable<MPQ>
    • equals

      public boolean equals(Object obj)
      Compare this MPQ with the object op for equality. It returns true if and only if op is an MPQ with the same value of this.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Return a hash code value for this MPQ.
      Overrides:
      hashCode in class Object
    • intValue

      public int intValue()
      Convert this MPQ to an int, truncating if necessary.
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Convert this MPQ to an long, truncating if necessary.
      Specified by:
      longValue in class Number
    • doubleValue

      public double doubleValue()
      Convert this MPQ 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 class Number
    • floatValue

      public float floatValue()
      Convert this MPQ to a float, truncating if necessary.
      Specified by:
      floatValue in class Number
    • toString

      public String toString(int base)
      Convert this MPQ to its string representation in the specified base, or null if the base is not valid. See the GMP function mpq_get_str.
      Throws:
      IllegalArgumentException - if the base is not valid.
    • toString

      public String toString()
      Convert this MPQ to its decimal string representation.
      Overrides:
      toString in class Object
    • writeObject

      private void writeObject(ObjectOutputStream out) throws IOException
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException
    • readObjectNoData

      private void readObjectNoData() throws ObjectStreamException
      Throws:
      ObjectStreamException