How to read a very large number, say of 700 digits?
조회 수: 15 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
But have we stored al of those digits, correctly?
sprintf('%0.30f',X)
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'
XS = str2sym(S)
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.
추가 답변 (2개)
Sulaymon Eshkabilov
2021년 10월 1일
A good solution for this exercise is vpi() fcn developed by John D'Errico:
댓글 수: 0
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)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!