What is the max number of digits allowed for a base 2 number?
68 ビュー (過去 30 日間)
古いコメントを表示
I am working with base 10 integers in the quadrillion range. My understanding is that when converted to base 2, such numbers end up being about 50 digits. What is the max number of digits MATLAB allows for base 2 numbers?
0 件のコメント
回答 (1 件)
Fangjun Jiang
2025 年 2 月 4 日 18:04
編集済み: Fangjun Jiang
2025 年 2 月 4 日 19:22
The default data type in MATLAB is "double", which means 64 bits. When used to represent a base 2 number, its maximum value is 2^64-1 (the value of intmax('uint64')), which is significantly larger than a quadrillion (10^15).
For 64 bits or under, you can use bin2dec() directly.
quadrillion =10^15
a=uint64(2^64-1)
double(a)
intmax('uint64')
dec2bin(a)
dec2bin(quadrillion)
dec2hex(a)
dec2hex(quadrillion)
bin2dec(repmat('1',[1,64]))
bin2dec(repmat('1',[1,65]))
9 件のコメント
Steven Lord
2025 年 2 月 5 日 14:48
Do note that as many of us have said, if you're working with numbers near intmax('uint64') you need to be a bit careful with how you create them. Simply defining them numerically and calling uint64 on the result won't necessarily work.
x = uint64(2^64-(1:4))
By the time MATLAB "sees" the uint64 call it's already performed the calculation in double precision. How far apart are numbers on the order of 2^64 spaced?
eps(2^64)
Walter Roberson
2025 年 2 月 5 日 17:56
This differs from
x = uint64(2^64)-uint64(1:4)
The 2^64 gets evaluated in double precision. Then uint64() gets applied and saturates the 2^64 to (2^64-1). Then the subtraction takes place.
Compare
x = uint64(18446744073709551615) - uint64(0:3)
In this case, uint64(LITERALCONSTANT) is parsed as a uint64 value without loss of precision -- but that parsing only applies to literal constants, not to expressions (even if the expressions are effectively constant)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!