二进制与单精度浮点

Posted mia1004

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二进制与单精度浮点相关的知识,希望对你有一定的参考价值。

单精度浮点:1符号S+8指数E+23小数M

1.M*2^(127 + E)

例如:-24 = -1.1^4,c = -24,指数为131,小数M = 1

技术图片

 

 浮点数与定点数转化

 

function y=f_b2d(n)
    
   %-----------------------------------------------------------------------
   %PROGRAMMER:
   %GORDON NANA KWESI AMOAKO
   %EMAIL: kwesiamoako@gmail.com || gsnka@yahoo.com
   %
   %You can contact me with any questions and feel free to modify to suit
   %you needs. I don‘t think this is the best though, but it‘s good.
   %-----------------------------------------------------------------------
   % Modified: 22 FEB 2012
   
   %MATLAB‘s bin2dec( ) function just converts whole binary numbers
   %This function f_b2d( ) provides an extended functionality of converting
   %numbers with binary fractions or binary fractions only to decimalnumbers
   %-----------------------------------------------------------------------
   
   %
   %INPUT: n, n is a String of Binary numbers e.g. f_b2d(‘11001.101‘) gives us 

   %OUTPUT: A Decimal Number
   %  
   %SAMPLE input and output
   %-------------------------------
   %f_b2d(‘11001.101‘)=25.6250
   %f_b2d(‘110000111.111111101‘)=391.9941
   %f_b2d(‘0.11001111101‘)= 0.8110
   
   %Converting the Number to String


    n=strtrim(n);
    numadd=0;
    t=length(n);

    %----------------------------------------------------------------------
    if isempty(find(n==‘.‘))
        y=bin2dec(n);     
    else
    %----------------------------------------------------------------------
        j=find(n==‘.‘);

        i_part=n(1:j-1);
        f_part=n(j+1:end);
        
        % Look for the indices of the fraction part which are ones and
        % a simple computation on it to convert to decimal
        d_fpart=sum(0.5.^find(f_part==‘1‘));
        
        y=f_b2d(i_part)+d_fpart; % Concatenate the results

    %----------------------------------------------------------------------
    end

  

function y=f_d2b(n)

   
    
   %-----------------------------------------------------------------------
   %PROGRAMMER:
   %GORDON NANA KWESI AMOAKO
   %EMAIL: kwesiamoako@gmail.com || gsnka@yahoo.com
   % Modified: 14 JULY 2012
   %-----------------------------------------------------------------------
   
   
   %MATLAB‘s dec2bin( ) function just converts whole numbers to binary
   %This function f_d2b( ) provides an extended functionality of converting
   %numbers with fractions or fractions only to BINARY
   %----------------------------------------------------------------------
   %
   %INPUT: n e.g. 25.625
   %OUTPUT: Binary number e.g. 11001.101
   %
    %Converting the Number to String
    
     
    strn=strtrim(num2str(n));
     
    %------------------------------------------------
    if isempty(find(strn==‘.‘))   
        y=d2b(n);
        return;        
    else
    %------------------------------------------------
        k = find(strn ==‘.‘);

    end
    
    
    %Retrieving INTEGER and FRACTIONAL PARTS as strings
    i_part=strn(1:k-1);
    f_part=strn(k:end);
    
    
    %Converting the strings back to numbers
    ni_part=str2num(i_part);
    nf_part=str2num(f_part);
    
    ni_part=d2b(ni_part);
      
    strtemp=‘‘;
    

    
    temp=nf_part;
    %-------------------------------------------------
    t=‘1‘;s=‘0‘;
    
    while nf_part>= 0
        nf_part=nf_part*2;
        if (nf_part==1) || (nf_part==temp)
            strtemp=strcat(strtemp,t);
            break;
        elseif nf_part>1
            strtemp=strcat(strtemp,t);
            nf_part=nf_part-1;
        else
            strtemp=strcat(strtemp,s);
        end
    end
    
    
     
     if(i_part==‘0‘)
        y=strcat(‘0.‘,strtemp);
         
     else
        y=strcat(ni_part,‘.‘,strtemp);
     end
     
     %------------------------------------------------

    end
  

  

以上是关于二进制与单精度浮点的主要内容,如果未能解决你的问题,请参考以下文章

Python的基本类型

在 C 代码的 printf 语句中使用时,不会显示浮点或双精度值

Python3 基本类型

浮点型是啥意思

Python数字型整理(整型布尔型浮点型复数操作符内建函数与工厂函数控制数字精度)

浮点运算与boost.multiprecision