how to use large numbers

조회 수: 206 (최근 30일)
Sara AR
Sara AR 2019년 11월 4일
댓글: Andrew Sol 2023년 4월 18일
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
Guillaume
Guillaume 2019년 11월 4일
What does use mean?
The maximum value of a double is:
>> realmax
ans =
1.79769313486232e+308
And you can certainly enter 1e100. Of course at this magnitude, the precision is of the order of:
>> eps(1e100)
ans =
1.94266889222573e+84
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 =
1000
As you can see I just created a number of the order of 1e1000.
John D'Errico
John D'Errico 2019년 11월 4일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2019년 11월 4일
Depending on what you are doing, you can use any of several tools. For example ...
The symbolic toolbox:
sym(17)^173
ans =
737332536277664007345135166862919042342198303176957733423911897658606050420746608682751241168475893711891774173437592993636785506777793476094874734220957516554685643214228598246375959317220176420842802110495459537
My own VPI toolbox (free download from the file exchange):
vpi(17)^173
ans =
73733253627766400734513516686291904234219830317695773342391189765860
605042074660868275124116847589371189177417343759299363678550677779347609
487473422095751655468564321422859824637595931722017642084280211049545953
7
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
sin(hpf('2.3'))
ans =
0.74570521217672017738540621164349953894264877802047425750762828050000099313904725787119141718409288762817250225753133592135334980555453298342113583580957089845003916537428359514540258159501067012614643093890466791278548509412263198294482089203977890584979559621160856653150662947581690813330676541668856555926001056589318039296179001073304389700504217567754815087240130585632142045686943253384330660013244550634516301559995926758472414911946682819947915408675727440373168283432224358384420096808300220
Again, the symbolic toolbox can also give you large number arithmetic for floats.
digits 500
sin(sym('2.3'))
ans =
0.7457052121767201773854062116434995389426487780204742575076282805000009931390472578711914171840928876281725022575313359213533498055545329834211358358095708984500391653742835951454025815950106701261464309389046679127854850941226319829448208920397789058497955962116085665315066294758169081333067654166885655592600105658931803929617900107330438970050421756775481508724013058563214204568694325338433066001324455063451630155999592675847241491194668281994791540867572744037316828343222435838442009680830022
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)?
mod(2^2000,17)
ans =
NaN
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.)
powermod(2,2000,17)
ans =
1
As you can see, it is correct.
mod(sym(2)^2000,17)
ans =
1
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
x^(n+1)/factorial(n+1)
if you already know the term
x^n/factorial(n)
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.
log(factorial(100))
ans =
363.74
gammaln(101)
ans =
363.74
The point is, very often, use of mathematics in an intelligent, artful way is far better than a brute force use of arithmetic.
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 4월 18일
sympref('FloatingPointOutput',false);
Andrew Sol
Andrew Sol 2023년 4월 18일
@Walter Roberson yes, it works now, thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 11월 4일
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 =
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>> twoTo2000 = sym(2)^2000
twoTo2000 =
114813069527425452423283 % ... snip a whole bunch of digits, ending in ... 9376
>> twoTo2000_doubleFirst = sym(2^2000)
twoTo2000_doubleFirst =
Inf

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by