numeric::rationalize

Approximate a floating-point number by a rational number

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

numeric::rationalize(object, <Exact | Minimize | Restore>, <digits>)

Description

numeric::rationalize(object) replaces all floating-point numbers in object by rational numbers.

An object of a library domain, characterized by domtype(extop(object,0))=DOM_DOMAIN is returned unchanged. For all other objects, numeric::rationalize is applied recursively to all operands. Objects of library domains can be rationalized if the domain has an appropriate map method. See Example 5.

A floating-point number f is approximated by a rational number r satisfying |f - r| < ε |f|.

    Note:   With the options Exact and Minimize, the guaranteed precision is . With Restore, the guaranteed precision is only .

The default precision is digits = DIGITS.

The user defined precision must not be larger than the internal floating-point precision set by DIGITS: an error occurs for digits > DIGITS.

Environment Interactions

The function is sensitive to the environment variable DIGITS.

Examples

Example 1

numeric::rationalize is applied to each operand of a composite object:

numeric::rationalize(0.2*a+b^(0.7*I))

numeric::rationalize([{poly(0.2*x, [x]), sin(7.2*PI) + 1.0*I},
                      exp(3 + ln(2.0*x))])

Example 2

We demonstrate the default strategy Exact:

numeric::rationalize(12.3 + 0.5*I),
numeric::rationalize(0.33333), 
numeric::rationalize(1/3.0)

numeric::rationalize(10^12/13.0),
numeric::rationalize(10^(-12)/13.0)

We reduce the precision of the approximation to 5 digits:

numeric::rationalize(10^12/13.0, 5),
numeric::rationalize(10^(-12)/13.0, 5)

Example 3

We demonstrate the strategy Minimize for minimizing the complexity of the resulting rational number:

numeric::rationalize(1/13.0, 5),
numeric::rationalize(1/13.0, Minimize, 5),
numeric::rationalize(0.333331, 5),
numeric::rationalize(0.333331, Minimize, 5),
numeric::rationalize(14.285, 5),
numeric::rationalize(14.2857, Minimize, 5),
numeric::rationalize(1234.1/56789.2),
numeric::rationalize(1234.1/56789.2, Minimize)

We compute rational approximations of π with various precisions:

numeric::rationalize(float(PI), Minimize, i) $ i = 1..10

Example 4

We demonstrate the strategy Restore for restoring rational numbers after elementary float operations. In many cases, also the Minimize strategy restores:

numeric::rationalize(1/7.3, Exact),
numeric::rationalize(1/7.3, Minimize),
numeric::rationalize(1/7.3, Restore)

However, using Restore improves the chances of recovering from roundoff effects:

numeric::rationalize(10^9/13.0, Minimize),
numeric::rationalize(10^9/13.0, Restore)

DIGITS:= 11:
numeric::rationalize(1234.56/12345.67, Minimize),
numeric::rationalize(1234.56/12345.67, Restore)

In some cases, Restore manages to recover from roundoff error propagation in composite arithmetical operations:

DIGITS:= 10:
x:= float(122393/75025):
y:= float(121393/75025):
z := (x^2 - y^2)/(x + y)

numeric::rationalize(z, Restore)

The result with Restore corresponds to exact arithmetic:

rx := numeric::rationalize(x, Restore):
ry := numeric::rationalize(y, Restore):
rx, ry, (rx^2 - ry^2)/(rx + ry)

Note that an approximation with Restore may have a reduced precision of only digits/2:

x := 1.0 + 1/10^6:
numeric::rationalize(x, Exact),
numeric::rationalize(x, Restore)

delete x, y, z, rx, ry:

Example 5

The floats inside objects of library domains are not rationalized directly. However, for most domains the corresponding map method can forward numeric::rationalize to the operands:

Dom::Multiset(0.2, 0.2, 1/5, 0.3)

numeric::rationalize(%), map(%, numeric::rationalize, Restore)

Parameters

object

An arbitrary MuPAD® object

digits

A positive integer (the number of decimal digits) not bigger than the environment variable DIGITS. It determines the precision of the rational approximation.

Options

Exact

Specifies the strategy for approximating floating-point numbers by rational numbers. This is the default strategy, so there is no real need to pass Exact as a parameter to numeric::rationalize.

Any real floating-point number f ≠ 0.0 has a unique representation

With integer exponent and 1.0 ≤ mantissa < 10.0. With the option Exact, the float mantissa is replaced by the rational approximation

.

This guarantees a relative precision of digits significant decimals of the rational approximation.

Minimize

Specifies the strategy for approximating floating-point numbers by rational numbers. This strategy tries to minimize the complexity of the rational approximation, i.e., numerators and denominators are to be small.

The guaranteed precision of the rational approximation is digits.

See Example 3.

Restore

Specifies the strategy for approximating floating-point numbers by rational numbers. This strategy tries to restore rational numbers obtained after elementary arithmetical operations applied to floating-point numbers. E.g., for rational r, the float division f = 1/float(r) introduces additional roundoff, which the Restore algorithm tries to eliminate: numeric::rationalize(f, Restore) = 1/r. This strategy, however, is purely heuristic and will not succeed when significant roundoff is caused by arithmetical float operations!

    Note:   The guaranteed precision of the rational approximation is only digits/2!

See Example 4.

Return Values

If the argument is an object of some kernel domain, then it is returned with all floating-point operands replaced by rational numbers. An object of some library domain is returned unchanged.

Overloaded By

object

Algorithms

Continued fraction (CF) expansion is used with the options Minimize and Restore.

With Minimize, the first CF approximation satisfying the precision criterion is returned.

The Restore algorithm stops, when large coefficients of the CF expansion are found.

Was this topic helpful?