Number greater than the largest positive floating-point number (1.7977e+308)
이전 댓글 표시
I'm trying to calculate the following expression:
(l+m)! / (l!m!)
but when l=m > 85 I get infinity, because the result is greater than the largest positive floating-point number (1.7977e+308).
Is there a way of increasing this number limit, or even a trick to avoid this problem?
채택된 답변
추가 답변 (3개)
Roger Stafford
2014년 9월 2일
In case it is of interest, here is an alternate method of computation based on Pascal triangle summations.
m = 25;
L = 11;
x = ones(m+1,1);
for k = 2:L+1
x = cumsum(x);
end
y = x(m+1); % <-- This is the answer
This involves more operations than the other method presented earlier here, but, as opposed to this other method, there are no round-off errors until reaching numbers beyond 2^53 where not all integers can be represented. This is because only sums of integers are being computed in this method.
John D'Errico
2014년 9월 2일
0 개 추천
I'd suggest working in logs, which will be as insensitive as possible to problems with exponents. Only at the very end do you need to exponentiate.
If you truly need to compute a number too large to represent in a double, then you will have no choice in the end. Use either the symbolic toolbox, or a tool like my own HPF toolbox.
댓글 수: 2
Eric
2014년 9월 2일
Roger Stafford
2014년 9월 2일
product_{j=1}^l (m+j)/j
I gave you the matlab version of that in the line:
round(prod((m+1:m+L)./(1:L)))
The added 'round' merely corrects for rounding errors occurring in the division process.
Steven Lord
2016년 5월 18일
0 개 추천
(L+M)! / (L! * M!) is just nchoosek(L+M, L) or nchoosek(L+M, M). For most cases this uses the approach that others in this discussion have recommended.
But if L+M is on the order of 5000, the answer will not be exact unless one of L or M is much, much smaller than 5000 and will be infinite (too large to represent as a finite double) if the second input is greater than about 150 or 160.
카테고리
도움말 센터 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!