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.