 # how do you perform math on a function in an array?

I'm writing a program to control a cnc saw with an arduino. I have most of the code and the saw done, now I'm working on features. You can enter a value as a decimal (into a character array), but I want to be able to enter for example "1 1/2" and have it convert it to "1.5". I can make the " " into a "+" making it easier to set up the actual equation "1+(1/2)", but I'm stumped on actually getting it to do the math.

I would tackle it this way:

(1) Isolate and evaluate the whole part as an int value (2) Isolate and evaluate the numerator as an int value (3) Isolate and evaluate the denominator as an int value (4) multiply denominator times whole part and add numerator to produce the numerator of a (possibly improper) fraction (5) add one-half of the denominator to this numerator for rounding, if desired (6) divide this numerator (cast to float) by the denominator (also cast to float) (7) the quotient is the value you're after.

Thanks Morris, I had time to sleep on it and came to a similar conclusion to yours. I'm going to isolate the whole number to an int, the numerator to an int, and the denominator to an int like you said. then i will just divide numerator by denominator and place in a float. Add the whole to the float and there's your answer.

then i will just divide numerator by denominator and place in a float. Add the whole to the float and there's your answer.

The answer will be wrong, in a lot of cases. Work through what you said with 1 1/2 as input. int w=1, int n=1, int d =2. n/d = 0. Storing that in a float won't make it other than 0.

What Paul said.

In addition, unless your saw is going to handle really large stock and/or you are going to allow really large-denominator fractions, there may be some speed and code size advantage to postponing use of float variables until the very last step.