码迷,mamicode.com
首页 > 其他好文 > 详细

二进制与单精度浮点

时间:2019-12-16 09:53:33      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:yahoo   ==   return   cat   har   modify   free   cti   xtend   

单精度浮点: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
  

  

二进制与单精度浮点

标签:yahoo   ==   return   cat   har   modify   free   cti   xtend   

原文地址:https://www.cnblogs.com/mia1004/p/12047246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!