I need an algorithm to add two fractions. Its easy to do with your head but when I went to write it i was all like HTF?
So yeah, its for C++ by the way so if you feel like writing it do so.
I have to add:
[u]frac1.numerator[/u] + [u]frac2.numerator[/u]
frac1.denominator frac2.denominator
[QUOTE=ThePuska;19533166]frac1.numerator * frac2.denominator + frac2.numerator * frac1.denominator
-----------------------------------------------------------------------------------------
frac1.denominator * frac2.denominator[/QUOTE]
Yup, just find a common denominator by multiplying the two denominators (is not necessarily the smallest common multiple, but it works)
1/2 + 1/4
(1*4)/(2*4) + (1*2)/(4*2)
4/8 + 2/8
6/8
Then you can simplifying by looking for common factors from 2 to the numerator, so we get
6/8 (check from 2 to 6)
(2*3)/(2*4)
3/4
If the numerator is bigger than the denominator, we want to divide to find the amount of wholes, and then leave the remainder as a fraction
whole = numerator / denominator
remaining_numerator = numerator % denominator
[QUOTE=nullsquared;19533512]Yup, just find a common denominator by multiplying the two denominators (is not necessarily the smallest common multiple, but it works)
1/2 + 1/4
(1*4)/(2*4) + (1*2)/(4*2)
4/8 + 2/8
6/8
Then you can simplifying by looking for common multiples from 2 to the numerator, so we get
6/8 (check from 2 to 6)
(2*3)/(2*4)
3/4[/QUOTE]
If you want to simplify it, it might be best to factorize the original numerators and denominators
[editline]10:10PM[/editline]
And simply remove all factors that occur both in either of the numerators and either of the denominators
edit: Wait no I don't know what I'm doing, that'd only apply if you were multiplying them
[QUOTE=ThePuska;19533580]If you want to simplify it, it might be best to factorize the original numerators and denominators
[editline]10:10PM[/editline]
And simply remove all factors that occur both in either of the numerators and either of the denominators[/QUOTE]
That's essentially what I meant, I accidentally said multiples instead of factors.
Haha, thats pretty funny, I had to do this for a lab in my Computer Sciences class. It was pretty easy though.
[QUOTE=nullsquared;19533721]That's essentially what I meant, I accidentally said multiples instead of factors.[/QUOTE]
Ignore my post, you need to factorize them after adding anyway
I don't need to simplify for this assignment, its just practice using structures which we are just learning about. Thanks for the help though.
Or if you want some fun just divide the fractions and add them to get a decimal.
(1/2) + (1/4) = 0.75
Then convert 0.75 back to a fraction.
I did this a long time ago, I think it was something like this:
[PHP]
double dec = 0.75;
//this should get rid of the decimal
//if not, just truncate it
int mod = 1000000; //mod will be our denomenator too
double num = dec * mod;
int numerator = (int) num;
int gcd = gcd(numerator, mod);
numerator = numerator/gcd;
mod = mod/gcd;
printf("%d/%d", numerator, mod);
//Euclid's Algorithm
int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b, a % b);
}[/PHP]I haven't tested this though. But I did it about a year and a half ago.
[QUOTE=PvtCupcakes;19545681]Or if you want some fun just divide the fractions and add them to get a decimal.
(1/2) + (1/4) = 0.75
Then convert 0.75 back to a fraction.[/QUOTE]
All fun and games until binary floating point loses it's precision
[QUOTE=turby;19546174]All fun and games until binary floating point loses it's precision[/QUOTE]
Thats true, but it usually loses it precision by sticking a 1 or something in the last decimal place which I think is the 6th place. So you get a number like 1.000001
If you make that function I posted only accurate to the first 5 decimals (multiply by 10^5) you shouldn't have that problem because it'll just truncate that 1.
[QUOTE=PvtCupcakes;19546344]Thats true, but it usually loses it precision by sticking a 1 or something in the last decimal place which I think is the 6th place. So you get a number like 1.000001
If you make that function I posted only accurate to the first 5 decimals (multiply by 10^5) you shouldn't have that problem because it'll just truncate that 1.[/QUOTE]
Or, you can just user integer addition and multiplication for the fractions and have practically infinite precision :downs:
Sorry, you need to Log In to post a reply to this thread.