Trouble computing the factorial of a vector
3 次查看(过去 30 天)
显示 更早的评论
For a problem I need to get the factorial of numbers 1-50. I started by defining n = [1:50] and then typing factorial(n), but the answer seems really strange, with almost every element equaling to 0 except for the last 4.
Columns 1 through 18
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 19 through 36
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
etc.
0 个评论
回答(1 个)
John D'Errico
2021-1-21
编辑:John D'Errico
2021-1-23
No. Look CAREFULLY at that vector.
Are they really zero? Or are they just relatively zero?
F = factorial(1:50);
F([1 2 48 49])
ans =
1.0e+62 *
0.0000 0.0000 0.1241 6.0828
That was using the default display format. Do you see that 1.0e62* in front? What do you think that means? The ENTIRE vector as displayed is multiplied by 1e62, but SOME of those numbers are too small to display when the vector is displayed in this format. So they appear as 0.0000. Had you used a different display format, they would now show up.
format long g
F([1 2 49 50])
ans =
1 2 6.08281864034268e+62 3.04140932017134e+64
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!