How to display exactly 2^64?
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 7 Oct 2020
"2^64 exactly as 18446744073709551615"
2^64 is actually 18446744073709551616, not 18446744073709551615.
Réponse acceptée
Plus de réponses (3)
KSSV
le 7 Oct 2020
val = 2^64 ;
fprintf("%f\n",val)
6 commentaires
越琪 吴
le 7 Oct 2020
KSSV
le 7 Oct 2020
val = 2^64 ;
fprintf("%.f\n",val)
越琪 吴
le 7 Oct 2020
越琪 吴
le 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
le 7 Oct 2020
Modifié(e) : Bruno Luong
le 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
le 7 Oct 2020
Modifié(e) : Bruno Luong
le 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 commentaires
Bruno Luong
le 7 Oct 2020
Modifié(e) : Bruno Luong
le 7 Oct 2020
Hmmm, why 53 bit precision won't play a role?
Ameer Hamza
le 7 Oct 2020
Modifié(e) : Ameer Hamza
le 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
le 7 Oct 2020
Thanks for pointing out, the statement was ambiguous. I have updated the comment.
Bruno Luong
le 7 Oct 2020
Modifié(e) : Bruno Luong
le 7 Oct 2020
Still not accurate: 3*2^1000 is exactly represented by double/single, it's not power of two either.
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!