Why doesn't Matlab's answer to factorial(100) equal Wolfram Alpha's 100!?

조회 수: 4 (최근 30일)
When computing:
>> a = sprintf('%f',factorial(100))
a =
'93326215443944102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000'
However the number above does not equal 100!
why is this and how do I fix it?
  댓글 수: 2
Stephen23
Stephen23 2017년 10월 24일
편집: Stephen23 2018년 2월 26일
"why is this..."
Because you are doing a numeric calculation using a variable of class double, which is limited to approximately 16 decimal digits precision.
"...and how do I fix it?"
Do a symbolic calculation.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 10월 24일
factorial(sym(100))
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 10월 24일
sym(factorial(100)) calculates factorial(100) in double precision and passes the result to sym(), which then converts that rather wrong double precision number to an extended precision number.
Remember, in MATLAB, arguments are always evaluated before being passed to a function.
The only exception to this that I can think of is that the various int*() and uint*() and single() and double() calls with a literal number (and spacing is important!) is treated as syntax rather than a function call: those restricted cases of using those functions create the values at parse time rather than at execution time. This was needed to be able to write a accurately write integers between 2^53 and 2^64-1.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2017년 10월 24일
Two reasons:
1) You are using MS Windows, and the native number formatting routines cannot handle more than 16 digits. On OS-X / MacOS, which does the conversion properly,
>> a = sprintf('%f',factorial(100))
a =
'93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248.000000'
If I recall correctly, Linux does better than MS Windows but not as good as Mac.
2)
>> eps(factorial(100))
ans =
1.21943302746718e+142
By that large of a number, the distance between representable numbers is large enough integers as to badly distort the value.
Work around:
>> factorial(sym(100))
ans =
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

WEEKIAN SOH
WEEKIAN SOH 2018년 2월 26일
Indeed this is a good question. I had also noticed the same problem while I compare Mathematica's factorial(100) with Matlab's.
Good thing we don't have to work with so large of a numbers most of the time...
Now, if I want high precisions to see the digits, I'll use sym(Number), and pass this sym(Number) into the function to derive the long precisions.
Thank you for sharing the answer

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by