Change length of mantissa
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
John D'Errico
am 25 Mär. 2011
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 Kommentare
Jan
am 25 Mär. 2011
@Community: Let's vote the question and this answer such that John's valuable explanation is found easily in the forum!
James Tursa
am 25 Mär. 2011
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
Kategorien
Mehr zu Get Started with Signal Processing Toolbox finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!