matlab中实现 IEEE754浮点数 与 一般十进制数之间 互相转换的方法


%2020/12/2 11:42:31

clc
format long


% IEEE754 to dec
a = '40800000'
a = dec2bin(hex2dec(a),32)
M = bin2dec(a(10:32))
E = bin2dec(a(2:9))
x = (-1)^a(1)*(1 + M*2^-23)*2^(E-127)


% dec to IEEE754

result = cos(x)

if(result<0)
S = '1'
else
S= '0'
end

result = abs(result)

i = 0
while(result<1)
result = result * 2
i = i+1
end

j = 0
while(result>2)
result = result/2
j = j+1
end

E = i + j
if(j == 0)
E = -E
end
E = E+127
E = dec2bin(E,8)
M23 =dec2bin(floor((result-1)*2^23),23)
r = [S,E,M23]
dec2hex(bin2dec(r))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM