Approximate a floatingpoint number by a rational number
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
numeric::rationalize(object
, <Exact  Minimize  Restore
>, <digits
>)
numeric::rationalize(object)
replaces all
floatingpoint 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 floatingpoint number f is approximated by a rational number r satisfying f  r < ε f.
Note:
With the options 
The default precision is digits = DIGITS
.
The user defined precision must not be larger than the internal
floatingpoint precision set by DIGITS
: an error occurs for digits
> DIGITS
.
The function is sensitive to the environment variable DIGITS
.
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))])
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)
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
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:
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)

An arbitrary MuPAD^{®} object 

A positive integer (the number of decimal digits) not bigger
than the environment variable 

Specifies the strategy for approximating floatingpoint numbers
by rational numbers. This is the default strategy, so there is no
real need to pass Any real floatingpoint number f ≠ 0.0 has a unique representation
With integer exponent and 1.0 ≤ mantissa <
10.0. With the option . This guarantees a relative precision of  

Specifies the strategy for approximating floatingpoint 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 See Example 3.  

Specifies the strategy for approximating floatingpoint numbers
by rational numbers. This strategy tries to restore rational numbers
obtained after elementary arithmetical operations
applied to floatingpoint numbers. E.g., for rational
See Example 4. 
If the argument is an object of some kernel domain, then it is returned with all floatingpoint operands replaced by rational numbers. An object of some library domain is returned unchanged.
object
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.