help with single precision floating point arithmetic
Mostrar comentarios más antiguos
Hello Can you help me with the single precision floating point arithmetic in matlab I need to generate twiddle factors and obtain the output in the binary format( which is the standard IEEE 754 single precision 32-bit floating point format).Is there any function in MATLAB that will directly convert any value such as sine(pi/4)into the single precision format?
Thanks!
Respuesta aceptada
Más respuestas (7)
Titus Edelhofer
el 9 de Nov. de 2011
0 votos
Hi,
guess what: the function is called 'single' ;-).
Titus
Daniel Shub
el 9 de Nov. de 2011
You do need to be careful with MATLAB and converting to single. Potentially not obvious is that
isequal(single(pi*pi), single(pi)*single(pi))
Also, I believe that in some cases single precision numbers are converted to double precision during calcualtions
Aparna
el 9 de Nov. de 2011
0 votos
Titus Edelhofer
el 9 de Nov. de 2011
Hi Aparna,
I understand:
num2hex(single(pi))
Is that what you are looking for?
Titus
Aparna
el 9 de Nov. de 2011
0 votos
4 comentarios
Titus Edelhofer
el 9 de Nov. de 2011
binary 32 bit format? So 32 zeros and ones?
dec2bin(hex2dec(num2hex(single(pi))))
and add zeros on the left for having indeed 32 bits ...
Titus
Raiyyan Masumdar
el 24 de Ag. de 2016
Hi Titus,
I was actually looking for conversion of a matrix of real numbers into single precision binary. Any idea?
James Tursa
el 24 de Ag. de 2016
So, did you try the above code? It works for matrices also.
Raiyyan Masumdar
el 25 de Ag. de 2016
yes it works, thank you !!!
Tony Scarcia
el 10 de En. de 2018
This will convert a single precision float to 32 bit binary using IEEE 754 format where;
- s is the sign bit (bit 31)
- e is the exponent (bits 30 to 23)
- m is the mantissa (bits 22 to 0)
% a is the float number
% b is the 32 bit binary converted number
b = padarray(hexToBinaryVector(num2hex(single(a)))',32-length(hexToBinaryVector(num2hex(single(a)))'),'pre')';
sign = b(1)
exp = b(2:9)
mantissa = b(10:32)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!