MATLAB: Does the RAT function not return the smallest possible numerator and denominator for a given floating point number

MATLAB

I want to use the RAT function to find the fractional representation of a floating point number. However, the results that I receive are not the smallest possible set of numbers.
For example when I use RAT on the following number
[a b] = rat(3.1415926535, 8000e-6)
It gives me the numerator and denominator coefficients as
a =
22
b =
7
Now compute the relative error between the floating point number used above and the ratio 19/6
c = (19/6-3.1415926535)/3.1415926535 .
This results in c = 0.007981306277481 which is lesser than the tolerance value of 0.008.

Best Answer

The RAT function will give values for the numerator and denominator that are within the specified tolerance and does not guarantee that the numbers returned are the smallest possible set of numerator and denominator values.
When you supply a tolerance, you are telling the RAT function when the approximation is good enough to quit. There is no guarantee that the approximation returned will have the smallest numerator and denominator possible when the sequence is terminated. The algorithm does not consider all possibilities, rather a subset of possibilities that are constructed in a certain way.