Negative to Positive

I have a series of if else if statements inside an overloaded << function that is to control how negative values are displayed with fractions. They all see to work except for one. If there is a fraction such as -2/-3 it should display as 2/3. So I am multiplying the numerator and the denominator by -1 to convert the sign but it is not working. I have used this strategy to move a negative denominator into a positive numerator so 2/-3 is -2/3 and that worked, so I can't see why it is not working with a double negative.

Referring to the third else if.

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
  std::ostream& operator<<(std::ostream & out, const Fraction & right)
	{
		int wholeNumber, remainder, numToNegative, denToPositive;

		if (right.denominator == 1) {
			out << right.numerator;
		}
		else if (right.numerator < 0) {
			out << right.numerator << "/" << right.denominator;
		}
		else if (right.denominator < 0) {
			out << right.numerator * -1 << "/" << right.denominator * -1;
		}
		else if (right.numerator < 0 && right.denominator < 0) {
			out << right.numerator * -1 << "/" << right.denominator * -1;
		}
		else if (right.numerator > right.denominator) {

			wholeNumber = right.numerator / right.denominator;
			remainder = right.numerator % right.denominator;
			out << wholeNumber << "+" << remainder << "/" << right.denominator;

		}
		else out << right.numerator << "/" << right.denominator;

		return out;
	}
Last edited on
You have:
1
2
3
4
5
if (right.numerator < 0) {
}
else // previous if was false, so right.numerator<0 is false
if ( right.numerator < 0 && B ) {
}

We do know that the second if cannot possibly succeed.

1
2
3
4
5
6
7
if ( A ) {
  if ( B ) {
    // A and B
  } else {
    // A but not B
  }
}



EDIT: However ...
The numerator<0 is meaningless. You will do the * -1 for all numerators if denominator<0.
Last edited on
Just a thought...
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
31
32
33
34
#include <iostream>
using namespace std;

struct Fraction {
  int numerator;
  int denominator;
};

bool testAndAbs(int &x)
{
  bool result = x < 0;
  if (result)
    x = -x;
  return result;
}

std::ostream & operator<<(std::ostream & out, const Fraction & right)
{
    Fraction w = right;

    bool f1 = testAndAbs(w.numerator);
    bool f2 = testAndAbs(w.denominator);
    bool isNeg = f1 != f2;

    out << (isNeg?"-":"") << w.numerator << "/" << w.denominator;
    return out;
}

int main()
{
    Fraction f{-2,-3};
    cout << f << endl;
    return 0;
}

if you are already doing a routine that reduces the fractions somewhere (?) then I would put it in there. so if you had -4/-8 just fix it to 1/2 all in one place.

to do that you can just double down ;)
double d = num/den;
den = abs(den); //no matter what.
if (d>0) num = abs(num);
else num = abs(num)*-1;

or something to that effect.
Thanks to all you guys for the insight. I got it working. I ended up using my simplify function to handle negatives and the overloaded << function to display whole numbers, positive/negative mixed numbers, and any other fractions from the simplify function.
Topic archived. No new replies allowed.