How to display exactly 2^64?
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
More Answers (3)
KSSV
on 7 Oct 2020
val = 2^64 ;
fprintf("%f\n",val)
6 Comments
越琪 吴
on 7 Oct 2020
KSSV
on 7 Oct 2020
val = 2^64 ;
fprintf("%.f\n",val)
越琪 吴
on 7 Oct 2020
越琪 吴
on 7 Oct 2020
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
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)
regexprep(sprintf('%.19lu\n', 2^64), {'\.', 'e.*$'}, {'', ''})
5 Comments
Bruno Luong
on 7 Oct 2020
Edited: Bruno Luong
on 7 Oct 2020
Hmmm, why 53 bit precision won't play a role?
Ameer Hamza
on 7 Oct 2020
Edited: Ameer Hamza
on 7 Oct 2020
Edited according to Stephen comment below
Luong, double-precision, is able to represent all powers of 2 accurately. Even 2^1023 is correctly represented. What it cannot do is 2^64-1, or any other integer beyond flintmax except powers of 2.
>> y1 = sym(2)^1023;
>> y2 = sym(2^1023);
>> isequal(y1, y2)
ans =
logical
1
But following fails
>> y1 = sym(2)^1023-1;
>> y2 = sym(2^1023-1);
>> isequal(y1, y2)
ans =
logical
0
"double-precision, is able to represent all powers of 2 accurately. Even 2^1023 is correctly represented. What it cannot do is 2^64-1, or any other integer beyond flintmax."
???
Powers of two (like 2^1023 or 2^64) are integers beyond flintmax. The statement "What it cannot do is... any other integer beyond flintmax" contradicts the first/second sentences.
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.
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!