How can I perform computations in any user-defined precision and not just double precision in MATLAB?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 8 Dec 2010
Edited: MathWorks Support Team
on 23 Nov 2021
Can I use MATLAB to perform computations in any user-defined precision and not just double precision?
I want to compute using different precision, like 50 digits or quadruple precision. Can I do this in MATLAB?
Accepted Answer
MathWorks Support Team
on 8 Dec 2010
MATLAB can perform computations with the desired level of precision if you use variable precision arithmetic. This requires the use of Symbolic Math Toolbox. If you have the Symbolic Math Toolbox, then you may use the MuPAD Kernel to perform computations to desired precision.
For example:
To display the value of 'pi', the default double precision display in MATLAB is:
pi
ans =
3.14159265358979
You can use the VPA (Variable Precision Arithmetic) function from the Symbolic Math Toolbox to display the value of 'pi' to as many significant digits as you want. This is shown below for 25 digits:
vpa('pi', 25)
ans =
3.141592653589793238462643
For additional information and examples using VPA please execute:
>> doc vpa
at the MATLAB command prompt.
In the absence of the symbolic math toolbox you can use the following data types to obtain different degrees of precision:
1. single
2. double
3. unit8
4. uint16
5. uint32
1 Comment
Kevin Gleason
on 27 Sep 2016
Edited: MathWorks Support Team
on 23 Nov 2021
Almost. Adding "digit(50)" will set the precision for computations performed by "vpa". Now, all computations relying on higher precision must be computed symbolically.
>> x = sym(pi)
>> y = sym(3.14)
>> vpa(x - y)
% ans = 0.0015926535897932384626433832795028841971693993751058
I recommend you look at the vpa docs for examples of symbolic computations:
More Answers (0)
See Also
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!