help with single precision floating point arithmetic

조회 수: 6 (최근 30일)
Aparna
Aparna 2011년 11월 9일
답변: Tony Scarcia 2018년 1월 10일
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!

채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 9일
dec2bin(typecast(single(pi),'uint32'),32)

추가 답변 (7개)

Titus Edelhofer
Titus Edelhofer 2011년 11월 9일
Hi,
guess what: the function is called 'single' ;-).
Titus

Daniel Shub
Daniel Shub 2011년 11월 9일
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
Aparna 2011년 11월 9일
@titus , i tried using the function but it doesn't produce the output in the 32 bit precision floating point format.
p.s. i need the output in binary
thanks for helping me with this.

Titus Edelhofer
Titus Edelhofer 2011년 11월 9일
Hi Aparna,
I understand:
num2hex(single(pi))
Is that what you are looking for?
Titus

Aparna
Aparna 2011년 11월 9일
hey! no i do not want the answer in Hexadecimal format i need it in binary 32 bit format. Could you help me with that?
  댓글 수: 4
James Tursa
James Tursa 2016년 8월 24일
So, did you try the above code? It works for matrices also.
Raiyyan  Masumdar
Raiyyan Masumdar 2016년 8월 25일
yes it works, thank you !!!

댓글을 달려면 로그인하십시오.


Aparna
Aparna 2011년 11월 9일
hey actually it works i guess! thank you daniel and titus

Tony Scarcia
Tony Scarcia 2018년 1월 10일
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)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by