This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Add-ons & Links / Standard Packages / NumberTheory /


It is frequently useful to approximate arbitrary real numbers with a nearby rational. The built-in function Rationalize gives a rational approximation to a single real number.

In certain situations, such as the computation of gear ratios or the setting of musical scales, one wants to get a list of rational numbers that approximates a given list of real numbers. You could simply apply Rationalize to each element in the list, but it is very likely that the elements in the result would not be related to each other in a simple way. To get a simultaneous rational approximation that preserves the relationships among the given numbers, you should use ProjectiveRationalize or AffineRationalize.

Getting a simultaneous rational approximation.

The second argument in ProjectiveRationalize and AffineRationalize controls the number of digits that are to be considered significant in the numbers given in the first argument. If no second argument is given all digits are considered significant. Note that the convention used for specifying the tolerances in this package differs from the one used in the built-in Rationalize.

This loads the package.

In[1]:= <<NumberTheory`Rationalize`

Here is the ratio of rational approximations. The result is close to , but it is a very awkward way to approximate it.

In[2]:= Rationalize[N[3 Pi], 6]/
Rationalize[N[11 Pi], 6]


This computes a simultaneous rational approximation that preserves the simple relationship between the given numbers.

In[3]:= AffineRationalize[{N[3 Pi], N[11 Pi]}, 6]


This ratio is much simpler.

In[4]:= %[[1]]/%[[2]]


The ratios of the integers returned by ProjectiveRationalize approximate the ratios of the corresponding irrationals.

In[5]:= ProjectiveRationalize[{N[3 Pi], N[11 Pi]}]