Class SafeFraction


  • public class SafeFraction
    extends Object
    A small wrapper for Fraction instances that avoids an arithmetic overflow by using double based operations in case of an exception.
    Author:
    Ullrich Hafner
    • Constructor Summary

      Constructors 
      Constructor Description
      SafeFraction​(int numerator, int denominator)
      Creates a new fraction instance that wraps the specified fraction with safe operations.
      SafeFraction​(org.apache.commons.lang3.math.Fraction fraction)
      Creates a new fraction instance that wraps the specified fraction with safe operations.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.apache.commons.lang3.math.Fraction add​(org.apache.commons.lang3.math.Fraction summand)
      Adds the value of another fraction to the value of this one, returning the result in reduced form.
      org.apache.commons.lang3.math.Fraction multiplyBy​(org.apache.commons.lang3.math.Fraction multiplier)
      Multiplies the value of this fraction by another, returning the result in reduced form.
      org.apache.commons.lang3.math.Fraction subtract​(int numerator, int denominator)
      Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
      org.apache.commons.lang3.math.Fraction subtract​(org.apache.commons.lang3.math.Fraction subtrahend)
      Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
    • Constructor Detail

      • SafeFraction

        public SafeFraction​(org.apache.commons.lang3.math.Fraction fraction)
        Creates a new fraction instance that wraps the specified fraction with safe operations.
        Parameters:
        fraction - the fraction to wrap
      • SafeFraction

        public SafeFraction​(int numerator,
                            int denominator)
        Creates a new fraction instance that wraps the specified fraction with safe operations.
        Parameters:
        numerator - the numerator of the fraction
        denominator - the denominator of the fraction
    • Method Detail

      • multiplyBy

        public org.apache.commons.lang3.math.Fraction multiplyBy​(org.apache.commons.lang3.math.Fraction multiplier)
        Multiplies the value of this fraction by another, returning the result in reduced form. Since there might be an arithmetic exception due to an overflow, this method will handle this situation by calculating the multiplication based on the double values of the fractions.
        Parameters:
        multiplier - the fraction to multiply by
        Returns:
        a Fraction instance with the resulting values
      • subtract

        public org.apache.commons.lang3.math.Fraction subtract​(org.apache.commons.lang3.math.Fraction subtrahend)
        Subtracts the value of another fraction from the value of this one, returning the result in reduced form. Since there might be an arithmetic exception due to an overflow, this method will handle this situation by calculating the subtraction based on the double values of the fractions.
        Parameters:
        subtrahend - the fraction to subtract
        Returns:
        a Fraction instance with the resulting values
      • subtract

        public org.apache.commons.lang3.math.Fraction subtract​(int numerator,
                                                               int denominator)
        Subtracts the value of another fraction from the value of this one, returning the result in reduced form. Since there might be an arithmetic exception due to an overflow, this method will handle this situation by calculating the subtraction based on the double values of the fractions.
        Parameters:
        numerator - the numerator of the fraction
        denominator - the denominator of the fraction
        Returns:
        a Fraction instance with the resulting values
      • add

        public org.apache.commons.lang3.math.Fraction add​(org.apache.commons.lang3.math.Fraction summand)
        Adds the value of another fraction to the value of this one, returning the result in reduced form. Since there might be an arithmetic exception due to an overflow, this method will handle this situation by calculating the addition based on the double values of the fractions.
        Parameters:
        summand - the fraction to add
        Returns:
        a Fraction instance with the resulting values