tags:

views:

1154

answers:

5

I thought this would be simple, but searching Google didn't seem to help.

I'm basically trying to write a function which will return a ratio as a string (eg 4:3) when supplies with two integers (eg 800 and 600).

string GetRatio(Int A, Int B) {
    // Code I'm looking for
    return Ratio;
}

Many Thanks

+2  A: 

Are you basically trying to get the greatest common denominator - GCD for the two numbers and then dividing them by that and thus getting your string ?

I.e: 800 : 600 ; the greatest common denominator = 200 thus 4:3.

This would be able to deal with all integer numbers. Sorry for not sending the code, but I think that from this on it should be simple enough.

public int GCD(int a, int b)

{
    while (a != 0 && b != 0)
    {
         if (a > b)
            a %= b;
         else
            b %= a;
    }
     if (a == 0)
         return b;
     else
         return a;
}

// Using Konrad's code: 

var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)
Tomas Pajonk
If that's how it's done, then yes :) Code please...
GateKiller
This site isn't for premade code, GateKiller. Take the knowledge you get from answers, write code.
Cody Brocious
Tomas: Thanks for the code :)
GateKiller
@Cody Brocious: Please can you direct me to the rules which state you cannot ask for code?
GateKiller
There are no rules saying you can't ask for code. But the whole purpose is to give people a knowledge base where they can find answers to problems. Just code snippets rarely explains everything about the solution and about the problem.
Filip Ekberg
Filip++ I couldn't agree more. Give a man a fish and all that.
Cody Brocious
So is this where Gatekiller's hatred of 'plzsendtehcodez', came from? :-)
George Stocker
+10  A: 

I don't have a ready made code but it should be fairly simple. Namely, determine the GCD and work with it:

var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)

And a very basic function for calculating the GCD, using the Euclidean algorithm:

static int GCD(int a, int b) {
    return b == 0 ? a : GCD(b, a % b);
}
Konrad Rudolph
Thank you, this is exactly what I was looking for :)
GateKiller
<pedantic> Syntax Error @ return b == 0 ? a : gcd(b, a % b); function gcd not defined </pedantic>
Aamir
Konrad Rudolph
A: 

Are you really passing doubles (e.g. 3.14159) into the method, or just integers / whole numbers? If just passing integers, then GCD calculation is trivial.

A: 

Other commentators have given good solutions for integers; if you really have to deal with floating-point values, though, you'll need something else. In general, two real numbers won't have a clean ratio that can be prettily printed; what you want is the closest rational approximation. Probably the best way to go about finding that is just to compute the continued fraction expansion of the quotient; Mark Dominus gives a good introduction to those on his blog.

kquinn
+1  A: 

Having played with such things in the past, I'll just add that dealing with signed values can get ugly. Let me suggest that the simplest way to handle signed values is to apply Konrad's approach to the absolute values of your original numbers, then prepend a '-' to the resulting string if the original values have different signs.

Using this approach, the Greatest Common Divisor of -100 and -35 is 5, for a ratio of 20:7. If the original inputs had been either of the pairs (-100 and 35) or (100 and -35), you'd still get a GCD of 5, and an initial result of 20:7, but the final answer would be -20:7 (i.e. standardized form regardless of which input was negative, just as both -6/2 and 6/-2 = -3).

joel.neely