High precision rational fraction approximation
Mostrar comentarios más antiguos
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 comentarios
"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
Starting from high-precision values and then converting to double is obviously not a viable option, so you likely have three options:
- Implement your own function with an inbuilt toolbox (sym, mp, vpa).
- Implement your own function with some custom floating point class.
- 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!
John D'Errico
el 10 de Sept. de 2014
I should probably write rat for HPF. Another round tuit.
Mehdi
el 10 de Sept. de 2014
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Conversion Between Symbolic and Numeric en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!