繁体   English   中英

C#中的分数比较问题

[英]Fraction comparison issue in C#

> =方法的比较存在问题:

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

它似乎不起作用?!

例如:

25> = 6/5返回FALSE

谁能对此有所启发?

以下是要求的代码。 我希望有人能发现问题所在。

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

CrossReducePair更改数字之间的关系。 您的(3/4,5/9)=(1/4,5/3)的示例非常明显。 如果您要进行乘法运算,则交叉减法是有意义的,但如果您只是进行比较,则没有意义。

CrossReducePair方法似乎没有多大意义。 如评论中所述,它将(3/4, 5/9)(1/4, 5/3) (3/4, 5/9) (1/4, 5/3) 注意: 3/4 > 5/9 ,但1/4 < 5/3

另外,拥有一个CompareTo方法来修改对象也是一个非常糟糕的主意。

暂无
暂无

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

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