IMG

 
IMG
IMG   IMG
  Welcome to GTAForums! Be sure to check out the Grand Theft Auto V Forum.

You are not registered! (If you are, click here to login) Registering is fast, free and easy and allows you to instantly reply to any topic on GTAForums.
Why wait? Click here to register your own unique username and become part of the ever-growing community!


( Log In | Register | Revalidate Validation E-mail )
Quick Log-In:
  IMG
       
>
  Reply to this topicStart new topicStart Poll

 Simplifying a Fraction

 C#
 
metacortex  
Posted: Monday, May 14 2012, 16:00
Quote Post


3,4-dihydroxyphenethylamine
Group Icon
Group: Members
Joined: Jan 17, 2011

uk.gif

XXXXX



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
PM
  Top
 

 
K^2  
Posted: Monday, May 14 2012, 16:21
Quote Post


Vidi Vici Veni
Group Icon
Group: Zaibatsu
Joined: Apr 14, 2004

us.gif

Member Award




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.
PMMSN
  Top
 

 
metacortex  
Posted: Monday, May 14 2012, 16:37
Quote Post


3,4-dihydroxyphenethylamine
Group Icon
Group: Members
Joined: Jan 17, 2011

uk.gif

XXXXX



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
PM
  Top
 

 

1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)

0 Members:

Topic Options Reply to this topicStart new topicStart Poll
Search topic for posted by (exact match)



 
IMG IMG