Package maths

Class BigFraction

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Comparable

    public class BigFraction
    extends java.lang.Object
    implements java.lang.Cloneable, java.lang.Comparable, java.io.Serializable
    Represents a fraction with arbitrary precision using BigInteger.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      BigFraction​(long num, long den)
      Constructs a BigFraction with the specified long numerator and denominator.
      BigFraction​(java.lang.String s)
      Constructs a BigFraction from a string representation.
      BigFraction​(java.math.BigInteger b)
      Constructs a BigFraction with the specified BigInteger numerator.
      BigFraction​(java.math.BigInteger num, java.math.BigInteger den)
      Constructs a BigFraction with the specified numerator and denominator.
      BigFraction​(BigFraction f)
      Copy constructor for BigFraction.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      BigFraction add​(long n)
      Adds the specified long value to this fraction.
      BigFraction add​(java.math.BigInteger n)
      Adds the specified BigInteger to this fraction.
      BigFraction add​(BigFraction b)
      Adds the specified fraction to this fraction.
      java.lang.Object clone()
      Creates and returns a copy of this BigFraction.
      int compareTo​(java.lang.Object other)
      Compares this fraction with another object.
      int compareTo​(java.math.BigInteger n)
      Compares this fraction with a BigInteger.
      java.math.BigInteger denominator()
      Returns the denominator as a BigInteger.
      BigFraction divide​(java.math.BigInteger n)
      Divides this fraction by the specified BigInteger.
      BigFraction divide​(BigFraction b)
      Divides this fraction by the specified fraction.
      boolean equals​(long n)
      Checks if this fraction is equal to a long value.
      boolean equals​(java.lang.Object other)
      Checks if this fraction is equal to another object.
      boolean equals​(java.math.BigInteger n)
      Checks if this fraction is equal to a BigInteger.
      int hashCode()
      Returns a hash code for this fraction.
      boolean isZero()
      Checks if the numerator is zero.
      BigFraction max​(BigFraction val)
      Returns the maximum of this fraction and the specified fraction.
      BigFraction min​(BigFraction val)
      Returns the minimum of this fraction and the specified fraction.
      BigFraction multiply​(java.math.BigInteger n)
      Multiplies this fraction by the specified BigInteger.
      BigFraction multiply​(BigFraction b)
      Multiplies this fraction by the specified fraction.
      java.math.BigInteger numerator()
      Returns the numerator as a BigInteger.
      BigFraction pow​(int d)
      Raises this fraction to the power of the specified integer.
      BigFraction sqrt()
      Calculates the square root of this fraction if it is non-negative.
      java.lang.String toString()
      Returns a string representation of the fraction.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • numerator_

        protected final java.math.BigInteger numerator_
      • denominator_

        protected final java.math.BigInteger denominator_
    • Constructor Detail

      • BigFraction

        public BigFraction​(java.math.BigInteger num,
                           java.math.BigInteger den)
        Constructs a BigFraction with the specified numerator and denominator.
        Parameters:
        num - the numerator
        den - the denominator
      • BigFraction

        public BigFraction​(java.math.BigInteger b)
        Constructs a BigFraction with the specified BigInteger numerator.
        Parameters:
        b - the numerator
      • BigFraction

        public BigFraction​(BigFraction f)
        Copy constructor for BigFraction.
        Parameters:
        f - the BigFraction to copy
      • BigFraction

        public BigFraction​(java.lang.String s)
        Constructs a BigFraction from a string representation.
        Parameters:
        s - the string representation of the fraction
      • BigFraction

        public BigFraction​(long num,
                           long den)
        Constructs a BigFraction with the specified long numerator and denominator.
        Parameters:
        num - the numerator
        den - the denominator
    • Method Detail

      • numerator

        public java.math.BigInteger numerator()
        Returns the numerator as a BigInteger.
        Returns:
        the numerator as a BigInteger
      • denominator

        public java.math.BigInteger denominator()
        Returns the denominator as a BigInteger.
        Returns:
        the denominator as a BigInteger
      • toString

        public java.lang.String toString()
        Returns a string representation of the fraction.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string representation of the fraction
      • clone

        public java.lang.Object clone()
        Creates and returns a copy of this BigFraction.
        Overrides:
        clone in class java.lang.Object
        Returns:
        a clone of this BigFraction
      • min

        public BigFraction min​(BigFraction val)
        Returns the minimum of this fraction and the specified fraction.
        Parameters:
        val - the fraction to compare with
        Returns:
        the minimum fraction
      • max

        public BigFraction max​(BigFraction val)
        Returns the maximum of this fraction and the specified fraction.
        Parameters:
        val - the fraction to compare with
        Returns:
        the maximum fraction
      • pow

        public BigFraction pow​(int d)
        Raises this fraction to the power of the specified integer.
        Parameters:
        d - the exponent
        Returns:
        the resulting fraction
      • add

        public BigFraction add​(BigFraction b)
        Adds the specified fraction to this fraction.
        Parameters:
        b - the fraction to add
        Returns:
        the resulting fraction
      • add

        public BigFraction add​(java.math.BigInteger n)
        Adds the specified BigInteger to this fraction.
        Parameters:
        n - the BigInteger to add
        Returns:
        the resulting fraction
      • add

        public BigFraction add​(long n)
        Adds the specified long value to this fraction.
        Parameters:
        n - the long value to add
        Returns:
        the resulting fraction
      • multiply

        public BigFraction multiply​(BigFraction b)
        Multiplies this fraction by the specified fraction.
        Parameters:
        b - the fraction to multiply by
        Returns:
        the resulting fraction
      • multiply

        public BigFraction multiply​(java.math.BigInteger n)
        Multiplies this fraction by the specified BigInteger.
        Parameters:
        n - the BigInteger to multiply by
        Returns:
        the resulting fraction
      • divide

        public BigFraction divide​(BigFraction b)
        Divides this fraction by the specified fraction.
        Parameters:
        b - the fraction to divide by
        Returns:
        the resulting fraction
      • divide

        public BigFraction divide​(java.math.BigInteger n)
        Divides this fraction by the specified BigInteger.
        Parameters:
        n - the BigInteger to divide by
        Returns:
        the resulting fraction
      • sqrt

        public BigFraction sqrt()
        Calculates the square root of this fraction if it is non-negative.
        Returns:
        the square root as a BigFraction, or null if not a perfect square
      • isZero

        public boolean isZero()
        Checks if the numerator is zero.
        Returns:
        true if the numerator is zero; false otherwise
      • compareTo

        public int compareTo​(java.lang.Object other)
        Compares this fraction with another object.
        Specified by:
        compareTo in interface java.lang.Comparable
        Parameters:
        other - the object to compare with
        Returns:
        -1, 0, or 1 as this fraction is less than, equal to, or greater than the given object
      • compareTo

        public int compareTo​(java.math.BigInteger n)
        Compares this fraction with a BigInteger.
        Parameters:
        n - the BigInteger to compare with
        Returns:
        -1, 0, or 1 as this fraction is less than, equal to, or greater than the given BigInteger
      • equals

        public boolean equals​(java.lang.Object other)
        Checks if this fraction is equal to another object.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - the object to compare with
        Returns:
        true if the fractions are equal; false otherwise
      • equals

        public boolean equals​(java.math.BigInteger n)
        Checks if this fraction is equal to a BigInteger.
        Parameters:
        n - the BigInteger to compare with
        Returns:
        true if equal; false otherwise
      • equals

        public boolean equals​(long n)
        Checks if this fraction is equal to a long value.
        Parameters:
        n - the long value to compare with
        Returns:
        true if equal; false otherwise
      • hashCode

        public int hashCode()
        Returns a hash code for this fraction.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code computed from numerator and denominator