What is the max number of digits allowed for a base 2 number?
조회 수: 7 (최근 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일
편집: 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
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일
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일
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!