Skip to content

Rationalize ​

Finds a simple rational number near x.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageRationalize✓
Rationalize(x)the simplest rational within about one machine epsilon of x.
Rationalize(x, tolerance)the simplest rational within the given tolerance of x.
Rationalize(x, tolerance?)Finds a simple rational number near x.
Typed signatures
TypePackageApplies
(real, real<0..>?) -> rationalcompute-enginealways
(value, number?) -> valueanalyticreplaces compute-enginealways
(value, number?) -> valuecombinatoricsreplaces analyticalways

Domain: Arithmetic

Details
  • Finds a simple rational number near x.
  • With one argument, it finds the simplest fraction within about one machine epsilon of x's current value; a second argument sets an explicit tolerance instead.
  • An already-exact rational (or integer) is returned unchanged.
  • Builds the approximation via continued fractions, the same technique behind classic approximations like π≈355/113\pi \approx 355/113.

Examples

3 more kept as data, checked against the oracles
Basic #
live ↗
InRationalize(0.1)\mathrm{Rationalize}(0.1)Out110\frac{1}{10}

An exact integer is returned unchanged

live ↗
InRationalize(2)\mathrm{Rationalize}(2)Out22
live ↗
InRationalize(2.5)\mathrm{Rationalize}(2.5)Out52\frac{5}{2}

Recognizes a float that is, to machine precision, a simple fraction

live ↗
InRationalize(0.333 333 333 333 333 3)\mathrm{Rationalize}(0.333\,333\,333\,333\,333\,3)Out13\frac{1}{3}
live ↗
InRationalize(6.75)\mathrm{Rationalize}(6.75)Out274\frac{27}{4}

A second argument sets the tolerance

live ↗
InRationalize(π,0.01)\mathrm{Rationalize}(π, 0.01)Out227\frac{22}{7}
Scope #

Any exact numeric expression, to any tolerance

live ↗
InRationalize(e2,2−12)\mathrm{Rationalize}(e^{\sqrt{2}}, 2^{-12})Out21853\frac{218}{53}
live ↗
InRationalize(2,0.001)\mathrm{Rationalize}(\sqrt{2}, 0.001)Out4129\frac{41}{29}
live ↗
InRationalize(−0.125)\mathrm{Rationalize}(-0.125)Out−18-\frac{1}{8}

The rational with the smallest denominator within 0.001 of π\pi -- 201/64201/64, not compute-engine's own closer-but-larger-denominator continued-fraction convergent 333/106333/106

live ↗
InRationalize(π,0.001)\mathrm{Rationalize}(π, 0.001)Out20164\frac{201}{64}

A tolerance finer than a double can resolve is still honoured -- the simplest fraction within 10−1610^{-16} of π\pi itself, not of N(π)N(\pi), which is already 1.2×10−161.2 \times 10^{-16} away

live ↗
InRationalize(π,1.0⋅10−16)\mathrm{Rationalize}(π, 1.0\cdot10^{-16})Out245 850 92278 256 779\frac{245\,850\,922}{78\,256\,779}

Rationalizes every approximate number inside an expression

live ↗
InRationalize(1.2+6.7x)\mathrm{Rationalize}(1.2+6.7x)Out67x10+65\frac{67x}{10}+\frac{6}{5}

Threads element-wise over a list; compute-engine's own Rationalize takes only a single real

live ↗
InRationalize([0.5,0.25,0.2])\mathrm{Rationalize}(\bigl\lbrack0.5, 0.25, 0.2\bigr\rbrack)Out[12,14,15]\bigl\lbrack\frac{1}{2}, \frac{1}{4}, \frac{1}{5}\bigr\rbrack

A zero tolerance gives the exact rational the machine double denotes -- N(π)N(\pi)'s own dyadic value, 884279719003555/248884279719003555/2^{48}, not merely a rational within some epsilon of it

live ↗
InRationalize(N(π),0)\mathrm{Rationalize}(N(π), 0)Out884 279 719 003 555281 474 976 710 656\frac{884\,279\,719\,003\,555}{281\,474\,976\,710\,656}
Applications #

Recovers a simple fraction from a decimal measurement

live ↗
InRationalize(0.75)\mathrm{Rationalize}(0.75)Out34\frac{3}{4}

Recognizes a ratio of consecutive Fibonacci numbers in a decimal approximation of 1/φ1/\varphi

live ↗
InRationalize(0.618 034,0.000 1)\mathrm{Rationalize}(0.618\,034, 0.000\,1)Out5589\frac{55}{89}
Properties #

With an explicit tolerance, recovers a classic approximation: 22/7 is within 0.01 of π

live ↗
InRationalize(π,0.01)=227\mathrm{Rationalize}(π, 0.01)=\frac{22}{7}OutTrue\mathrm{True}
Possible issues #

Without enough repeating digits to reach machine precision, it just returns the exact decimal as a fraction rather than snapping to 1/3

live ↗
InRationalize(0.333 333)\mathrm{Rationalize}(0.333\,333)Out333 3331 000 000\frac{333\,333}{1\,000\,000}

A loose tolerance can snap to something far simpler -- 0 is within 0.5 of 0.1

live ↗
InRationalize(0.1,0.5)\mathrm{Rationalize}(0.1, 0.5)Out00

Without a tolerance, a float with no simple fraction nearby still becomes a fraction with a large denominator

live ↗
InRationalize(N(π))\mathrm{Rationalize}(N(π))Out80 143 85725 510 582\frac{80\,143\,857}{25\,510\,582}
Neat examples #

Milü, Zu Chongzhi's 5th-century approximation of π, accurate to six decimal places

live ↗
InRationalize(π,0.000 001)\mathrm{Rationalize}(π, 0.000\,001)Out355113\frac{355}{113}
About
Examples24
Checked againstWolfram Language 24 agree
CrosswalkWolfram Language

See also: Round