High precision rational fraction approximation

I need to convert some high precision numbers to rational numbers. There is a function available in MATLAB called rat which does this for double precision numbers. For example if there are a couple of numbers different beyond the double precision decimal digits, then rat gives the same result. I tried to fix this by reducing the tolerance but it did not help. Is there any solutions available for this?
If I could have access to the source code of rat it would be possible to update it for high precision numbers as well.

6 commentaires

Stephen23
Stephen23 le 10 Sep 2014
Modifié(e) : Stephen23 le 10 Sep 2014
"Is there any solutions available for this?" Yes: learn about floating point numbers in MATLAB.
How did you manage to create numeric variables in MATLAB that were "a couple of numbers different beyond the double precision decimal digits"? Did you actually look at the "numbers" after you created them? Native floating point data types in MATLAB are single and double , so where do you imagine that these "couple of numbers" (digits) would be stored? Did you check to see if these extra digits were actually stored in the "number"?
What exactly does "fix this by reducing the tolerance" mean? "Fix" what, exactly? The reasons it gives the same result is because you are giving it the same input values.
It is likely (although I did not look) that rat 's algorithm would work for "high precision numbers", as you call them.... but where exactly are you going to get these "high precision numbers" from?
In any case, if you need higher precision, take a look at sym or vpa , or check out John D'Errico's HPF class .
You might also search MATLAB Answers for other topics relating to higher precisions than double, such as: http://www.mathworks.com/matlabcentral/answers/10033-matlab-s-accuracy-of-digits-number
And to answer your implied question at the end of your post: if you want the source code of a MATLAB function, call open in the command window, eg:
open rat
Mehdi
Mehdi le 10 Sep 2014
Modifié(e) : Mehdi le 11 Sep 2014
Thanks Stephen for the info. I create the high precision numbers using vpa or mp in the Multiprecision Computing Toolbox which has a lot of nice features and functionalities, yet unfortunately does not have rat in its supported functions. As far as I see rat does not accept inputs from sym or mp class.
Stephen23
Stephen23 le 10 Sep 2014
Modifié(e) : Stephen23 le 10 Sep 2014
Starting from high-precision values and then converting to double is obviously not a viable option, so you likely have three options:
  1. Implement your own function with an inbuilt toolbox (sym, mp, vpa).
  2. Implement your own function with some custom floating point class.
  3. Implement your own function from scratch, based on digit vectors or something equally ungainly.
Sooo... you might be stuck writing your own function to do this then, either from rat or find another algorithm for deriving rational fractions from decimal fractions. This might be a good start: http://en.wikipedia.org/wiki/Continued_fraction
Good luck!
I should probably write rat for HPF. Another round tuit.
Mehdi
Mehdi le 10 Sep 2014
Thanks Stephen!
John, it would be great if you could write a code for this, and I would appreciate it if you could update me here in case you write the code.
Mehdi
Mehdi le 10 Sep 2014
Modifié(e) : Mehdi le 10 Sep 2014
By the way, having this tool enables us to get high precision results from many functions of the symbolic math toolbox; because we will be able to convert the high precision number to a fraction and use it as an input for functions in the symbolic math toolbox to get the output as another fraction which can be simply converted to a high precision number then!

Connectez-vous pour commenter.

 Réponse acceptée

John D'Errico
John D'Errico le 15 Oct 2014
Modifié(e) : John D'Errico le 15 Oct 2014
I decided to get this working. Easy enough. And since I had to post a bug fix for something in HPF anyway, I pushed it to the top of my list.
So in the most recently uploaded version of HPF, there is now a rat function, using a continued fraction approximation to any HPF floating point number. It uses a few divides, so while I'd like it to be faster, it works nicely for numbers on the order of a few hundred digits in precision.
[N,D] = rat(hpf('pi',100),hpf('1e-101'))
N =
394372834342725903069943709807632345074473102456264
D =
125532772013612015195543173729505082616186012726141
N/D
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
hpf('pi',100)
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by