Accuracy: 1.0E-3 | Stern-Brocot OPTIMIZED | Eppstein | Richards
You may want to copy the table to your text editor for full screen viewing.
Void Seek(ref int a, ref int b, int ainc, int binc, Func f) / Binary seek for the value where f() becomes false. Throw new ArgumentOutOfRangeException("accuracy", "Must be > 0 and (lower_d + ud) * (value + maćrror) (ln + upper_n) Public Fraction RealToFraction(double value, double accuracy)
Here is some very non-idiomatic (and hence, hopefully, readable even if you don't know the language) Python that implements this algorithm. Test your middle against x, and either make the middle the upper, the lower, or return your final answer. At each iteration if your lower is a/b and your upper is c/d your middle is (a+c)/(b+d). Now do a binary search until you are close enough. To do that you subtract off the floor to get the number in the range [0, 1), then your lower estimate is 0, and your upper estimate is 1.
To find that you have to walk the Stern-Brocot tree. However it is not guaranteed to give you the smallest fraction that is within a distance epsilon of a real number. This gives a fractional sequence which is guaranteed to converge very, very rapidly. The algorithm that the other people have given you gets the answer by calculating the Continued Fraction of the number.