How to display exactly 2^64?
    132 views (last 30 days)
  
       Show older comments
    
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 Comment
  Stephen23
      
      
 on 7 Oct 2020
				"2^64 exactly as 18446744073709551615"
2^64 is actually 18446744073709551616, not 18446744073709551615.
Accepted Answer
  Ameer Hamza
      
      
 on 7 Oct 2020
        
      Edited: Ameer Hamza
      
      
 on 7 Oct 2020
  
      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 Comments
  John D'Errico
      
      
 on 7 Oct 2020
				
      Edited: John D'Errico
      
      
 on 7 Oct 2020
  
			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)
Or even seriously higher powers can be computed now. 
java.math.BigInteger.pow(java.math.BigInteger(2),1024)
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
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.
More Answers (3)
  KSSV
      
      
 on 7 Oct 2020
        val = 2^64 ; 
fprintf("%f\n",val)
6 Comments
  Bruno Luong
      
      
 on 7 Oct 2020
				
      Edited: Bruno Luong
      
      
 on 7 Oct 2020
  
			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
      
      
 on 7 Oct 2020
        
      Edited: Bruno Luong
      
      
 on 7 Oct 2020
  
      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)
0 Comments
  Walter Roberson
      
      
 on 7 Oct 2020
        regexprep(sprintf('%.19lu\n', 2^64), {'\.', 'e.*$'}, {'', ''})
5 Comments
  Ameer Hamza
      
      
 on 7 Oct 2020
				Thanks for pointing out, the statement was ambiguous. I have updated the comment.
  Bruno Luong
      
      
 on 7 Oct 2020
				
      Edited: Bruno Luong
      
      
 on 7 Oct 2020
  
			Still not accurate: 3*2^1000 is exactly represented by double/single, it's not power of two either.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





