MATLAB data type expression problem
6 views (last 30 days)
Show older comments
(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 ?
0 Comments
Accepted Answer
OCDER
on 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
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!