How to display exactly 2^64?

175 ビュー (過去 30 日間)
越琪 吴
越琪 吴 2020 年 10 月 7 日
編集済み: Stephen23 2020 年 10 月 12 日
I want to display 2^64 exactly as 18446744073709551615, without e. Without using vpa(), how can I display18446744073709551615?
Any help is appreciated!
Thanks a lot!
  1 件のコメント
Stephen23
Stephen23 2020 年 10 月 7 日
"2^64 exactly as 18446744073709551615"
2^64 is actually 18446744073709551616, not 18446744073709551615.

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 7 日
編集済み: Ameer Hamza 2020 年 10 月 7 日
First, 2^64 is not exactly 18446744073709551615. It is 18446744073709551616. 2 to any power cannot be an odd number.
Second, this number is beyond flintmax so the number near it, e.g., 2^64-1 cannot be exactly represented using double-precision floating-point. Even for uint64, the maximum value is 2^64-1. The other option is to use a variable precision library, i.e., vpa() or symbolic arithmetic.
If you don't want to use the symbolic toolbox, then download this big decimal library from here: https://www.mathworks.com/matlabcentral/fileexchange/36534-hpf-a-big-decimal-class. It works perfectly for variable precision mathematics.
x = hpf(2);
y = x^64
Result
y =
18446744073709551616
Some inaccurate statements were fixed as pointed out in Stephen's comment: https://www.mathworks.com/matlabcentral/answers/606841-how-to-display-exactly-2-64#comment_1041516
  7 件のコメント
越琪 吴
越琪 吴 2020 年 10 月 7 日
Thank you very much! This is exactly what I want!
John D'Errico
John D'Errico 2020 年 10 月 7 日
編集済み: John D'Errico 2020 年 10 月 7 日
Better, if you wanted to create arbitrarily high precision INTEGERS, is to use VPI. HPF is fine, except that you will need to define how many digits to use, whereas VPI has no explicit limit on the number of digitis except those induced by the available memory. HPF essentially uses a fixed number of decimal digits, although that number will be set by the user.
But since the request is to not need any external library to represent the number, both VPI and HPF fail that goal. One other option is to use java.
java.math.BigInteger.pow(java.math.BigInteger(2),64)
ans = 18446744073709551616
Or even seriously higher powers can be computed now.
java.math.BigInteger.pow(java.math.BigInteger(2),1024)
ans = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
In fact 2^1024 is just a small number in terms of the abilities of the java.math.BigInteger tools. I've used that tool to work with numbers with hundreds of thousands of digits, and that barely scratches the surface in terms of capability. For example, while I won't display it, 2^1e8 takes only a tiny fraction of a second to compute, and that is a pretty big integer.
tic,x=java.math.BigInteger.pow(java.math.BigInteger(2),100000000);toc
Elapsed time is 0.002492 seconds.
And Java is sort of already included with MATLAB. Except that it will one day go away, something we know lies in the future. But for now, the BigInteger solution is perfectly valid.

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

その他の回答 (3 件)

KSSV
KSSV 2020 年 10 月 7 日
val = 2^64 ;
fprintf("%f\n",val)
  6 件のコメント
Stephen23
Stephen23 2020 年 10 月 7 日
編集済み: Stephen23 2020 年 10 月 7 日
Does not display the correct value on R2012b:
>> fprintf('%f\n',2^64)
18446744073709552000.000000
>> fprintf('%.f\n',2^64)
18446744073709552000
Or R2015b:
>> fprintf('%f\n',2^64)
18446744073709552000.000000
>> fprintf('%.f\n',2^64)
18446744073709552000
Bruno Luong
Bruno Luong 2020 年 10 月 7 日
編集済み: Bruno Luong 2020 年 10 月 7 日
Same question with Walter regexp "why 53 bit precision won't play a role?"
And why this give wrong answer
>> val = 2^64 - 1;
>> fprintf("%.f\n",val-1)
18446744073709551616

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


Bruno Luong
Bruno Luong 2020 年 10 月 7 日
編集済み: Bruno Luong 2020 年 10 月 7 日
They teach me this when I was kid
% Raise to power 2^64
d=1;
for p=1:64
r=0;
for i=length(d):-1:1
a=2*d(i)+r;
r=floor(a/10);
d(i)=a-r*10;
end
if r>0
d = [r d];
end
end
% substract b (==1 here) to d=2^64
s = [1];
sp = [zeros(1,length(d)-length(s)) s];
r=0;
for i=length(d):-1:1
a=d(i)+r-sp(i);
r=floor(a/10);
d(i)=a-r*10;
end
if r<0
error('overflow (b>d)');
end
d = char(d+'0');
fprintf('%s\n', d)

Walter Roberson
Walter Roberson 2020 年 10 月 7 日
regexprep(sprintf('%.19lu\n', 2^64), {'\.', 'e.*$'}, {'', ''})
ans = '18446744073709551616'
  5 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 10 月 7 日
Thanks for pointing out, the statement was ambiguous. I have updated the comment.
Bruno Luong
Bruno Luong 2020 年 10 月 7 日
編集済み: Bruno Luong 2020 年 10 月 7 日
Still not accurate: 3*2^1000 is exactly represented by double/single, it's not power of two either.

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

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by