Quantcast

Jump to content

» «
Photo

Simplifying a Fraction

2 replies to this topic
metacortex
  • metacortex

    3,4-dihydroxyphenethylamine

  • Members
  • Joined: 17 Jan 2011

#1

Posted 14 May 2012 - 04:00 PM

Hey everyone,

I am currently writing a small finger exercise in C# dealing with fractions. The class for the fractions is almost done, only the method of simplifying a fraction using Euclids algorithm for finding the GCD is kind of messy sad.gif



CODE

       public Fraction Simplify()
       {
           int divisor = this.GCD();
           if (divisor != 0)
           {
               Fraction tmpFrac = new Fraction((this.Numerator / divisor), (this.Denominator / divisor));
               return tmpFrac;
           }
           else
           {
               Fraction dummyFraction = new Fraction(1, 1);
               return dummyFraction;
           }
       }

CODE

       public int GCD()
       {
           int tmpNumerator = this.Numerator;
           int tmpDenominator = this.Denominator;

           if (tmpNumerator == 0 || tmpNumerator == tmpDenominator)
           {
               return tmpDenominator;
           }
           else
           {
               while ((tmpDenominator != 0) && (tmpNumerator != 0))
               {
                   if (tmpNumerator > tmpDenominator)
                   {
                       tmpNumerator = tmpNumerator % tmpDenominator;
                   }
                   else
                   {
                       tmpDenominator = tmpDenominator % tmpNumerator;
                   }
               }
           }
           return tmpNumerator;
       }



and these are the members:

CODE
       //members
       #region Members
       
       //private
       private int _numerator;
       private int _denominator;
       private string _absoluteValue;

       //public
       public int Numerator
       {
           get
           {
               return this._numerator;
           }
           private set
           {
               this._numerator = value;
           }
       }

       public int Denominator
       {
           get
           {
               return this._denominator;
           }
           private set
           {
               this._denominator = value;
           }
       }

       public string AbsoluteValue
       {
           get
           {
               return this._absoluteValue;
           }
           private set
           {
               this._absoluteValue = value;
           }
       }

       #endregion Members

This method is based on an improved algorithm PatrickW posted in this forum a long time ago.


I don't really see any mistake in my code, nor do I recognise any mathematical issues :S

Help is very much appreciated. smile.gif

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States

#2

Posted 14 May 2012 - 04:21 PM

Your GCD algorithm returns tmpNumerator regardless of whether it was tmpDenominator or tmpNumerator that went to zero. So it will have 50% chance of returning 0 for GCD when it really isn't.

metacortex
  • metacortex

    3,4-dihydroxyphenethylamine

  • Members
  • Joined: 17 Jan 2011

#3

Posted 14 May 2012 - 04:37 PM

f*ck.

Please excuse my explicit language but that was just plain stupid blush.gif .

Here is the new method, just in case anyone is looking for something like this:

CODE
       public int GCD()
       {
           int gcd = 0;
           int tmpNumerator = this.Numerator;
           int tmpDenominator = this.Denominator;
           if (tmpNumerator == 0 || tmpNumerator == tmpDenominator)
           {
               return tmpDenominator;
           }
           else
           {
               while((tmpDenominator != 0) && (tmpNumerator != 0))
               {  
                 if(tmpNumerator > tmpDenominator)
                 {
                       tmpNumerator = tmpNumerator % tmpDenominator;
                 }
                 else
                 {
                       tmpDenominator = tmpDenominator % tmpNumerator;
                 }
               }
               if (tmpNumerator == 0)
               {
                  gcd = tmpDenominator;
               }
               else if (tmpDenominator == 0)
               {
                   gcd = tmpNumerator;
               }
           }
           return gcd;
       }


Thank you a lot K^2, have some cookies smile.gif

cookie.gif cookie.gif cookie.gif

cheers




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users