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

조회 수: 7 (최근 30일)
Ken Bannister
Ken Bannister 2025년 2월 4일
댓글: Walter Roberson 2025년 2월 5일
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일
편집: Fangjun Jiang 2025년 2월 4일
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일
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일
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by