MATLAB Answers


how to use large numbers

Sara AR 님이 질문을 제출함. 4 Nov 2019
최근 활동 Sara AR 님이 댓글을 추가함. 5 Nov 2019
i would like to use extremely large numbers in my matlab code for testing. so to say, a number like: 10^100. i tried the vpi but the numbers are still smaller than i need. this is for school work not just to test matlabs ability

  댓글 수: 2

What does use mean?
The maximum value of a double is:
>> realmax
ans =
And you can certainly enter 1e100. Of course at this magnitude, the precision is of the order of:
>> eps(1e100)
ans =
The governing principle of floating point is that at that magnitude you don't need more precision.
As far as I know vpi has no restriction to the number of digits:
>> digits = '0':'9';
>> n = vpi(digits(randi(10, 1, 1000))); %integer with 1000 digits
>> ceil(log10(n)) %how many digits
ans =
As you can see I just created a number of the order of 1e1000.
VPI (HPF too) has no explicit restriction, except for the limits of your computer and the speed of it. Huge numbers can become highly computationally expensive to work with, of course. For example, in order to validate my tools, I used them to compute a million digits of pi. It took a little while though. And I can use tools like VPI to search for huge primes.

로그인 to comment.


답변 수: 2

John D'Errico 님의 답변 4 Nov 2019
 채택된 답변

Depending on what you are doing, you can use any of several tools. For example ...
The symbolic toolbox:
ans =
My own VPI toolbox (free download from the file exchange):
ans =
Of course, VPI is only for large integer arithmetic.
For high precision floating point operations, you can use my HPF toolbox, also a free download from the file exchange. So here, the sine of 2.3 radians, out to 500 decimal digits.
DefaultNumberOfDigits 500
ans =
Again, the symbolic toolbox can also give you large number arithmetic for floats.
digits 500
ans =
HOWEVER, a large, even huge caveat exists. Much of the time, you never needed to use those large numbers in the first place! This often means you need to learn to use mathematics in a thoughtful way.
For example, at least some of the time when we think we need to use large integer arithmetic, we can avoid it completely. Thus, what is the value of mod(2^2000,17)?
ans =
Of course that fails, since 2^2000 overflows a double, creating an inf. The mod of that is indeterminate, so the mod is a NaN.
However, we can use powermod toools (I have one in my VPI toolbox, and there is one in the symbolic toolbox too.)
ans =
As you can see, it is correct.
ans =
Powermod tools are tremendously useful for large integer problems, doing things like primality testing and large integer factorizations.
For example, suppose I want to compute a Taylor series expansion for exp(x). That series looks like:
exp(x) = 1 + x + x^2/factorial(2) + x^3/factorial(3) + x^4/factorial(4) + x^5/factorial(5) + ...
A good trick for many series is to recognize that one term of the series can easily be computed from the previoius term. Think about it. How would you compute the term
if you already know the term
And of course, logs are often valuable too, if you recognize that factorial(n) = gamma(n+1), and you know that the gammaln function exists, and does not need to compute a factorial at all.
ans =
ans =
The point is, very often, use of mathematics in an intelligent, artful way is far better than a brute force use of arithmetic.

  댓글 수: 1

thank you so much!!

로그인 to comment.

Steven Lord 님의 답변 4 Nov 2019

Use Symbolic Math Toolbox. Be careful not to perform computations in double that result in values greater than flintmax and then convert the results to sym. Convert exact quantities to sym and perform the calculations symbolically. In some cases you'll get the results you expect, but in others you won't. This is especially important if you're working with numbers too big to store in double precision.
>> tenTo100 = sym(10)^100
tenTo100 =
>> twoTo2000 = sym(2)^2000
twoTo2000 =
114813069527425452423283 % ... snip a whole bunch of digits, ending in ... 9376
>> twoTo2000_doubleFirst = sym(2^2000)
twoTo2000_doubleFirst =

  댓글 수: 0

로그인 to comment.

Translated by