简体   繁体   English

在Java中减去负分数的问题

[英]Issue with subtracting a negative fraction in Java

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). 例如,如果我执行1/4-(-2/4),则得到(-3/4)。

Here is my minus method for subtracting fractions. 这是我减去分数的减法。

   /**
   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... 这是我的reduce()方法,以防万一...

  /**
   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. 我认为仅将+切换为-可能导致了我的问题。

   /**
   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;
   }

i think the problem is in your Fraction Minus function: 我认为问题出在您的小数减法函数中:

public Fraction minus(Fraction toUse)
{
  int newNum = ( (numerator * toUse.denominator)-
                       (toUse.numerator * denominator)
                 );
  int newDen = denominator * toUse.denominator;
  Fraction minusFraction = new Fraction(newNum, newDen);

  return minusFraction;
}

ex: 1/4-(-2/4)..... here the toUse.numerator is -2 and toUseDenominator is 4 what your code was doing it made your toUse fraction(-2/4) as base fraction and was getting subtracted from the original base Fraction(1/4) ie (-2/4)-(1/4)...hence the result -3/4 例如:1/4-(-2/4).....此处的toUse.numerator为-2,toUseDenominator为4,您的代码正在执行的操作使您的toUse分数(-2/4)作为基本分数,并且从原始基本分数(1/4)中减去,即(-2/4)-(1/4)...因此结果-3/4

hopefully it works 希望它能工作

Two problems: 两个问题:

  1. You are subtracting in the wrong order. 您的减法顺序错误。

    Your javadoc says: 您的javadoc说:

     /** Subtracts a fraction from another fraction. @param toUse, the fraction to subtract. @return minusFraction, the result after subtraction. */ 

    Which implies this.numerator - toUse.numerator 这意味着this.numerator - toUse.numerator

    But here you do the opposite: 但在这里您做相反的事情:

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

    Which, when you call 1/4.minus(-2/4) ends up computing: 当您调用1/4.minus(-2/4)1/4.minus(-2/4)结束计算:

     int newNum = (-2*4) - (1*4) = -9 int newDen = 4 * -4 = -16 // Your new fraction will have -9/-16 which you reduce to -3/-4 // (which is how you hold -3/4) 

    Just switch the code to: 只需将代码切换为:

     /** 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 = ((numerator * toUse.denominator) - (toUse.numerator * denominator)); int newDen = denominator * toUse.denominator; Fraction minusFraction = new Fraction(newNum, newDen); return minusFraction; } 

    Now when 现在,当

     int newNum = (1*4) - (-2*4) = 9 int newDen = 4 * -4 = -16 
  2. This brings us to the second problem. 这给我们带来了第二个问题。 Your reduce method relies on the denominator to determine if the number is negative or positive. 您的reduce方法依靠分母来确定数字是负数还是正数。 But it is the numerator that should really carry the sign because you do the subtraction on the numerator. 但是分子应该真正携带符号,因为您需要对分子进行减法运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM