3

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
sn6uv
  • 565
  • 3
  • 7
  • 1
    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
  • 1
    @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 thought this would be thus or differently, do you have a suspect? –  Jul 19 '16 at 11:33
  • To make a somewhat stronger statement, @Louis that past post is on no way relevant to this one (I don't think it was relevant to much of anything, to be frank). – Daniel Lichtblau Jul 19 '16 at 18:55
  • 3
    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
  • @DanielLichtblau What criterion does Rationalize use for the 2-argument case with eps = 0, i.e., Rationalize[some_number, 0]? Using a few test numbers (N[Pi], 0.1, 399847593.00000068), I tried applying your criterion to the latter, and they all evaluated to False. – theorist Apr 04 '17 at 19:21
  • @theorist Offhand I do not know if any criterion is used when the second arg is 0. It just works with the bit pattern in that case. – Daniel Lichtblau Apr 04 '17 at 19:59
  • @DanielLichtblau What I've found, empirically, is that I can ensure Rationalize produces a rational number exactly equal to the decimal "num" if I evaluate Rationalize[num\z, 0]wherez >= 2*(Length@RealDigits[num][[1]] ) + 1(and where "num" is the actual decimal I type in). E.g., Length@RealDigits[0.39984759300000068][[1]] = 16, andRationalize[0.39984759300000068`33, 0]gives a fraction exactly equal to the decimal, butRationalize[0.39984759300000068`32, 0]` does not. I''d like to understand why that's the cutoff. – theorist Apr 05 '17 at 02:56

0 Answers0