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]


How does Rationalize actually work?

  • 1
    $\begingroup$ 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. $\endgroup$ Jul 19, 2016 at 10:51
  • $\begingroup$ Have a look Does Mathematica get Pi wrong? $\endgroup$
    – user9660
    Jul 19, 2016 at 11:10
  • 1
    $\begingroup$ @IstvánZachar, the part you quoted with regards to 10^-4 is specific to Rationalize[x] forms. $\endgroup$
    – sn6uv
    Jul 19, 2016 at 11:14
  • $\begingroup$ @Louis, I'm not sure if that's relevant. Even machine precision should provide ample accuracy for this computation. $\endgroup$
    – sn6uv
    Jul 19, 2016 at 11:16
  • 3
    $\begingroup$ 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. $\endgroup$ Jul 19, 2016 at 18:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.