# Rationalize error

The docs state that "Rationalize[x,dx] yields the rational number with smallest denominator that lies within dx of x." However, testing this out it appears to be false.

Rationalize[Pi, 0.1]


22 / 7

The problem is that 16 / 5 is also within 0.1 of Pi and has a smaller denominator.

N[16 / 5 - Pi]


0.0584073

How does Rationalize actually work?

• Since N[22/7 - Pi] < N[16/5 - Pi], I assume, that Rationalize tries to minimize Abs[dx] though within reasonable time limits. Also, as the documentation states, it has to be true that Abs[p/q - x] < c/q^2, where c = 10^-4. – István Zachar Jul 19 '16 at 10:51
• Have a look Does Mathematica get Pi wrong? – user9660 Jul 19 '16 at 11:10
• @IstvánZachar, the part you quoted with regards to 10^-4 is specific to Rationalize[x] forms. – sn6uv Jul 19 '16 at 11:14
• @Louis, I'm not sure if that's relevant. Even machine precision should provide ample accuracy for this computation. – sn6uv Jul 19 '16 at 11:16
• I'd say the documentation could be worded better for the 2-argument case of Rationalize. There is an interplay between denominator of result, epsilon, and size of residual. If you take dx as replacing the default epsilon then I think the correct claim might be to the effect: ratPi = Rationalize[N[Pi], eps]; Abs[N[Pi] - ratPi] < N[eps]/Denominator[ratPi]^2. – Daniel Lichtblau Jul 19 '16 at 18:57