Conversions

I have a fraction class, but I need to add functions that will convert a fraction to a float and vice versa. This is what I've been trying to work with. Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 Fraction Fraction (float rhs)
{
    Fraction temp;

    temp.WholeNumber = static_cast<int>(rhs);
    temp.Numerator = rhs  * 1000;
    temp.Denominator = 1000;
    ReduceFractions();

    return temp;
}
 operator float(const Fraction & rhs)
{
    Fraction lhs;
    float decimal;
    lhs.Numerator = (rhs.WholeNumber * rhs.Denominator) + rhs.Numerator;
    decimal = (float)rhs.Numerator / rhs.Denominator;
    return decimal;
}
Unless explicitly required by the assignment to have a WholeNumber member, you should get rid of that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Fraction
{
  int Numerator;
  int Denominator;

  Fraction( int n, int d = 1 ): Numerator( n ), Denominator( d ) { }

  operator float ()  // remember, this is a conversion operator
  {                  // (converting from *this Fraction to a float)
    ...
  }

  Fraction( float f )  // and this is a constructor
  {                    // (converting from a float to *this Fraction)
    ...
  }
};

Anything you do to that (except set the denominator to zero) is a valid number.

All member functions should then work from that. To convert to a string, for example:

1
2
3
4
5
6
7
8
9
ostream& operator << ( ostream& outs, const Fraction& fraction )
{
  int whole     = fraction.Numerator / fraction.Denominator;
  int remainder = fraction.Numerator % fraction.Denominator;

  outs << whole << "+" << remainder << "/" << fraction.Denominator;

  return outs;
}

Now, to convert to a float, remember that a rational value is obtained from any fraction -- divide the numerator by the denominator using floating point division:

8
9
10
11
operator float ()
{
  return (float)fraction.Numerator / fraction.Denominator;
}

The code to convert from a float is a little more involved. Get ready by reading here first:
http://www.mathsisfun.com/converting-decimals-fractions.html

13
14
Fraction( float f )
{

If you want to make life easier, just pick some (large-ish) number to multiply by:

15
16
  Numerator   = f * 10000;  // (I would use a bigger number, personally)
  Denominator = 1 * 10000;

Now to simplify (or reduce) the fraction, just as in the example link above. To do it, you'll need the GCD (also called GCF).
http://stackoverflow.com/questions/10956543/gcd-function-in-c-sans-cmath-library

The GCD only works on unsigned values, so you'll have to make sure to have both Numerator and Denominator unsigned. Don't forget to remember the sign, though.

17
18
19
20
  int sign = 1;
  if ((Numerator * Denominator) < 0) sign = -1;
  Numerator   = abs( Numerator );
  Denominator = abs( Denominator );


Find the GCD of the numerator and denominator. Then divide both by it.

20
21
22
  int d = gcd( Numerator, Denominator );
  Numerator   /= d;
  Denominator /= d;

Don't forget the sign!

 
  Numerator *= sign;

Hope this helps.
Topic archived. No new replies allowed.