Sign in to follow this  
MasterDario

Fraction class Reducing problem

Recommended Posts

MasterDario    120
Hey everyone, I have a fraction class that does a bunch of things such as add,subtract,mult,reduce, e.t.c fractions. The problem i'm having is with my conversion constructor if i pass in a double number into my class (ex: Fraction b(3.90), I would expect it to return 3 9/10 but instead its returning 3 899999/1000000. What's even stranger is that if i pass in (3.70) its returning what it should 3 7/10. So now im just completely confused. Here are my two class member functions to convert from decimal to fraction and reduce. I hope someone can tell me whats going wrong. Thanks! Dario // Converts a double into a Fraction Fraction::Fraction(double ss) { WholeNum = int(ss); Numer = int((ss - WholeNum)* 1000000); Denom = 1000000; ReduceFraction(); //Call Reduce Fraction Function } // Function that Reduces the whole number,numerator // And denominator of a Fraction Object void Fraction::ReduceFraction() { int TempWholeNumber; int ReduceVal; // Make sure there is no 0 in the denominator if(Denom == 0) { cout << endl << endl; cout << "Error: 0 Found in Denominator. Press Any Key to Exit Program" << endl; cout << endl << endl; exit(1); } // Check to see if Fraction can be simplified further // Before finding the GCD. if(Numer >= Denom) { TempWholeNumber = Numer/Denom ; Numer = Numer % Denom; WholeNum = WholeNum + TempWholeNumber; } // Calls Function to find the GCD of the Fraction Object ReduceVal = abs(GCD(Numer,Denom)); // Assigns the new numerator and denominator to the fraction Numer = Numer / ReduceVal; Denom = Denom / ReduceVal; }

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Google for an article called "What Every Computer Scientist Should know about Floating Point Numbers." To summarize, lots of decimal numbers can't be represented within floats, even simple ones like 3.8. So you get unexpected rounding errors that naturally lead to weird fractions being generated.

CM

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Quote:
Original post by MasterDario
wow, thats pretty interesting. I had no idea. Is there an actual way to fix this?

Not really. You can use fixed point math, but that requires doing everything by hand. Or a fractional class, which you're currently working on. Within the confines of float/double, though, the best you can do is account for it as best as possible.

One possibility for your case is to have a "fudge" function that attempts to convert to a more reasonable fraction. I've never tried writing one, so I'm not sure where you'd start, but its a thought.

*edit: And apparently, you can fix it. Nevermind [sad]

CM

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by MasterDario
wow, thats pretty interesting. I had no idea. Is there an actual way to fix this?


There is no way to "fix" it, but the next best thing would be an algorithm that could come up with a reasonable fraction. For that algorithm, look up "Farey Series".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this