Precision in calculation of large digits

조회 수: 3 (최근 30일)
Sin Keong Tong
Sin Keong Tong 2019년 10월 1일
편집: John D'Errico 2019년 10월 1일
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.

답변 (2개)

James Tursa
James Tursa 2019년 10월 1일
편집: James Tursa 2019년 10월 1일
You need to convert to vpa first so that the factorial calculation is done with extended precision.
factorial(vpa(97))

John D'Errico
John D'Errico 2019년 10월 1일
편집: John D'Errico 2019년 10월 1일
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?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by