Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-251

Improve the Fraction class to avoid overflow problems and increase features



    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.2
    • 2.0
    • None
    • None
    • java version "1.6.0_12"
      Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
      Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

      Windows XP

      Eclipse Ganymede 3.4.1


      Hello everybody,

      I've improved and extended the Fraction class for my personal needs, but it could be nice to incorporate it within the commons-math library., either by modifying the current Fraction class, or by creating a new class which could be called AdvancedFraction or something like that. I've tried to keep all features from the original Fraction class.

      New features :

      • I needed to avoid overflow problem. As a result, I've changed numerator and denominator to BigInteger and all associated methods / constructors.
      • I've also added other methods and static fields such as :
        public String toString()
        public Fraction add(int i)
        public Fraction subtract(int i)
        public Fraction multiply(int i)
        public Fraction divide(int i)
        public Fraction pow(int exponent)
        public double pow(Fraction exponent)
        public BigDecimal bigDecimalValue()
        public Fraction clone()
        public double percentageValue()
        public static Fraction ONE_HALF = new Fraction(1, 2)
        public static Fraction ONE_QUARTER = new Fraction(1, 4)
      • I've made sure that the original Junit test within the FractionTest class are all right (I've just commented the overflow tests)
      • I've made sure that checkstyle is all right without any tabs

      Obviously, some things need to be reworked or improved :

      • Some methods are still naively implemented, such as
        public Fraction add(Fraction fraction)
        public Fraction subtract(Fraction fraction)

        because I don't have converted the original private Fraction addSub(Fraction fraction, boolean isAdd) method.

      • The original private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) constructor may use BigDecimal instead of double. I'm not sure it is relevant.
      • Junit tests have to be written with news methods (such as pow)
      • Some minor adaptations have to be done in the FractionFormat class (change getDenominator() by getDenominatorAsInt() for instance)

      You'll find my work in the attached zip file :

      • The Fraction and FractionTest classes
      • The javadoc generated with public and private fields and methods (to show you a complete overview)

      Thank you.


        1. NewFraction.zip
          95 kB
          Benjamin Croizet

        Issue Links



              luc Luc Maisonobe
              bct Benjamin Croizet
              0 Vote for this issue
              0 Start watching this issue