# How do you determine the closest whole number ratio for a given real number?

I've been messing around with formulas for musical notes (trying not to read anything that would help me unless I get stuck) and currently I'm at a point where I'm trying to get a function that describes each note's frequency in terms of it's surrounding notes and specifically to find if their respective frequencies are close to a whole number ratio.

I can use a computer to calculate a table of every whole number ratio that I expect to see but I'm wondering if there is any other approach to this?

-
Continued fractions might be a good start? en.wikipedia.org/wiki/Continued_fraction –  copper.hat May 22 '12 at 15:57

In general there is no closest whole number ratio. For example, the ratio between C and the next lower F♯ is exactly $\sqrt 2$. (In an equal-tempered 12-tone system.) As was known to the Greeks, there are no integers $a$ and $b$ with $\frac ab = \sqrt 2$. But there are arbitrarily good approximations: $\frac 32$, $\frac75$, $\frac{17}{12}$, … . The sequence continues with each fraction $\frac ab$ followed by $\frac{a+2b}{a+b}$, and each one is closer than the ones before.
The general rule is that every irrational number $\alpha$ has a unique continued fraction representation, and that if one truncates this continued fraction at some point, one obtains a good rational approximation to $\alpha$ with relatively small numerator and denominator. The further along one truncates the continued fraction, the better the approximation, but the larger its numerator and denominator will be. One can show that these so-called "convergents" of the continued fraction are among the very best rational approximations to $\alpha$ that exist, in the sense that any closer rational approximation must have a larger denominator.
convergents… are among the very best rational approximations…: to complete this, we have the two facts (which you surely know): (1) if you define the badness of a rational approximation $p/q$ as the value $|q\alpha-p|$, then all the best rational approximations (better than any with a smaller denominator) are precisely the convergents, (2) if you define the badness as $|x-p/q|$, then the best are precisely all convergents plus the semiconvergents $(p_k+np_{k+1})/(q_k+nq_{k+1})$ for $n$ from either $a_{k+2}/2$ or $a_{k+2}/2+1$ to $a_{k+2}$. shreevatsa.wordpress.com/2011/01/10/not –  ShreevatsaR May 31 '12 at 10:04