Change length of mantissa

How to change the accuracy of calculations? For example: >> x = log(2) x =0.6931 >> vpa(x) ans = 0.69314718055994528622676398299518041312694549560546875 I see only 53 symbols after dot. But I need to more. Help, please.

 Accepted Answer

Jan
Jan on 25 Mar 2011
Under "help vpa" I find:
R = vpa(A, d) uses at least d significant (nonzero)
digits, instead of the current setting of digits.
So it seems like this could help (I do not have the VPA toolbox, but the help text looks very clear):
vpa('log(2)', 100)

More Answers (1)

You cannot get more accuracy than that for a double precision input. vpa will not help you unless you use it properly, nor do you really have 53 decimal digits there as you have done it. The last 35 digits (or so) are garbage. For example, what is the full decimal expansion of a number represented in the IEEE format that MATLAB uses for a double? Suppose you write
X = 1.2;
in MATLAB? In fact, MATLAB defines the number in terms of a binary representation. Internally the number has a 52 bit binary mantissa. In fact, if we take the binary representation of that number, then turn it into decimal, we will get...
1.1999999999999999555910790149937383830547332763671875
See that this is not at all the value of 1.2 we originally asked for. Only the first 16 digits were correct, and that only after round-off. The remainder were garbage. Asking for vpa to give you more than 16 digits starting with a double is asking for a list of virtual random numbers. As it turns out though, vpa does give you 53 digits or so. What are they? Start with the number 2. In fact, the number 2 is an integer, representable exactly in MATLAB.
However, when you compute x using the log function, matlab does this...
>> x = log(2)
x =
0.693147180559945
which is as close as matlab can get to log(2) in the internal representation it uses. To 100 digits of precision, the correct value for log(2) is (using my own set of tools for high precision arithmetic)
>> log(hpf(2,100))
ans =
0.693147180559945309417232121458176568075500134360255254120680009493
3936219696947156058633269964186875
In fact though, MATLAB stores the value
>> hpf(log(2),100)
ans =
0.69314718055994528622676398299518041312694549560546875
See that, just as with 1.2, the number stored in the binary form is different after about the 16th decimal digit or so. Only 52 binary bits are stored in the mantissa. You end up with 53 non-zero decimals because that is the exact decimal equivalent of that binary form. If you really need a high precision result, you need to force MATLAB to start with a number it can know to high precision. Thus, Jan's recommendation for use of vpa.
vpa('log(2)',100)

2 Comments

Jan
Jan on 25 Mar 2011
@Community: Let's vote the question and this answer such that John's valuable explanation is found easily in the forum!
FYI, to get the exact decimal conversion for a MATLAB double you can use the num2strexact utility:
http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

Sign in to comment.

Categories

Products

Tags

Asked:

on 25 Mar 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!