Class BigFraction

  • All Implemented Interfaces:
    java.lang.Comparable<BigFraction>

    public final class BigFraction
    extends java.lang.Object
    implements java.lang.Comparable<BigFraction>
    Can store any rational number.

    Simplification rules:

    • If the numerator is zero, then the denominator is one
    • The numerator holds the sign of the fraction, and the denominator is always positive
    • If the numerator is not zero, then the numerator and the denominator are coprime

    This class is immutable, meaning all operations return the result, and no operation can modify this fraction

    • Constructor Summary

      Constructors 
      Constructor Description
      BigFraction​(long numerator)
      Creates a BigFraction representing the given integer
      BigFraction​(long numerator, long denominator)
      Creates a BigFraction with the given numerator and denominator
      BigFraction​(java.math.BigInteger numerator)
      Creates a BigFraction representing the given integer
      BigFraction​(java.math.BigInteger numerator, java.math.BigInteger denominator)
      Creates a BigFraction with the given numerator and denominator
    • Field Detail

      • TWO

        public static final java.math.BigInteger TWO
        https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/math/BigInteger.java#l1182 The BigInteger constant two. (Not exported.) private static final BigInteger TWO = valueOf(2); since 9 it is public http://hg.openjdk.java.net/jdk/jdk11/file/tip/src/java.base/share/classes/java/math/BigInteger.java#l1277
    • Constructor Detail

      • BigFraction

        public BigFraction​(java.math.BigInteger numerator,
                           java.math.BigInteger denominator)
        Creates a BigFraction with the given numerator and denominator
        Parameters:
        numerator - The numerator
        denominator - The denominator
        Throws:
        java.lang.ArithmeticException - If denominator is zero
      • BigFraction

        public BigFraction​(long numerator,
                           long denominator)
        Creates a BigFraction with the given numerator and denominator
        Parameters:
        numerator - The numerator
        denominator - The denominator
        Throws:
        java.lang.ArithmeticException - If denominator is zero
      • BigFraction

        public BigFraction​(java.math.BigInteger numerator)
        Creates a BigFraction representing the given integer
        Parameters:
        numerator - The integer to represent
      • BigFraction

        public BigFraction​(long numerator)
        Creates a BigFraction representing the given integer
        Parameters:
        numerator - The integer to represent
    • Method Detail

      • parse

        public static BigFraction parse​(java.lang.String str)
        Parses a string into a fraction.

        If the input string does not contain a "/", then the input is assumed to be an integer. Otherwise, the numerator and denominator are separated by a single "/" character and possibly some whitespace

        Parameters:
        str - The string to parse
        Returns:
        A fraction represented by the input string
        Throws:
        ParseException - If the input string has invalid format, or the denominator is zero
      • parse

        public static BigFraction parse​(StringParser parser)
        Parses a fraction from a string parser
        Parameters:
        parser - The parser to parse from
        Returns:
        A fraction from the input
        Throws:
        ParseException - If the input has invalid format, or the denominator is zero
      • getNumerator

        public java.math.BigInteger getNumerator()
        Returns the numerator of this fraction
        Returns:
        The numerator of this fraction
      • getDenominator

        public java.math.BigInteger getDenominator()
        Returns the denominator of this fraction
        Returns:
        The denominator of this fraction
      • toBigDecimal

        public java.math.BigDecimal toBigDecimal​(java.math.MathContext mc)
        Converts this fraction to a BigDecimal, rounding with the given MathContext where necessary
        Parameters:
        mc - The math context for rounding
        Returns:
        A BigDecimal approximation of this fraction
      • toDouble

        public double toDouble()
        Converts this fraction to a double, rounding where necessary
        Returns:
        A double approximation of this fraction
      • add

        public BigFraction add​(BigFraction other)
        Adds this fraction to the other fraction
        Parameters:
        other - The fraction to add
        Returns:
        The result of the sum
      • add

        public BigFraction add​(java.math.BigInteger other)
        Adds this fraction to the given integer
        Parameters:
        other - The integer to add
        Returns:
        The result of the addition
      • add

        public BigFraction add​(long other)
        Adds this fraction to the given integer
        Parameters:
        other - The integer to add
        Returns:
        The result of the addition
      • subtract

        public BigFraction subtract​(BigFraction other)
        Subtracts the other fraction from this fraction
        Parameters:
        other - The fraction to subtract
        Returns:
        The result of the subtraction
      • subtract

        public BigFraction subtract​(java.math.BigInteger other)
        Subtracts the given integer from this fraction
        Parameters:
        other - The integer to subtract
        Returns:
        The result of the addition
      • subtract

        public BigFraction subtract​(long other)
        Subtracts the given integer from this fraction
        Parameters:
        other - The integer to subtract
        Returns:
        The result of the addition
      • multiply

        public BigFraction multiply​(BigFraction other)
        Multiplies this fraction with the other fraction
        Parameters:
        other - The fraction to multiply by
        Returns:
        The result of the multiplication
      • multiply

        public BigFraction multiply​(java.math.BigInteger other)
        Multiplies this fraction with the given integer
        Parameters:
        other - The integer to multiply by
        Returns:
        The result of the multiplication
      • multiply

        public BigFraction multiply​(long other)
        Multiplies this fraction with the given integer
        Parameters:
        other - The integer to multiply by
        Returns:
        The result of the multiplication
      • divide

        public BigFraction divide​(BigFraction other)
        Divides this fraction by the other fraction
        Parameters:
        other - The fraction to divide by
        Returns:
        The result of the division
        Throws:
        java.lang.ArithmeticException - If other is zero
      • divide

        public BigFraction divide​(java.math.BigInteger other)
        Divides this fraction by the given integer
        Parameters:
        other - The integer to divide by
        Returns:
        The result of the division
        Throws:
        java.lang.ArithmeticException - If other is zero
      • divide

        public BigFraction divide​(long other)
        Divides this fraction by the given integer
        Parameters:
        other - The integer to divide by
        Returns:
        The result of the division
        Throws:
        java.lang.ArithmeticException - If other is zero
      • negate

        public BigFraction negate()
        Returns -this
        Returns:
        The result of the negation
      • reciprocal

        public BigFraction reciprocal()
        Returns this-1
        Returns:
        The result of the reciprocation
        Throws:
        java.lang.ArithmeticException - If this fraction is zero
      • floor

        public java.math.BigInteger floor()
        Returns the largest integer k such that k <= this
        Returns:
        The floor of this fraction
      • ceil

        public java.math.BigInteger ceil()
        Returns the smallest integer k such that k >= this
        Returns:
        The floor of this fraction
      • round

        public java.math.BigInteger round()
        Returns the closest integer to this fraction. In the case of there being 2 closest integers, returns the higher of the two
        Returns:
        This fraction rounded to the nearest integer
      • signum

        public int signum()
        Returns the sign of this fraction
        Returns:
        -1 if negative, 0 if zero, 1 if positive
      • abs

        public BigFraction abs()
        Returns the absolute value of this fraction
        Returns:
        The absolute value of this fraction, always non-negative.
      • exp

        public BigFraction exp()
        Returns the exponential value of this fraction Using simple serie expansion
        Returns:
        The exponential value of this fraction.
      • log

        public BigFraction log()
        Returns the logarithm value of this fraction Using simple series expansion
        Returns:
        The logarithm value of this fraction.
      • compareTo

        public int compareTo​(BigFraction other)
        Specified by:
        compareTo in interface java.lang.Comparable<BigFraction>
      • compareTo

        public int compareTo​(java.math.BigInteger other)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object