Package it.unich.jgmp

Class 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 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:
    Serialized Form
    • Nested Class Summary

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

      Fields 
      Modifier and Type Field Description
      private MpqT mpqNative
      The pointer to the native mpq_t object.
      private static long serialVersionUID
      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​(java.lang.String str)
      Build an MPQ whose value is the number represented by the string str in decimal base.
        MPQ​(java.lang.String str, int base)
      Build an MPQ whose value is the number represented by the string str in the specified base.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MPQ abs()
      Return an MPQ whose value is the absolute value of this.
      MPQ absAssign()
      Set this MPQ to its absolute value.
      MPQ absAssign​(MPQ op)
      Set this MPQ to the absolute value of op.
      MPQ add​(MPQ op)
      Return an MPQ whose value is (this + op).
      MPQ addAssign​(MPQ op)
      Set this MPQ to (this + op)
      MPQ 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 compareTo​(MPQ op)
      Compare this MPQ with op.
      MPQ div​(MPQ op)
      Return an MPQ whose value is (this / op).
      MPQ div2Exp​(long b)
      Return an MPQ whose value is (this / 2b).
      MPQ div2ExpAssign​(long b)
      Set this MPQ to (this / 2b).
      MPQ div2ExpAssign​(MPQ op, long b)
      Set this MPQ to (op / 2b).
      MPQ divAssign​(MPQ op)
      Set this MPQ to (this / op)
      MPQ divAssign​(MPQ op1, MPQ op2)
      Set this MPQ to (op1 / op2).
      double doubleValue()
      Convert this MPQ to a double, truncating if necessary.
      boolean equal​(MPQ op)
      Return true if this is equal to op, false otherwise.
      boolean equals​(java.lang.Object obj)
      Compare this MPQ with the object op for equality.
      float floatValue()
      Convert this MPQ to a float, truncating if necessary.
      double getD()
      Convert this MPQ to a double, truncating if necessary.
      MPZ getDen()
      Return the denominator of this.
      MpqT getNative()
      Return the native pointer to the GMP object.
      MPZ getNum()
      Return the numerator of this.
      java.lang.String getStr​(int base)
      Return the String representation of this MPQ in the specified base, or null if the base is not valid.
      int hashCode()
      Return a hash code value for this MPQ.
      static MPQ init()
      Return an MPQ whose value is zero.
      int intValue()
      Convert this MPQ to an int, truncating if necessary.
      MPQ inv()
      Return an MPQ whose value is (1/this).
      MPQ invAssign()
      Set this MPQ to its inverse.
      MPQ invAssign​(MPQ op)
      Set this MPQ to 1/op.
      boolean isZero()
      Return true if and only if this MPQ is zero.
      long longValue()
      Convert this MPQ to an long, truncating if necessary.
      MPQ mul​(MPQ op)
      Return an MPQ whose value is (this * op).
      MPQ mul2Exp​(long b)
      Return an MPQ whose value is (this * 2b).
      MPQ mul2ExpAssign​(long b)
      Set this MPQ to (this * 2b).
      MPQ mul2ExpAssign​(MPQ op, long b)
      Set this MPQ to (op * 2b).
      MPQ mulAssign​(MPQ op)
      Set this MPQ to (this * op)
      MPQ mulAssign​(MPQ op1, MPQ op2)
      Set this MPQ to (op1 * op2).
      MPQ neg()
      Return an MPQ whose value is the quotient of (- this).
      MPQ negAssign()
      Set this MPQ to its opposite.
      MPQ negAssign​(MPQ op)
      Set this MPQ to (- op).
      private void readObject​(java.io.ObjectInputStream in)  
      private void readObjectNoData()  
      MPQ set​(double op)
      Set this MPQ to op.
      MPQ set​(long num, long den)
      Set this MPQ to (<num/>den).
      MPQ set​(MPF op)
      Set this MPQ to op.
      MPQ set​(MPQ op)
      Set this MPQ to op.
      MPQ set​(MPZ op)
      Set this MPQ to op.
      int set​(java.lang.String str, int base)
      Set this MPQ to the number represented by the string str in the specified base.
      MPQ setDen​(MPZ den)
      Set the denominator of this to the value den.
      MPQ setNum​(MPZ num)
      Set the numerator of this to the value num.
      MPQ setUi​(long num, long den)
      Set this MPQ to (num/den).
      MPQ setValue​(double op)
      Set this MPQ to op op.
      MPQ setValue​(long op)
      Set this MPQ to op.
      MPQ setValue​(MPF op)
      Set this MPQ to op.
      MPQ setValue​(MPQ op)
      Set this MPQ to op.
      MPQ setValue​(java.lang.String str)
      Set this MPQ to the value represented by the string str in decimal base.
      MPQ setValue​(java.lang.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).
      MPQ sub​(MPQ op)
      Return an MPQ whose value is (this - op).
      MPQ subAssign​(MPQ op)
      Set this MPQ to (this - op)
      MPQ subAssign​(MPQ op1, MPQ op2)
      Set this MPQ to (op1 - op2).
      MPQ swap​(MPQ op)
      Swap the value of this MPQ with the value of op.
      java.lang.String toString()
      Convert this MPQ to its decimal string representation.
      java.lang.String toString​(int base)
      Convert this MPQ to its string representation in the specified base, or null if the base is not valid.
      private void writeObject​(java.io.ObjectOutputStream out)  
      • 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 Detail

      • serialVersionUID

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

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

      • 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:
        java.lang.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​(java.lang.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:
        java.lang.NumberFormatException - if either base is not valid or str is not a valid string in the specified base.
      • MPQ

        public MPQ​(java.lang.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:
        java.lang.NumberFormatException - if str 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 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​(java.lang.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:
        java.lang.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 java.lang.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:
        java.lang.ArithmeticException - if op2 is zero.
      • divAssign

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

        public MPQ div​(MPQ op)
        Return an MPQ whose value is (this / op).
        Throws:
        java.lang.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:
        java.lang.ArithmeticException - if op is zero.
      • invAssign

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

        public MPQ inv()
        Return an MPQ whose value is (1/this).
        Throws:
        java.lang.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:
        java.lang.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​(java.lang.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:
        java.lang.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​(java.lang.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:
        java.lang.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 java.lang.Comparable<MPQ>
      • equals

        public boolean equals​(java.lang.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 java.lang.Object
      • hashCode

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

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

        public long longValue()
        Convert this MPQ to an long, truncating if necessary.
        Specified by:
        longValue in class java.lang.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 java.lang.Number
      • floatValue

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

        public java.lang.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:
        java.lang.IllegalArgumentException - if the base is not valid.
      • toString

        public java.lang.String toString()
        Convert this MPQ to its decimal string representation.
        Overrides:
        toString in class java.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