使用MATLAB创建一个将小数转换为IEEE 754浮点精度数字的函数?

使用MATLAB创建一个将小数转换为IEEE 754浮点精度数字的函数?,matlab,binary,floating-point,converter,ieee-754,Matlab,Binary,Floating Point,Converter,Ieee 754,我需要创建一个函数,该函数完全按照标题所述执行,并输出一个32个字符的字符串。到目前为止,我可以正确地得到有符号位和指数部分。我正在努力计算分数23位的部分。我正在做的作业也让你使用了一个奇怪的代码框架 function [out] = IEEE754Converter(N) if N==0 s= 0; e=0; f=0; out= '00000000000000000000000000000000'; else % assign sign value if

我需要创建一个函数,该函数完全按照标题所述执行,并输出一个32个字符的字符串。到目前为止,我可以正确地得到有符号位和指数部分。我正在努力计算分数23位的部分。我正在做的作业也让你使用了一个奇怪的代码框架

function [out] = IEEE754Converter(N)

if N==0
    s= 0;
    e=0;
    f=0;
    out= '00000000000000000000000000000000';

else
% assign sign value
if N<0
    s = '1';
else
    s = '0';
end

% take absolute value of N since we know sign already
N = abs(N);

% get exponent (unbiased and in decimal)
e_temp = floor(log2(N));

% turn e to binary
e = dec2bin(127+e_temp);

% get fraction (in decimal, lose the 1)
f_temp = N-floor(N);

% start f as empty
f = [];

% loop through decimal places
for i = 1:23
    if f_temp>2^-i   % check if fraction is big enough to have non-zero place holder
            % for 2^-i, then this place holder has a 1.
            f = [f,'1'];

            % remove placed decimal value from f_temp for future
            % iterations.
            f_temp = f_temp-1;

    else
            % in this case, this binary placeholder is 0
            f = [f,'0'];
    end
end
out= [s e f]; %Do Not change the lines after this
if (N~=0)
find (out=='1')
end

end % end IEEE75Converter
end
函数[out]=IEEE754转换器(N)
如果N==0
s=0;
e=0;
f=0;
out='00000000000000000000';
其他的
%赋值
如果N2^-i%,则检查分数是否足够大,是否有非零的占位符
%对于2^-i,则该占位符具有1。
f=[f,'1'];
%从f_temp中删除放置的十进制值,以备将来使用
%迭代。
f_temp=f_temp-1;
其他的
%在本例中,此二进制占位符为0
f=[f,'0'];
终止
终止
out=[SEF];%在此之后不要更改行
if(N~=0)
查找(out='1')
终止
结束%end IEEE75转换器
终止

您是否正在尝试重新实现sprintf(“%u32”,java.lang.Float.floatToRawIntBits(N))?您可以使用java方法。。。正如丹尼尔在我打字时说的that@Daniel基本上是的