Adding Fraction to current fraction?

Tolkingdom

I am doing something quick for a lesson

Fraction.getGCF is a working method to get the GCF with a numerator and denominator Fraction.getLCD is also a working method that gets the LCD of 2 Denominators

I am trying to add a numerator(n2) and denominator(d2) to the existing numerator and denominator.

first i reduce the incoming fraction to simplest terms , then try to add them together

public void addFraction(int n2, int d2)
{
    int gcf = Fraction.getGCF(n2, d2);
    n2 = n2/gcf;
    d2 = d2/gcf;
    numerator = (numerator*d2)+(denominator*n2);
    denominator = Fraction.getLCD(denominator, d2);
    this.reduce();
}

Is my Math wrong or something? Numerator = 1 Denominator = 8 (1/8) and n2 = 16 , d2 = 64 (16/64) so I'm trying to add 1/8 to 16/64 and it resulting in 3/2 after the final reduce. Before the final reduce it is resulting in 12/8

TL;DR how can i fix my numerator , its not producing the correct result with this program

NoseKnowsAll

You are combining two different algorithms. One is to perform a/b+c/d=(a*d+b*c)/(bd). The other is to perform a/b+c/d=(a*(lcd(b,d)/b) + c*(lcd(b,d)/d))/lcd(b,d)

numerator = numerator*d2+n2*denominator;
denominator = denominator*d2;

or

int lcd = Fraction.getLCD(denominator, d2);
numerator = numerator*(lcd/denominator)+n2*(lcd/d2);
denominator = lcd;

So in your example it would be:

(1/8) + (16/64) = (1*64+16*8)/(8*64) = 192/512 = 3/8

or

(1/8) + (16/64) = (1*(64/8) + 16*(64/64))/64 = 24/64 = 3/8

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related