二进制与单精度浮点

单精度浮点: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: [email protected] || [email protected]
   %
   %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: [email protected] || [email protected]
   % 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
  

  

猜你喜欢

转载自www.cnblogs.com/mia1004/p/12047246.html
今日推荐