matlab實現十進制小數轉二進制


clear,clc
a=input('Please input a number:');
f=input('Please input the accuracy:');
b=floor(a);
c=dec2bin(b);
d=a-b;
w=d*2;
if(d==0)
    disp(c);
else
    for i=1:f
        if(w==1)
            h(i)=1;
            break
        else if(floor(w)==1);
            h(i)=1;
            w=w-1;
            else
              h(i)=0; 
            end
       
        end
    end
end
    A=num2str(h);
    A(isspace(A))=[];
    z=strcat(c,'.',A);
    disp(z)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM