What is the max number of digits allowed for a base 2 number?

68 ビュー (過去 30 日間)
Ken Bannister
Ken Bannister 2025 年 2 月 4 日 15:55
コメント済み: Walter Roberson 2025 年 2 月 5 日 17:56
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?

回答 (1 件)

Fangjun Jiang
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
quadrillion = 1.0000e+15
a=uint64(2^64-1)
a = uint64 18446744073709551615
double(a)
ans = 1.8447e+19
intmax('uint64')
ans = uint64 18446744073709551615
dec2bin(a)
ans = '1111111111111111111111111111111111111111111111111111111111111111'
dec2bin(quadrillion)
ans = '11100011010111111010100100110001101000000000000000'
dec2hex(a)
ans = 'FFFFFFFFFFFFFFFF'
dec2hex(quadrillion)
ans = '38D7EA4C68000'
bin2dec(repmat('1',[1,64]))
Warning: Binary numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
ans = 1.8447e+19
bin2dec(repmat('1',[1,65]))
Error using bin2dec
Binary text has too many digits for specified or implied type suffix.
  9 件のコメント
Steven Lord
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))
x = 1×4
18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
ans = 4096
Walter Roberson
Walter Roberson 2025 年 2 月 5 日 17:56
This differs from
x = uint64(2^64)-uint64(1:4)
x = 1×4
18446744073709551614 18446744073709551613 18446744073709551612 18446744073709551611
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
x = 1×4
18446744073709551615 18446744073709551614 18446744073709551613 18446744073709551612
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 ExchangeLogical についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by