Advertisement

fractions

Started by October 21, 2000 01:18 PM
12 comments, last by pizzaman 24 years, 2 months ago
how do i write fractions in c++. im making a program that converts farenheight to celsius, but i dont know the code to write a fraction thanks to anyone that helps me out
i am the best
Okay, fractions are the same as decimals....if you say 5/9 or whatever, it''ll automatically divide for you. I don''t think that there''s a way to get it to stay in fraction form. Hope that helped - Cloxs.
Advertisement
Make a class and store the numerator & denominator seperately.
You'll need a gcd (greatest common divisor) function to reduce the fraction properly.

and overload the -,+,*,/, <<, >> operators

...
Whoops, guess I should read the whole thing
    float C;float F:float m;float b;m = 9.0f/5.0f;b=32;cout<<"Enter ºC"<<endl;cin>>C;cout<<C<<"ºC is "<<(C-b)*m<<" ºF"<<endl;    


Edited by - Magmai Kai Holmlor on October 22, 2000 4:04:45 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
If you don't mind losing a little bit of accuracy, a fraction is simply division:

5/9 = 5 divided by 9.

Some fractions, such as 1/3, are a repeating decimal:

1/3 = 1.33333333333333333. . .

It continues forever, so your computer has to round it off.

If you don't want to lose accuracy, you can do what Magmai suggested, and make them their own class. Essentially, you can use the new class as a new datatype. I did that for a programming assignment, and I overloaded practically all of the operators. The only thing I have to do is make something to convert decimals to fractions, and I could use it for any programs that need it. Does anybody have any ideas on how to do that?


"Whoever performs only his duty is not doing his duty." --Bahya ibn Pakuda

Edited by - CobraA1 on October 22, 2000 3:11:02 PM
"If a man does not keep pace with his companions, perhaps it is because he hears a different drummer. Let him step to the music he hears, however measured or far away"--Henry David Thoreau
Hmm.. The best way I can think of to convert decimals to fractions would be to program it with pre-recognized patterns.

Although that obviously limits functionality.

This would be slow but, you could have a loop cycle through denominators and keep going until a close enough match is found.

For a denominator of 3 you''d calculate:

1/3 = 0.3333..

Take the fractional part of the number you''re trying to convert (2.33333... would be 0.3333...) and divide 0.3333... into it.

The closer the result comes to being an integral number the closer you''ve come to finding the proper denominator..

For example, say I want to convert 2.66667 to fractions. We can look at it and say that it probably came from the fraction

8/3

In our algorithm we would calculate 1/3, which is 0.333333...

Now we calculate 0.666667/0.33333... which we find to be:

2.00003000003... Now that is very close to being integral (no fractinoal part) and based on some threshold type error metric we''d conclude that 3 is the denominator of our fraction.

The numerator can be calculated by multiplying the denominator (in this case 3) by the integral part of the number to be converted (integral part of 2.666667 is 2) and then add the integral part of 0.666667/0.33333... (which is 2) to obtain 8/3.

I hope people followed this... Not a very good explanation but I think the idea got across (I hope).

Any comments on this algo?

I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Ok. Well for anyone that cares I whipped up this algo to see if it worked.

And well, it does.

Results are pretty good. Though if you feed it really long decimals you begin to lose accuracy due to the limitations of the floating point format.

Anyone interested I will gladly send the example code.



--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Advertisement
quote: Original post by Promiscuous Robot

Anyone interested I will gladly send the example code.



I am. Please do, my E-mail address is: moss-taylor@geocities.com.

Actually, I was looking for an algorithm to read decimals, not to convert infinite decimals, but here''s an easy way to do that: Find the part that repeats, and place the same number as nines as the number of digits in the pattern.

.333333333: The number that repeats is 3. 3/9 = 1/3.

.303030303: The numbers that repeat is 30. 30/99 = 10/33.

.037037037: The numbers that repeat are 037. 037/999 = 1/27.

Of course, this only works if the pattern actually repeats itself. I still would like the code if it can read decimals into something like an array. Then I can convert decimals to fractions.




"Whoever performs only his duty is not doing his duty." --Bahya ibn Pakuda
"If a man does not keep pace with his companions, perhaps it is because he hears a different drummer. Let him step to the music he hears, however measured or far away"--Henry David Thoreau
Hmm.. Thats a much easier method than mine .

I''m unclear what you mean by ''read decimals into something like an array''.

Do you mean create an array of ints, each one holding a separate digit of the real number??

eg. int foo[5] = { 3, 3, 3, 3, 3 };

would represent the real number 0.33333.

Or do you want to parse a float out of string. If so, then
atof() does nicely.

--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
I''m looking for the opposite of atof(). I just need to convert from a float into something that I can read individual digits from.


"Whoever performs only his duty is not doing his duty." --Bahya ibn Pakuda
"If a man does not keep pace with his companions, perhaps it is because he hears a different drummer. Let him step to the music he hears, however measured or far away"--Henry David Thoreau
to convert from a rational decimal (doesn''t repeat forever) just multiply by 10 n times, where n is as many times as it takes to make the number an integer then divide by 10 ^ n (10 to the power of n)

This topic is closed to new replies.

Advertisement