### 17.7 Rational Approximations

s = rat (x)
s = rat (x, tol)
[n, d] = rat (…)

  Find a rational approximation of x to within the tolerance defined by tol.

  If unspecified, the default tolerance is `1e-6 * norm (x(:), 1)`.

  When called with one output argument, return a string containing a continued fraction expansion (multiple terms).

  When called with two output arguments, return numeric matrices for the numerator and denominator of a fractional representation of x such that `x = n ./ d`.

  For example:

      s = rat (pi)
      ⇒ s = 3 + 1/(7 + 1/16)

      [n, d] = rat (pi)
      ⇒ n =  355
      ⇒ d =  113

      n / d - pi
      ⇒ 0.00000026676

  Programming Note: With one output `rat` produces a string which is a continued fraction expansion. To produce a string which is a simple fraction (one numerator, one denominator) use `rats`.

  See also: rats, format.

rats (x, len)

  Convert x into a rational approximation represented as a string.

  The string can be converted back into a matrix as follows:

      r = rats (hilb (4));
      x = str2num (r)

  The optional second argument defines the maximum length of the string representing the elements of x. By default len is 9.

  If the length of the smallest possible rational approximation exceeds len, an asterisk (*) padded with spaces will be returned instead.

  See also: format, rat.