2012-03-24 3 views
-1

> = 방법의 비교에 문제가있다 :분수 비교 문제는

public static bool operator >=(Fraction left, Fraction right) 
    { 
     return left.CompareTo(right) >= 0; 
    } 

작동하지 않는 것은?! 예를 들어

:

25> = FALSE 6/5 반환

사람이에 도움이 되거 수 있습니까?

다음은 요청 된 코드입니다. 누군가가 잘못된 점을 알아낼 수 있기를 바랍니다.

/// <summary> 
    /// Compares an object to this Fraction 
    /// </summary> 
    /// <param name="obj">The object to compare against (null is less than everything)</param> 
    /// <returns>-1 if this is less than <paramref name="obj"></paramref>, 
    /// 0 if they are equal, 
    /// 1 if this is greater than <paramref name="obj"></paramref></returns> 
    /// <remarks>Will convert an object from longs, doubles, and strings as this is a value-type.</remarks> 
    public int CompareTo(object obj) 
    { 
     if (obj == null) 
      return 1; // null is less than anything 

     Fraction right; 

     if (obj is Fraction) 
      right = (Fraction)obj; 
     else if (obj is long) 
      right = (long)obj; 
     else if (obj is double) 
      right = (double)obj; 
     else if (obj is string) 
      right = (string)obj; 
     else 
      throw new ArgumentException("Must be convertible to Fraction", "obj"); 

     return this.CompareTo(right); 
    } 

    /// <summary> 
    /// Compares this Fraction to another Fraction 
    /// </summary> 
    /// <param name="right">The Fraction to compare against</param> 
    /// <returns>-1 if this is less than <paramref name="right"></paramref>, 
    /// 0 if they are equal, 
    /// 1 if this is greater than <paramref name="right"></paramref></returns> 
    public int CompareTo(Fraction right) 
    { 
     // if left is an indeterminate, punt to the helper... 
     if (this.m_Denominator == 0) 
     { 
      return IndeterminantCompare(NormalizeIndeterminate(this.m_Numerator), right); 
     } 

     // if right is an indeterminate, punt to the helper... 
     if (right.m_Denominator == 0) 
     { 
      // note sign-flip... 
      return -IndeterminantCompare(NormalizeIndeterminate(right.m_Numerator), this); 
     } 

     // they're both normal Fractions 
     CrossReducePair(ref this, ref right); 

     try 
     { 
      checked 
      { 
       long leftScale = this.m_Numerator * right.m_Denominator; 
       long rightScale = this.m_Denominator * right.m_Numerator; 

       if (leftScale < rightScale) 
        return -1; 
       else if (leftScale > rightScale) 
        return 1; 
       else 
        return 0; 
      } 
     } 
     catch (Exception e) 
     { 
      throw new FractionException(string.Format("CompareTo({0}, {1}) error", this, right), e); 
     } 
    } 


    /// <summary> 
    /// Cross-reduces a pair of Fractions so that we have the best GCD-reduced values for multiplication 
    /// </summary> 
    /// <param name="frac1">The first Fraction [WILL BE MODIFIED IN PLACE]</param> 
    /// <param name="frac2">The second Fraction [WILL BE MODIFIED IN PLACE]</param> 
    /// <remarks>Modifies the input arguments in-place!</remarks> 
    /// <example>(3/4, 5/9) = (1/4, 5/3)</example> 
    public static void CrossReducePair(ref Fraction frac1, ref Fraction frac2) 
    { 
     // leave the indeterminates alone! 
     if (frac1.m_Denominator == 0 || frac2.m_Denominator == 0) 
      return; 

     long gcdTop = GCD(frac1.m_Numerator, frac2.m_Denominator); 
     frac1.m_Numerator = frac1.m_Numerator/gcdTop; 
     frac2.m_Denominator = frac2.m_Denominator/gcdTop; 

     long gcdBottom = GCD(frac1.m_Denominator, frac2.m_Numerator); 
     frac2.m_Numerator = frac2.m_Numerator/gcdBottom; 
     frac1.m_Denominator = frac1.m_Denominator/gcdBottom; 
    } 
+1

당신의'CompareTo' 메소드를 보여주세요 ... –

+1

당신은 Fraction 클래스를 볼 필요가 있습니다, 특히 CompareTo – BlackBear

+0

'Fraction' 클래스에 대한 코드를 보여줄 수 있습니까? – Ryan

답변

2

CrossReducePair 방법은별로 의미가없는 것처럼 보입니다. 주석에 언급 된대로 (3/4, 5/9)(1/4, 5/3)으로 변환됩니다. 참고 : 3/4 > 5/9, 그러나 1/4 < 5/3.

또한 개체를 수정하는 CompareTo 메서드를 사용하는 것은 매우 나쁜 생각입니다.

5

CrossReducePair는 숫자 간의 관계를 변경합니다. 귀하의 (3/4, 5/9) = (1/4, 5/3)의 예는 매우 분명합니다. 교차 감소는 곱셈을하는 경우에 의미가 있지만, 단순히 비교하는 경우는 아닙니다.