Why doesn't Matlab's answer to factorial(100) equal Wolfram Alpha's 100!?
Show older comments
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?
Accepted Answer
More Answers (2)
Walter Roberson
on 24 Oct 2017
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
on 26 Feb 2018
0 votes
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
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!