Number greater than the largest positive floating-point number (1.7977e+308)
Show older comments
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?
Accepted Answer
More Answers (3)
Roger Stafford
on 2 Sep 2014
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
on 2 Sep 2014
0 votes
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 Comments
Eric
on 2 Sep 2014
Roger Stafford
on 2 Sep 2014
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
on 18 May 2016
0 votes
(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.
Categories
Find more on Numeric Types 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!