How to read a very large number, say of 700 digits?

13 ビュー (過去 30 日間)
Abdul Gaffar
Abdul Gaffar 2021 年 10 月 1 日
編集済み: Stephen23 2021 年 10 月 2 日
If I have a number, say N of 700 digits, then how can I read it?
I have tried using ELLIPSIS (...) as:
N = 1234567890123 ...
4567899089339 ...
1234567891233;
But, this does Not work.
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 10 月 1 日
What datatype are you hoping the result will be ?
Abdul Gaffar
Abdul Gaffar 2021 年 10 月 1 日
a number N, like N = 1234... (up to 700 digits). i.e., N is a 1x1 double format.

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

採用された回答

John D'Errico
John D'Errico 2021 年 10 月 2 日
編集済み: John D'Errico 2021 年 10 月 2 日
You don't understand. A 700 digit number is not representable in double precision. Even if it were, then roughly 684 of those digits will never be stored in the number when you do put it into a variable. Anything past the 16 most significant digits will be complete garbage. For example:
X = 1234567890987654321012345;
X has 25 digits in it.
format long g
X
X =
1.23456789098765e+24
But have we stored al of those digits, correctly?
sprintf('%0.30f',X)
ans = '1234567890987654441861120.000000000000000000000000000000'
MATLAB sees it as an integer, but do you see that the 9 least significant digits are complete garbage?
Next, can you use a line continuation for the digits of a number? No. So you CANNOT do this to represent a 20 digit number
Y = 1234567890...
9876543210;
MATLAB will generate an error if you try.
The only way you could store a number like that is if you use symbolic form. Thus...
S = '123456789012345678901234567890123456789012345678901234567890'
S = '123456789012345678901234567890123456789012345678901234567890'
XS = str2sym(S)
XS = 
123456789012345678901234567890123456789012345678901234567890
And you could build up a string vector using multiple continued lines. But don't bother trying to put that number into a double. Again, complete crapola.
But remember that all computations with such a number will be incredibly slow, when compared to using double precision. You could also use my VPI or HPF toolboxes to represent long numbers like this, but again, quite slow by comparison.
  1 件のコメント
Stephen23
Stephen23 2021 年 10 月 2 日
編集済み: Stephen23 2021 年 10 月 2 日
Emphasizing: 700 digits is well above the largest floating point number anyway.
realmax()
ans = 1.7977e+308

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

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 10 月 1 日

Steven Lord
Steven Lord 2021 年 10 月 1 日
Your 700 digit number is too large to fit in double precision. The largest finite double precision number doesn't have that many digits.
log10(realmax)
ans = 308.2547
What were you hoping to do with this huge number? Are you hoping to deploy whatever solution you come up with using MATLAB Compiler or MATLAB Coder?
  2 件のコメント
Abdul Gaffar
Abdul Gaffar 2021 年 10 月 2 日
編集済み: Abdul Gaffar 2021 年 10 月 2 日
Maybe I don't undertand you. I am rewriting:
Let N = 123456...23, (a 700 digits number) of double type.
One way to read N is as: Write N in a single row, and I am done.
But, if I have to write N in several rows, as I write the following (using ellipsis):
y = 3 + ...
2 + ...
3
then how to do for N?
Further, I will separate every 2 digits of N to make N as 1 x 350 row vector. If more elaboration is needed, I am ready to provide.
Stephen23
Stephen23 2021 年 10 月 2 日
編集済み: Stephen23 2021 年 10 月 2 日
"Let N = 123456...23, (a 700 digits number) of double type."
Nope. As this answer already explains, that value is too large to store as DOUBLE type.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by