-
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.
-
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.
-
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?