Dealing with very high number like 2^1024 bit

10 views (last 30 days)
I work with very high number like 2^1024 and bigger when I try to compute this function MATLAB GIVE Inf. How can I over come this problem.

Accepted Answer

John D'Errico
John D'Errico on 1 Sep 2016
Edited: John D'Errico on 1 Sep 2016
2^1024 is a number that exceeds the dynamic range of a double precision number. You cannot create it using a double.
There are many ways you can deal with it however.
1. You might choose to simply leave it in binary form, storing only the bits of your result. This would simply be a Boolean vector.
2. You can use a different class to store the number. For example:
sym(2)^1024
ans =
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
Or using my VPI class...
vpi(2)^1024
ans =
17976931348623159077293051907890247336179769789423065727343008115773
267580550096313270847732240753602112011387987139335765878976881441662249
284743063947412437776789342486548527630221960124609411945308295208500576
883815068234246288147391311054082723716335051068458629823994724593847971
6304835356329624224137216
Or my HPF class...
hpf(2,30)^1024
ans =
1.79769313486231590772930519079e308
SYM comes from the symbolic toolbox. VPI and HPF are both on the file exchange for download.
Finally, you can recognize that it is rarely really necessary to compute a number that large. Most of the time, it is an intermediate in some other computation. So you might be able to re-arrange your computation using logs. In other cases, what really matters is not that large number, but the modulus of it wrt some integer. Again, this is easily computed without ever creating that huge number.
The skill of computation is in knowing how to get around problems when brute force fails.
  1 Comment
Mohammed Amain
Mohammed Amain on 2 Sep 2016
Many thanks for your help. The ways you suggest all work and your support was great.

Sign in to comment.

More Answers (2)

michio
michio on 1 Sep 2016
One way to overcome the issue is to use the Symbolic Math Toolbox in MATLAB to evaluate expressions with more floating-point precision than double precision.

Steven Lord
Steven Lord on 1 Sep 2016
Use Symbolic Math Toolbox.
two = sym(2);
two1024 = two^1024
Note that the following will NOT work, as 2^1024 will be computed in double precision (and overflow to Inf) then that Inf will be converted into a symbolic value.
two1024 = sym(2^1024)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!