1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB precision for variables

  1. May 2, 2012 #1
    1. The problem statement, all variables and given/known data

    The precision in MATLAB is approximately 2*10^(-16),
    is there a way that my computations can be calculated using a smaller precision?

    2. Relevant equations

    3. The attempt at a solution

    if i want to calculate the sum of pi^2*j where j=1:20
    but i want it to be computed using high precision.if i type:

    for j=1:20

    this would use the default double precision.

    I know that

    will give me (as a symbol) the answer to 32 decimal places. But, I don't want to display it in 32 deciaml places, i wan the inner computations to be calculated with a better precision (eg 32 decimal places).

    Any ideas will be very much appreciated.
  2. jcsd
  3. May 3, 2012 #2
    Not directly, no. The limit isn't an artificial constraint but comes from constraints of the machine itself, which can only faithfully represent so many digits. If you store the first half of your variable in a double precision variable and the second half in another double precision variable, you can do it that way, but that's kind of awkward.

    Also, note that regardless of digits of precision, the machine memory cannot directly represent pi in binary, so that's an external constraint on the precision of your result.
  4. May 6, 2012 #3
    oh right i see.
    With what precision is pi calculated? double precision?
    also, with other functions such a sine, cosine, log, are they also calculated with double precision?

    thank you
  5. May 6, 2012 #4
    Something like this is better suited in a lower-level language. You can create your own floating point class with arbitrary precision (using an ever-growing amount of memory and time per computation) and then code your own multiplication/division algorithms that use your new data type.

    The implementation of double-precision is well-documented. Just look at that documentation and extend it to use perhaps 128 bits or 512 bits.


    There is a starting point on how you can represent your bit array.

    You would also probably need to load somehow pi into your new data class using this greater precision or else the attempt would be for naught. You could look into methods for computing pi and do so using your new data type.

    I have to also note that using 2 doubles makes no sense at all.

    As for the precision of pi and computation in MATLAB, the default data type is double. pi() is double precision. cos() and all other functions are too if you feed it a double.
  6. May 6, 2012 #5
    The way that a double precision number is stored in memory, for 32-bit arithmetic, is to store the number in two neighboring memory locations (or in 64 bit arithmetic, you can represent all 16 decimal places as one number). So, in effect, merging two singles to make a double logically predicts merging two doubles to make a quadruple precision number (32 digits of precision, perhaps), but when you can only have 64 bits per number you have no choice but to split it into two numbers. That is naturally how any quadruple precision (or double-double) memory type would be implemented on a system that does not have 128-bit arithmetic.

    The problem here is that MATLAB does not have built-in support for that level of precision at the current time, and creating user-defined data types is difficult in MATLAB (unlike in C++).
  7. May 6, 2012 #6
    It doesn't work that way. You have to manage the bits themselves. You can't just use two doubles and say "I got more precision". With respect to MATLAB, it makes zero sense whatsoever.
  8. May 6, 2012 #7
    Yes, I did state that it is quite difficult to do in MATLAB. But it is possible to implement such an extended precision format in MATLAB. Indeed, some toolboxes have been written to this effect, although I cannot vouch for their usefulness, having never needed to use them myself. But if the original poster is not interested in learning a new language, those toolboxes might be worth checking out (or perhaps writing your own).

    By the way, the symbolic math toolbox supports arbitrary precision arithmetic, but obviously it can be quite restrictive to do it this way.
  9. May 7, 2012 #8
    thank you very much for these ideas.

    well, I don't want to use another language, I would like to stick with MATLAB.
    the symbolic toolbox can use higher precision using vpa for example:
    vpa(pi,40) will use 40 significant figures, however, the problem is: the outcome will be a symbol and i want to use this outcome as a numerical number not a symbol.

    is there a way i can use the outcome as a number?
  10. May 7, 2012 #9
    Well, what you can do is basically convert your whole program into a symbolic math calculation (which is why I said it's restrictive). This is because if you just use it for things like constants such as pi, and then you multiply it by some double precision variable stored in memory, you lose all the precision you wanted to keep, since whatever is multiplying pi is not itself accurate to 40 digits. So to maintain that precision, you need to have every term in your calculation be accurate to 40 digits. Then at the end you can just output your results to whatever precision you want.
  11. May 7, 2012 #10
    Hi Sara, I came across this article on computing pi recently. It might be of use in general for implementing variable precision commands in matlab.

    http://www.mathworks.co.uk/company/newsletters/articles/clevescorner-computing-pi.html [Broken]
    Last edited by a moderator: May 6, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook