Simpliying function

I'm trying to write a function that accepts 2 integers, simplify them and return it as a fraction.
Here is what i have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 void Rational::simplify()
{
	bool positive = false;
	bool negative = false;

	if (numerator < 0)
	{
		numerator =- numerator;
		positive = true;
	}

	if (denominator < 0)
	{
		denominator = -denominator;
		negative = true;
	}

	int gcd = GCF(numerator, denominator);

	if (gcd = !0)
	{
		numerator = numerator / gcd;
		denominator = denominator / gcd;

	}

	if (positive^negative)
		numerator =- numerator;

}


it's part of a class implementation file. The issue is that the fraction doesnt get simplified.
Here is the gcf function to find the gcf

1
2
3
4
5
6
7
8
9
int GCF(int a, int b)
{
	if (b%a == 0)
		return a;
	if (a > b)
		return GCF(b, a);
	else
		return GCF(b%a, a);
}


Some help?
Last edited on
-1-
You don't need two variables for sign. Use just one.
Remember, -1/-2 == 1/2.

-2-
You are assigning the value of 1 to gcd on line 20:

    !0 → 1
    gcd = 1

Hence, numerator = numerator / 1.

You probably meant if (gcd != 0), which is more succinctly written if (gcd).

Hope this helps.
What is line 27 doing?
@Arslan7041
if (positive ^ negative)

I think the OP meant this :
if ((positive ^ negative) < 0)

https://graphics.stanford.edu/~seander/bithacks.html#DetectOppositeSigns
Topic archived. No new replies allowed.