[C++] Fraction Class

This topic is 3205 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys, I just wrote a little class which can deal with fractions. Here it is:
class fraction
{
public:
//so the user can access the numerator and denomenater
float num;
float den;

//constructor which allows for instant assignment. i.e. fraction x(5, 10)
fraction(float n=0, float d=0) {num=n; den=d;}

//allows for post declaration assigning
void assign(float n, float d);

//prints the fraction
void print();

//returns the decimal value of the fraction
float decimal() {return num/den;}

//returns the percentage value of the fraction
float percentage() {return num/den*100;}

private:

};

//function definitions
blah blah blah


But my question is, can I make a method which can make a fraction object from a decimal. For example, something like this. fraction x(0.5); which then automatically converts it into 1/2. Cheers, Darkcrobat

Share on other sites
Quote:
 can I make a method which can make a fraction object from a decimal.

Yes, it's doable, but not with float representation of numerator and denumerator (they are inaccurate) since they do not have adequate precision.

For example: 88.3129831, or 0.000123e-24 would almost certainly exceed the ranges.

In theory, every floating point number (float type) should be representable with two integers of adequate precision, but not with built-in int, float or any other numeric type.

Share on other sites
So, basically with standard c++, it's impossible? Or is there some other type which I don't know about yet?

Share on other sites
Quote:
 Original post by darkcrobatSo, basically with standard c++, it's impossible? Or is there some other type which I don't know about yet?
With the standard types offered in hardware, it is impossible to do accurately.

You can *approximate* a floating point value, using an integer numerator and denominator, but you can't accurately reproduce the value without much larger types.

However, since floats are themselves approximations (and decimals are also approximations), it likely doesn't matter.

Share on other sites
Well you know that 0.5 is 50/100 = 1/2

Use that idea and try it out. For example :

0.1234 = 1234/10000 = 617/5000
0.1 = 10/100 = 1/10;
0.01 = 1/100
1 = 100/100
5 = 500/100
2.5 = 250/100
101 = 101*100/100

So you need to construct the numerator and the denominator separately. Do you
see some kind of pattern?

Also have a look at geometric series,

http://www.purplemath.com/modules/series5.htm

Share on other sites
It's a computer science problem, not a language problem. The decimal doesn't encode the same information as the fraction, as much as you might expect it to - because it isn't completely precise. If it did work, you wouldn't really need the fraction class in the first place. :)

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013549
×