High precision rational fraction approximation

9 vues (au cours des 30 derniers jours)
Mehdi
Mehdi le 10 Sep 2014
Modifié(e) : John D'Errico le 15 Oct 2014
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
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