What function returns (as an integer) the number of bits in a data type or class, e.g. returns 16 for 'int16' or 'uint16' variables, 32 (or whatever) for 'float' types, etc.?
53 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jim Tonti
el 11 de Abr. de 2019
The MATLAB function intmax() is close, but is only defined for integer types up to 32 bits. The function class() returns type variable type, but metainformation has to be inferred from documentation for the particular version of MATLAB after that.
Is there a function that returns the native bit size (i.e. the number of bits used for internal storage), or even more useful, all metadata about a given variable or class?
An extension to the function might return other metadata about the variable type, e.g. IEEE format, number of bits, signed/unsigned, etc.
Here's how it might work:
A = uint64(1234);
varinfo(A)
ans =
64
[nBits, meta] = varinfo(A)
nBits =
64
meta =
class = 'uint64'
nBits = 64
signed = 0
value = 1234
format = 'IEEE ...
nExp = 0
nMantissa = 64
max = {actual intmax for the type}
min = {actual minimum value > 0}
{and other useful metadata about the variable)
I'm really hoping something like this already exists...
1 comentario
Star Strider
el 11 de Abr. de 2019
The whos function is probably as close as you can currently get to what you want.
Respuesta aceptada
Stephen23
el 11 de Abr. de 2019
Editada: Stephen23
el 11 de Abr. de 2019
>> A = single(0);
>> S = whos('A');
>> S.class
ans = single
>> S.bytes*8
ans = 32
If you have non-scalar arrays to get the bits (or bytes) per element simply divide by the number of elements (does not work for empty arrays), or use cast:
B = cast(0,S.class)
S = whos('B');
S.bytes*8
S.class
0 comentarios
Más respuestas (1)
James Tursa
el 11 de Abr. de 2019
Editada: James Tursa
el 11 de Abr. de 2019
I think everything that MATLAB runs on currently uses IEEE single and double floating point formats. And the signed integer formats are two's complement storage. Char is actually two bytes per value. Logical is one byte per value. Differences would be big or little endian. So you could easily write a function that gives all of this info, but most of the results would be the same across all platforms for a given class.
What would you use such a function for?
3 comentarios
Walter Roberson
el 7 de Jun. de 2021
Side note: in some recent releases, Apple M1 has been supported. It is a 64 bit ARM CPU.
Microsoft is working on a 64 bit ARM operating system, but at the moment it is quite unreliable, and their 32 bit ARM is more stable but not great.
... so the list of architectures supported these days has to include ARM
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!