Function Resource

RealRationalize

Express a real number as a nearby rational number

ResourceFunction["RealRationalize"][r]

expresses real number r as a rational number p/q.

Details and Options

ResourceFunction["RealRationalize"] uses RealDigits to compute its answer.
Unlike Rationalize which returns rational numbers only under certain conditions, ResourceFunction["RealRationalize"] returns a rational numbers for most real number inputs.
ResourceFunction["RealRationalize"] works with complex numbers by operating on its real and imaginary parts directly.

Examples

Basic Examples (3) 

Express 0.5 as a rational number:

In[1]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][0.5]
Out[1]=

Express the machine precision approximation of π as a rational number:

In[2]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][N[Pi]]
Out[2]=

Express a 50-digit approximation of π as a rational number:

In[3]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][N[Pi, 50]]
Out[3]=

Scope (5) 

Integers are returned unchanged:

In[4]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][1]
Out[4]=

Rationals are returned unchanged:

In[5]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][1/3]
Out[5]=

Exact representations of irrational numbers are returned unchanged:

In[6]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][\[Pi]]
Out[6]=

RealRationalize also works with complex numbers by applying itself to the real and imaginary parts of that number:

In[7]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][RandomComplex[]]
Out[7]=

RealRationalize has the Listable attribute, so it automatically works with lists:

In[8]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][RandomReal[1, 10]]
Out[8]=

Properties and Relations (4) 

Converting the rational representation back to a real number typically yields the original real number:

In[9]:=
r = RandomReal[]
Out[9]=
In[10]:=
q = ResourceFunction[
  "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RealRationalize"][r]
Out[10]=
In[11]:=
N[q] === r
Out[11]=

Unlike Rationalize, which has specific constraints on which numbers are rationalized and which ones are not, RealRationalize always attempts to return the nearest a rational number using RealDigits as its guide:

In[12]:=
r = RandomReal[]
Out[12]=
In[13]:=
{Rationalize[r], ResourceFunction[
  "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RealRationalize"][r]}
Out[13]=

RealRationalize simply finds a close approximation, whereas Rationalize aims to find a human-friendly approximation:

In[14]:=
{ResourceFunction[
  "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RealRationalize"][0.333], Rationalize[0.333]}
Out[14]=
In[15]:=
 333/1000 - 0.333
Out[15]=
In[16]:=
 5998794703657501/18014398509481984 - 0.333
Out[16]=

RealRationalize may give slightly "better" results than Rationalize for certain special real number inputs:

In[17]:=
{ResourceFunction[
  "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RealRationalize"][0.5000000000001], Rationalize[0.5000000000001]}
Out[17]=
In[18]:=
1/2 - 0.5000000000001
Out[18]=
In[19]:=
4503599627371397/9007199254740992 - 0.5000000000001
Out[19]=

Possible Issues (2) 

No attempt is made to rationalize exact representations of irrational numbers:

In[20]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][Sqrt[2]]
Out[20]=

Convert these numbers to a machine or high-precision real first:

In[21]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RealRationalize"][N[Sqrt[2]]]
Out[21]=