0

I am currently using Rationalize after NumberForm in order to check if the some values of my function are in the Rationals. Unfortunately, using NumberForm completely breaks Mathematica's output.

I have the following minimal reproducible example:

For[i = 1, i < 20, i += 1, (
  x = Rationalize@NumberForm[i/(i + 1), 32];
  If[Element[x, Rationals], Print[x]]
  )]

The above code checks whether $\frac{i}{i+1}$ is rational, for integer $1\leq i<20$. Unexpectedly, it outputs nothing.

Even more unexpectedly, the following code, which is supposed to print $x$ no matter what, also outputs nothing:

For[i = 1, i < 20, i += 1, (
  x = Rationalize@NumberForm[i/(i + 1), 32];
  If[Element[x, Rationals], Print[x], Print[x]]
  )]

Note the difference in the If clause.

So why does Mathematica fail to detect the rational values, even after Rationalize?

I am using Mathematica 11.1.


Note that I added the bugs tag. Feel free to remove it if adequate.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Klangen
  • 1,009
  • 5
  • 14
  • 5
    Take a look at https://mathematica.stackexchange.com/q/92226/5478, does it answer your question? – Kuba Aug 13 '19 at 18:54
  • 1
    SeedRandom[12345]; For[i=1,i<20,i+=1, j=If[RandomReal[]<0.5,i,RandomReal[{1.0,20.0}]]; x0=j/(j+1.0); x1=Rationalize@x0; Print[If[Denominator[x1]<=40,x1,x0]//InputForm] ] – Manuel --Moe-- G Aug 14 '19 at 20:47

0 Answers0