Precision in calculation of large digits
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want more than 32 digits precision in the calculation.
digits(200);
f97=vpa(factorial(97))
96192759682482062236598631563798937437476306361515295860273049067319419226943192827878886900710340579037421510433530649010990406523708314612471414390784.0
The correct answer is:
96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000.
I am using R2019a under windows 10 Version 1803
Please help, many thanks.
0 Kommentare
Antworten (2)
James Tursa
am 1 Okt. 2019
Bearbeitet: James Tursa
am 1 Okt. 2019
You need to convert to vpa first so that the factorial calculation is done with extended precision.
factorial(vpa(97))
0 Kommentare
John D'Errico
am 1 Okt. 2019
Bearbeitet: John D'Errico
am 1 Okt. 2019
What you need to understand is how MATLAB works. When you have one function call another, it evaluates the inside operation FIRST. That is, what is the value of
factorial(97)
ans =
9.61927596824821e+151
So we have a number with over a hundred decimal digits, stored as a DOUBLE precision number. A double does not store all those digits. Just the top 16 or so. (Actually, it stores binary bits, regardless...) Anyway, it stores the result of the factorial as a double temporarily.
THEN you passed that result into vpa. WRONG!!!!!!
Instead, remember how MATLAB works. Think about the difference between what you wrote, and this:
factorial(sym(97))
ans =
96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000
>> vpa(ans)
ans =
9.6192759682482119853328425949564e+151
Do you understand why this operation now works properly as you wish?
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!