rational {MASS}  R Documentation 
Find rational approximations to the components of a real numeric object using a standard continued fraction method.
rational(x, cycles = 10, max.denominator = 2000, ...)
x 
Any object of mode numeric. Missing values are now allowed. 
cycles 
The maximum number of steps to be used in the continued fraction approximation process. 
max.denominator 
An early termination criterion. If any partial denominator
exceeds max.denominator the continued fraction stops at that point.

... 
arguments passed to or from other methods. 
Each component is first expanded in a continued fraction of the form
x = floor(x) + 1/(p1 + 1/(p2 + ...)))
where p1
, p2
, ... are positive integers, terminating either
at cycles
terms or when a pj > max.denominator
. The
continued fraction is then rearranged to retrieve the numerator
and denominator as integers and the ratio returned as the value.
A numeric object with the same attributes as x
but with entries
rational approximations to the values. This effectively rounds
relative to the size of the object and replaces very small
entries by zero.
X < matrix(runif(25), 5, 5) solve(X, X/5) ## [,1] [,2] [,3] [,4] [,5] ## [1,] 2.0000e01 3.7199e17 1.2214e16 5.7887e17 8.7841e17 ## [2,] 1.1473e16 2.0000e01 7.0955e17 2.0300e17 1.0566e16 ## [3,] 2.7975e16 1.3653e17 2.0000e01 1.3397e16 1.5577e16 ## [4,] 2.9196e16 2.0412e17 1.5618e16 2.0000e01 2.1921e16 ## [5,] 3.6476e17 3.6430e17 3.6432e17 4.7690e17 2.0000e01 ## rational(solve(X, X/5)) ## [,1] [,2] [,3] [,4] [,5] ## [1,] 0.2 0.0 0.0 0.0 0.0 ## [2,] 0.0 0.2 0.0 0.0 0.0 ## [3,] 0.0 0.0 0.2 0.0 0.0 ## [4,] 0.0 0.0 0.0 0.2 0.0 ## [5,] 0.0 0.0 0.0 0.0 0.2