Precision in calculation of large digits

6 vues (au cours des 30 derniers jours)
Sin Keong Tong
Sin Keong Tong le 1 Oct 2019
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.

Réponses (2)

John D'Errico
John D'Errico le 1 Oct 2019
Modifié(e) : John D'Errico le 1 Oct 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?

James Tursa
James Tursa le 1 Oct 2019
Modifié(e) : James Tursa le 1 Oct 2019
You need to convert to vpa first so that the factorial calculation is done with extended precision.
factorial(vpa(97))

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by