views:

815

answers:

5

Hi folks...

Is it possible to store a fraction like 3/6 in a variable of some sort? When I try this it only stores the numbers before the /. I know that I can use 2 variables and divide them, but the input is from a single text field. Is this at all possible?

I want to do this because I need to calculate the fractions to decimal odds.

A bonus question ;) - Is there an easy way to calculate a decimal value to a fraction? Thanks..

+4  A: 

Well in short, there is no true way to extract the original fraction out of a decimal.

Example: take 5/10

you will get 0.5

now, 0.5 also translates back to 1/2, 2/4, 3/6, etc.

Your best bet is to store each integer separately, and perform the calculation later on.

John T
+3  A: 

I'm afraid there aren't any easy answers for you on this. For creating the fraction, you'll have to split the text field on the '/', convert the two halves to doubles, and divide them out. As for converting it back to a fraction, you'll have to crack open a math textbook and figure it out. (Even worse, a double is not actually precise—you may think it has 0.1 in it, but it really has 0.09999999999999998726 or something like that, so you'll have to choose a precision and go for it, or write some sort of fraction class that's based on a pair of integers.)

Brent Royal-Gordon
+2  A: 

The best thing to do is to implement a fraction class (or rational number class). Normally it would take a numerator and denominator and be able to provide a double, and do basic math with other fraction objects. It should also be able to parse and format fractions.

Rational Arithmetic on Rosetta Code looks like something good to start with.

Lou Franco
A: 

Is there an easy way to calculate a decimal value to a fraction?

If you limit your decimal values to a certain number of decimal points you could create a lookup table.

0.3333, 1/3
0.6666, 2/3
0.0625, 1/16
0.1250, 1/8
0.2500, 1/4
0.5000, 1/2
0.7500, 3/4
etc...

So if the user input 0.5 you pad it with 0's until you got 4 decimal places. You would then use the lookup table to return "1/2". The lookup table should probably be a dictionary of sorts.

It wouldn't be too difficult to do estimating either. For example, if the user entered 0.0624 you could easily select the value in the table closest to that decimal. In this case it would return "1/16."

Don't let typing/entering of the finite set of decimal/fraction pairs scares you (it's really not that large depending on the precision you choose).

If all else fails perhaps a google search would reveal a library that does this sort of this for you.

Hrm... Why the downvotes?
+3  A: 

The method, as been said, is to store the numerator and denominator, much in the way you can write it on paper.

for 'C' use the

GNU Multiple Precision Arithmetic Library

look for 'rational' in the docs.

Liran Orevi
The question is tagged C, yes. However, I think the author made a mistake and thought Obj-C and C are related. Still, a useful answer. +1.
strager
@strager: Objective-C and C are related, in that one is a strict superset of the other. GMP can be used with Obj-C, although it's probably overkill for this.
Adam Rosenfield