4

I am dealing with very big numbers, on the scale of 10^500,10^800. When I try to calculate 10^309 it reurns inf.

>> 10^309
ans =
   Inf

I tested format long it also returns inf.

>> format long 
>> 10^309
ans =
   Inf

I also tested the vpa function:

vpa(10^309,309)
ans =
    Inf

how can I calculate and use big numbers in my calculation? I cannot use a logarithm, because I require all digits in the very large numbers. i.e. I want MATLAB to return the following for 10^308

vpa(10^308,308)
    ans =
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Can anyone help me to do this?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 4
    Did you consider that the number you want may in fact be too large for any sensible computer to represent? – rayryeng Oct 15 '15 at 20:21
  • 1
    can you change the unit of your values, for example, from nano meter to light year? – user3528438 Oct 15 '15 at 20:26
  • 1
    IKavanagh i want to use matlab as possible. –  Oct 15 '15 at 20:26
  • rayryeng,you are right ,whats the bigest number that i can use for compuer ,do you know?can i calculation this large number with creation a c++ function –  Oct 15 '15 at 20:29
  • Regarding your last comment, this question has already been asked and has a very interesting answer and article linked to it [here](http://stackoverflow.com/questions/3708461/biggest-number-in-computer-ever) – brodoll Oct 15 '15 at 20:30
  • user3528438,my data is not physics,its related number theory,discreet math,it is digit number chain .this chain has been used in my operation –  Oct 15 '15 at 20:31
  • brodroll ,i want to a solution in matlab.is it impossible!thanks –  Oct 15 '15 at 20:33
  • if that's the case, you want to avoid using floating point numbers, then the best matlab can offer is `uint64`, which has a range between [0, 18446744073709551615]. Symbolic is another option, or some kind of big integer library. – user3528438 Oct 15 '15 at 20:36
  • @stackprogramer I was referring to the question "whats the bigest number that i can use for compuer" you asked to rayryeng – brodoll Oct 15 '15 at 20:36
  • user3528438,thanks for reply and attention,10^309 is out of range 18446744073709551615, brodroll yes,thanks for your reply and eattention but i think i can write this calculator with a c++ vectors –  Oct 15 '15 at 20:39
  • The largest real valued floating point number that MATLAB can represent on your computer can be found by `realmax`. On my computer, it's currently: `1.7977e+308`. This is probably why you are getting `Inf` when trying values that have an exponent beyond 308. – rayryeng Oct 15 '15 at 20:43
  • rayryeng dear you are right,my realmax is 1.797693134862316e+308 ,, i have a idea tha i created a special code with vectors in c++, my mean in a first second i need a number and it's digit and in second 2,i delete the former number and i test new number and i don't need big memory and so on... –  Oct 15 '15 at 20:50
  • If you install the external (and freeware) `gmp` library there are tools on FileExchange that will allow you to do unlimited precision work with floats. But there really is no excuse for working with low precision numbers of the magnitudes you've posted. Please read up on the difference between precision and magnitude, and figure out how to scale your numbers into something reasonable. – Carl Witthoft Oct 16 '15 at 15:19
  • @ Carl Witthoft thanks for attention,i soon test gmp,.... answer "Please read up on the difference between precision and magnitude, and figure out how to scale your numbers into something reasonable." my data is chain of number that uses in number theory,discreet math , there is not physics data. –  Oct 17 '15 at 11:46

2 Answers2

4

You can still use the Variable Precision Arithmetic module vpa. Just make sure you put the exponent outside of the vpa call:

>> A = vpa(10)^400

A =

1.0e400
rayryeng
  • 102,964
  • 22
  • 184
  • 193
3

You could take a look at the Variable Precision Integer Arithmetic library that's on the MATLAB file exchange. An example of its use is as follows:

>>> A = vpi(17)^17 
ans = 
827240261886336764177

>>> 17 + A^17 
ans = 
39786732894291535047752038041559739510060813980024082 
30012867731573722066105737100731556603857745946047229 
53759676529121155309750944582301597489457676380805029 
59227566911971103003303064782118652210655457390045806 
99039190393572334521701109889855832341416056005878848 
49943142324389193616484809157960034059531548585473213 
36465170635561696613297503569949729314 

This seems to be what you want.

GJStein
  • 648
  • 5
  • 13
  • i run this has error! >> A = vpi(17)^17 ans = 827240261886336764177 Undefined function or variable 'vpi'. >> factorial(vpi(171)) Undefined function or variable 'vpi'. –  Oct 15 '15 at 21:08
  • 1
    Is the folder where you downloaded the submission in your matlab's path? An easy fix would be to unzip the downloaded file directly into the folder you have the script you are running – brodoll Oct 15 '15 at 21:12
  • @brodroll it works, i copied it to path and worked thanks very much A = vpi(17)^17 A = 827240261886336764177 –  Oct 15 '15 at 21:25
  • 1
    it works nice.A = vpi(10)^309 A = 10000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000 –  Oct 15 '15 at 21:38
  • but for this it has errors: vpi(4222)*45555555555555555555555555555555 Error using vpi (line 160) If N is a double, it may be no larger than 2^53 - 1 Error in .* (line 79) INT1 = vpi(INT1); Error in * (line 27) INT = INT1.*INT2; –  Oct 27 '15 at 17:28
  • vpi(10988888877777)*10^20 Error using vpi (line 160) If N is a double, it may be no larger than 2^53 - 1 Error in .* (line 79) INT1 = vpi(INT1); Error in * (line 27) INT = INT1.*INT2; –  Oct 27 '15 at 17:31
  • this tool can't calculate for number large than 2^53 - 1 so i can n't finish my project –  Oct 27 '15 at 17:33
  • Try casting: `vpi(4222)*int32(45555555555555555555555555555555)` – GJStein Oct 27 '15 at 21:11