We write software for managing recipes and are working on moving from an approximation based decimal to fraction conversion, for example, anything between 0.03125 and 0.09375 becomes 1/16 to a math based conversion. We are running into a few problems doing the conversion. The numbers we are dealing with here come from unit conversions of foods within a recipe.
What we need to determine is how many significant decimal points to use when converting. For example .0625 @ .01 sig decimals = 1/14 but at .001 sig decimals we get the proper 1/16. However at times we'll end up with 659999966621399 which at .01 sig decimals is 2/3 and at .001 sig decimals is 29/44.
Is there any way we can determine how best to handle this scenario?
I know this is not a programming site but here is the formula we're using
class Rational @rationalize: (float, epsilon = .01) -> epsilon = .01 rational = bigRat(float) denominator = 0 numerator = undefined error = undefined loop denominator++ numerator = Math.round((rational.numerator * denominator) / rational.denominator) error = Math.abs(rational.minus(numerator / denominator)) break unless error > epsilon fraction = bigRat(numerator, denominator) intPart = fraction.floor() fracPart = fraction.minus(intPart) [intPart.valueOf(), fracPart.numerator.valueOf(), fracPart.denominator.valueOf()]