2

Assume I want to calculate a ratio equality...

So,

 a      c
--- == ---
 b      d

Where a = 1.2, b = 2.4, d = 5.3

I know how to solve the equation (cross multiply and divide) easily; however, I end up with an answer of:

2.65
----
5.3

That's the right answer, but a BETTER answer would be:

 1
---
 2

In Objective-C, I can easily reduce a "normal" fraction using this class method:

+(NSString *)reduce: (int) x1 by: (int) y1
{
    int u = x1;
    int v = y1;
    int temp;

    while (v) {
        temp = u % v;
        u = v;
        v = temp;
    }
    if (u) x1 /= u;
    if (u) y1 /= u;
    return [NSString stringWithFormat:@"%i:%i",x1,y1];
}

But, I'm really unclear on how to deal with a fraction with a decimal...

How would I implement a method (or how would I approach the math problem) of making a decimal-fraction into a reduced fraction?

Note: Since this is a ratio that I'm interested in, the answer can't be a decimal. i.e. the right answer is 1/2 NOT .5. Ultimately, the idea is to be able to take something like, "An object is 5.35cm wide by 2.12cm long. If I wanted to make a duplicate of the item that is scaled to fit a 5.79cm wide opening, what should the length be? -- and, further, what is the reduced form of that ratio?"

Hope I'm explaining this correctly.

DrDavid
  • 904
  • 3
  • 9
  • 18
  • Ultimately, I guess I want to know how to apply the Euclidean algorithm to a decimal fraction, or if there's a different way to approach this problem...? – DrDavid May 15 '13 at 00:24
  • Looks like it might answer the question.. :) But, wish there was a nicer way to deal with it vs. making a floating point into an integer. Thanks! – DrDavid May 15 '13 at 00:39
  • 1
    When your inputs are floating point numbers but you would like to get a rational numbers as a result, at one step you have to convert floating points to rationals. It is probably best to do the conversion before the calculation so that you can apply rounding-free integer/rational arithmetic (and the Euclidean algorithm, which does not make sense in the realm of the reals). So just multiply nominators and denominators by a high enough power of ten to make them integral. – Marc May 16 '13 at 20:36

0 Answers0