MATLAB data type expression problem

1 vue (au cours des 30 derniers jours)
cui,xingxing
cui,xingxing le 30 Sep 2017
Réponse apportée : OCDER le 30 Sep 2017
(2^61-5):(2^61),This number can't get a vector with 5 elements,why?
Another question,In matlab command window ,input :
a = isequal(int64(2^63-2^9) , int64(2^63-2^1))
the result is:
a =
logical
1
why a == 1 ,not zero ?

Réponse acceptée

OCDER
OCDER le 30 Sep 2017
The issue is caused because MATLAB uses 16-digits of precision for double calculations, and 2^61 exceeds 16 digits. The excess digits are just rounded off, hence (2^63 - 2) does not see the " - 2" adjustment after rounding.
To fix the issue, immediately convert to int64 for larger numbers > 16 digits of precision.
(2^61-5):(2^61) %Gives you 1 number, exceeds 16-digit precision
int64(2^61)-5:int64(2^61) %Gives you 6 numbers, within range
a = isequal(int64(2^63 - 2^9) , int64(2^63 - 2)) %Returns 1, since 2^63 exceeds 16-digit precision (19-digit needed)
a = isequal(int64(2^63)- 2^9 , int64(2^63)-2) %Returns 0

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!