0

With the help of this link in SO Calculate a Ratio in C# i am able to do calculation of ratio in c#

var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)
static int GCD(int a, int b) {
    return b == 0 ? a : GCD(b, a % b);
}

Now how can i compare the 2 ratio values

 if A:B< 1:10 my result =1

and

 if A:B >1:10  my result=0

So how can I add this ratio comparison in C# how to check my ration is less than 1:10 ??

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • Could you please clarify for me 1:10 bigger that 1:50 or opposite? – Andrey Tretyak Dec 26 '15 at 00:43
  • May be if you are using GCD to calculate ratio you can treat to ratio as fraction and compare results of the division? like 1:10 = 0.1 and 1:50 = 0.02 then 0.1 > 0.02 and 1:10 > 1:50. – Andrey Tretyak Dec 26 '15 at 00:47
  • @AndreyTretyak i hope the edit in question answers doubt – Sebastian Dec 26 '15 at 00:49
  • I don't quite follow what you are trying to do, so I won't submit a formal answer at this time. Without clarifying more, it might be easier to work with fractions. A ratio (X:Y) expresses that for every 'X of these' I have 'Y of those'. Which means the concentration of X is X/(X+Y) and the concentration of Y is Y/(X+Y) – Élie Dec 26 '15 at 00:49
  • What is the _result_ in the question? There is no _result_ in the posted code. – Dmitry Dec 26 '15 at 00:50
  • i am following GCD approach – Sebastian Dec 26 '15 at 00:55
  • Would "if (A/(A+B) < (1/11)) result = 1; if (A/(A+B) > (1/11) result = 0;" suffice? – Élie Dec 26 '15 at 00:58

1 Answers1

0

You could create a class for ratio (with positive denominator).
It also makes sense to implement the IComparable interface and comparison operators:

public class Ratio : IComparable<Ratio>
{
    public readonly int A;
    public readonly int B;

    public Ratio(int a, int b)
    {
        if (b < 0)
        {
            a = -a;
            b = -b;
        }

        A = a;
        B = b;
    }

    public int CompareTo(Ratio other)
    {
        return A * other.B - B * other.A;
    }

    public static bool operator <(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) < 0;
    }

    public static bool operator >(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) > 0;
    }

    public static bool operator >=(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) >= 0;
    }

    public static bool operator <=(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) <= 0;
    }

    public static bool operator ==(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) == 0;
    }

    public static bool operator !=(Ratio r1, Ratio r2)
    {
        return r1.CompareTo(r2) != 0;
    }
}

Sample usage:

var r1 = new Ratio(1, 10); // 1/10
var r2 = new Ratio(2, 20); // 2/20

Console.WriteLine(r1 >= r2); // true
Console.WriteLine(r1 > r2);  // false
Console.WriteLine(r1 < r2);  // false
Console.WriteLine(r1 == r2); // true

var r1 = new Ratio(1, 10); // 1/10
var r2 = new Ratio(1, 11); // 1/11

Console.WriteLine(r1 >= r2); // true

var r1 = new Ratio(1, 10); // 1/10
var r2 = new Ratio(1,  9); // 1/9

Console.WriteLine(r1 >= r2); // false

Or you could just use a logic of the CompareTo method above.
For 2 ratios a/b and c/d where b,d > 0:
a/b > c/d is true when and only when a*d > c*b.

Dmitry
  • 13,797
  • 6
  • 32
  • 48