What is the max number of digits allowed for a base 2 number?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Fangjun Jiang
el 4 de Feb. de 2025
Editada: Fangjun Jiang
el 4 de Feb. de 2025
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 comentarios
Steven Lord
el 5 de Feb. de 2025
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
el 5 de Feb. de 2025
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)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!