# Problem with subtracting a negative fraction

• March 11th, 2014, 12:58 AM
Chassy13
Problem with subtracting a negative fraction
For some reason, I'm getting the correct result, but my negative sign is having issues. For example, if I do 1/4 - (-2/4), I get (-3/4).

Here is my minus method for subtracting fractions.

Code :

``` /** Subtracts a fraction from another fraction. @param toUse, the fraction to subtract. @return minusFraction, the result after subtraction. */ public Fraction minus(Fraction toUse) { int newNum = ((toUse.numerator * denominator) - (numerator * toUse.denominator)); int newDen = denominator * toUse.denominator; Fraction minusFraction = new Fraction(newNum, newDen);   return minusFraction; }```
Here is my reduce() method, just in case...

Code :

``` /** Reduces the fraction, if possible, to it's simplest form. Converts negative fractions to the form -x/y, or if -x/-y --> x/y */ private void reduce() { int lowest = Math.abs(numerator); int highest = Math.abs(denominator);   if (lowest > highest) { int temp = highest; highest = lowest; lowest = temp; }   while (lowest != 0) { int temp = lowest; lowest = highest % lowest; highest = temp; }   numerator /= highest; denominator /= highest;   if (denominator < 0) { numerator *= -1; denominator *= -1; } }```
I only switched an operator from my previous addition method, given here as well. I think only switching the + to a - may have caused my issue.

Code :

``` /** Adds two fractions together. @param toUse, the fraction to be added. @return plusFraction, the sum of the two fractions. */ public Fraction plus(Fraction toUse) { int newNum = ((toUse.numerator * denominator) + (numerator * toUse.denominator)); int newDen = denominator * toUse.denominator; Fraction plusFraction = new Fraction(newNum, newDen);   return plusFraction; }```
• March 11th, 2014, 01:52 AM
jpharte
Re: Problem with subtracting a negative fraction
I think you may want to change

int newNum = ((toUse.numerator * denominator)
- (numerator * toUse.denominator));

With

int newNum = ((numerator * toUse.denominator)
- (toUse.numerator * denominator));

- Jim