long integers in matlab

72 ビュー (過去 30 日間)
dleal
dleal 2022 年 7 月 14 日
編集済み: John D'Errico 2022 年 7 月 14 日
Hi all,
how can we do (simple) operations in Matlab with integers that exceed intmax("uint64")?
I am aware that in Python this is a posibility and there's virtually no limit for int size. In Python 3:
>> a = 10**100;
>> b = 10**100 + 1;
>> b-a
1
Is this possible in MATLAB as well? I am just curious so I can understand Matlab a bit better. I have no use case in mind

採用された回答

Stephen23
Stephen23 2022 年 7 月 14 日
編集済み: Stephen23 2022 年 7 月 14 日
"what would be the recommended approach if I don't have access to Symbolic Math TB?"
You can download VPI by clicking on the big blue "DOWNLOAD" button in the top right corner:
unzipping it onto your MATLAB Search Path (e.g into the current directory), and then following its instructions.

その他の回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2022 年 7 月 14 日
You can use Java.
import java.math.*
y=BigInteger('10').pow(100)
y = 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
z=BigInteger('10').pow(100).add(BigInteger('1'))
z = 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
sub=z.subtract(y)
sub = 1
  1 件のコメント
John D'Errico
John D'Errico 2022 年 7 月 14 日
編集済み: John D'Errico 2022 年 7 月 14 日
I always hated the java.math.BigInteger interface. Way too much of a kludge to just do simple arithmetic operations. So I wrote a version of VPI some time ago (called VPIJ, of course) that uses the Java tools.
The virtue of the Java tools is they are a bit faster than syms. Ok, a heck of a LOT faster. And VPIJ also screams compared to VPI, since I had to write all of VPI in MATLAB itself.
P = vpij(2)^521 - 1
P =
6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151
Q = sym(2)^521 - 1;
isprime(P)
ans =
logical
1
isprime(Q)
ans =
logical
1
timeit(@() isprime(P))
ans =
0.0028533
tic,isprime(Q),toc
ans =
logical
1
Elapsed time is 5.427998 seconds.
Note that I did not bother using timeit to check the time for the sym test, since timeit uses MULTIPLE tests. Same number, but often orders of magnitude difference in speed.
Sadly, Java seems to be slowly going away in MATLAB.

サインインしてコメントする。


Steven Lord
Steven Lord 2022 年 7 月 14 日
See Symbolic Math Toolbox.
ten = sym(10);
a = ten^100;
b = ten^100+1;
b-a
ans = 
1
Note that performing the calculation then converting to a symbolic result won't work if the numbers get large enough. sym is perfectly happy to convert Inf to a symbolic value.
c1 = sym(10^500) % Already overflowed before sym was called
c1 = 
c2 = ten^500
c2 = 
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  1 件のコメント
dleal
dleal 2022 年 7 月 14 日
Thank you Steven! what would be the recommended approach if I don't have access to Symbolic Math TB?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by