I need help please.

This is a discussion on I need help please. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new here, I ran into a small problem while writing an math extension library. So far I have ...

  1. #1
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Post I need help please.

    Hi, I'm new here, I ran into a small problem while writing an math extension library. So far I have a Rational class that represents rational numbers as listed below:
    Code:
    class Rational {
      friend bool operator==(const Rational& l, const Rational& r);
      friend bool operator!=(const Rational& l, const Rational& r);
      friend bool operator>=(const Rational& l, const Rational& r);
      friend bool operator<=(const Rational& l, const Rational& r);
      friend bool operator<(const Rational& l, const Rational& r);
      friend bool operator>(const Rational& l, const Rational& r);
    public:
      Rational();
      ~Rational();
      Rational(int n, unsigned d = 1);
      Rational(int n, int d);
      Rational(const Rational& r);
      Rational& operator-(void);
      Rational& operator^(int exp);
      Rational& operator=(const Rational& r);
      Rational& operator+=(const Rational& r);
      Rational& operator-=(const Rational& r);
      Rational& operator*=(const Rational& r);
      Rational& operator/=(const Rational& r);
      Rational& operator++(void);
      Rational& operator--(void);
      Rational operator++(int);
      Rational operator--(int);
      Rational operator+(const Rational& r);
      Rational operator-(const Rational& r);
      Rational operator*(const Rational& r);
      Rational operator/(const Rational& r);
      operator double(void) const;
      double operator^(const Rational& r);
      void print(void) const;
    private:
      int num;
      unsigned den;
      unsigned gcd(int x, unsigned y);
      void reduce(void);
    };
    All implementations for above is done but I can't seen to convert floating point numbers into rational numbers, is there an algorithm to do this? It would be better if it doesn't attempt to convert irrational floating point numbers.

    I need an advice as how to write this, so any help would be appreciated.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    http://mathforum.org/dr.math/faq/faq.fractions.html might help. J
    ust search google and you will most likely find some sort of help.
    Woop?

  3. #3
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    I saw the rational class in the boost library but that did not give a good comprehensive guide as to how to convert from fraction to rational, instead they just use templates to pass floating point as an type parameter. Mine only deals with whole rational numbers.

    I need some sort of floating point parser, so I know how much x10 it needs to get all the digits to the right of the floating point. Any ideas?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21