# Calculating enormous numbers in Matlab

21 views (last 30 days)
dsmalenb on 18 Jul 2016
Edited: John D'Errico on 18 Jul 2016
Greetings!
I am trying to calculate truly enormous numbers in Matlab 2016a. I have tried using the Symbolic Toolbox but it doesn't seem to get me where I need to go. I am talking about on the order of 2^2000. I keep getting 'Inf' and I am not sure how to proceed.
Here is an example what I am trying to calculate:
V = 2000;
sym(V);
vpa(2^V)
ans = Inf
Any help would be greatly appreciated!

John D'Errico on 18 Jul 2016
Edited: John D'Errico on 18 Jul 2016
What you need to appreciate is the order that things are done in MATLAB. When you type
sym(2^2000)
MATLAB looks at this, and FIRST, evaluates 2^2000. What is that? inf. Both 2 and 2000 are doubles. So 2^2000 is a double. Oh yeah. It is inf. So what? Still a double.
Then, and only then, does MATLAB decide what to do with this result. Oh, yes, pass it to sym. What is sym(inf)? I'll give you two guesses, and be disappointed if you need more than one. :)
The point is, you need to remember how MATLAB operates. SYM cannot look inside an expression, and decide that since I'll be making this result symbolic, then I should create the intermediate result as a sym before I ever compute that intermediate result.
How can you fix this? It is simple enough. Force MATLAB to do this in symbolic form, from the beginning.
sym(2)^2000
ans =
114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376
So, by telling MATLAB that one of the operands was symbolic, it knows to compute the result in symbolic form.