*(**EDIT**: In response to grumpy comments, No it isn't homework. I am working on pitch detection, taking an array of potential harmonic peaks, and attempting to construct candidates for fundamental frequency. So, it is actually a very practical question.)* 

Consider the best fractional approximations for (eg) pi, ordered by increasing denominator: 3/1, 22/7, 355/113, ...

Challenge: create a *tidy* C algorithm that will generate the n'th quotient approximation a/b for a given float, returning also the discrepancy.

calcBestFrac(float frac, int n, int \* a, int \* b, float \* err) {...}

The best technique I believe is **continued fractions**

Take away the fractional part of pi, and you get 3  

Now, the remainder is 0.14159... = 1/7.06251..

So the next best rational is 3 + 1/7 = 22/7  

Take away the 7 from 7.06251 and you get 0.06251.. Roughly 1/15.99659..

Call it 16, then the next best approximation is  

3 + 1/(7 + 1/16) = 355/113

However, this is far from trivial to convert into clean C code. I will post if I get something tidy. In the meanwhile, someone may enjoy it as a brainteaser.