MATLAB precision for variables

  • Thread starter sara_87
  • Start date
  • #1
763
0

Homework Statement



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

Homework Equations





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:

b=0;
for j=1:20
b=b+pi^2*j;
end

this would use the default double precision.

I know that
vpa(b)

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.
 

Answers and Replies

  • #2
319
0

Homework Statement



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

Homework Equations





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:

b=0;
for j=1:20
b=b+pi^2*j;
end

this would use the default double precision.

I know that
vpa(b)

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.

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.
 
  • #3
763
0
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
 
  • #4
280
0
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.

http://stackoverflow.com/questions/2633400/c-c-efficient-bit-array

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.
 
  • #5
319
0
I have to also note that using 2 doubles makes no sense at all.

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++).
 
  • #6
280
0
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++).
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.
 
  • #7
319
0
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.

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.
 
  • #8
763
0
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?
 
  • #9
319
0
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.
 
  • #10
53
0
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:

Related Threads on MATLAB precision for variables

  • Last Post
Replies
3
Views
5K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
7
Views
15K
  • Last Post
Replies
2
Views
11K
Replies
4
Views
8K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
2K
Top