DADiSP Worksheet Functions > Function Categories > Numerical Formatting > RAT

 

RAT

Purpose:

Calculates a rational approximation of a value within a tolerance.

Syntax:

RAT(x, tol)

(n, d) = RAT(x, tol)

x

-

A scalar or series, the input value.

tol

-

Optional. A real, the tolerance of the approximation. Defaults to 1e-6 * norm(x, 1).

Returns:

A series or string, the continued fraction approximation of the rational expansion.

 

(n, d) = RAT(x, tol) returns the numerator and denominator as separate variables.

Example:

rat(1.5)

 

Returns the string 3/2.

Example:

rat(pi)

 

Returns the string 355/113.

Example:

rat(pi, 1e-2)

 

Returns the string 22/7.

Example:

(n, d) = rat(pi, 1e-2)

 

n == 22, d == 7.

Example:

a = rat({pi, 1.5})

 

a == {{355, 113},

      {  3,   2}} 

Remarks:

For series x, the first column of the result is the numerator value and the second column is the denominator.

 

RAT uses the continued fraction expansion to produce the rational approximation of a value. This is the same result as SETFORMAT(7) with the default tolerance.

 

The tolerance, tol, controls the result such that:

 

abs(n/d - x) <= abs(x) * tol

See Also:

SETFORMAT