Precision in calculation of large digits

18 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by