double转换为二进制 |
您所在的位置:网站首页 › double转二进制 › double转换为二进制 |
Description
returns the str = dec2bin(d) binary representation of d as a string. d must be a nonnegative integer smaller than 2^52. str = dec2bin(d,n) produces a binary representation with at least n bits. The output of dec2bin is independent of the endian settings of the computer you are using. 2参考:https://zhidao.baidu.com/question/431648565.html?qbl=relate_question_0 这两个都是我收集的,验证过没问题方案一:%函数(1)含小数的十进制,会调用函数二function y=f_d2b(n) strn=strtrim(num2str(n)); p=length(strn); if isempty(find(strn=='.')) y=d2b(n); %这里 return; else for i=1:p if find(strn(i)=='.') k=i; break; end 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 ni_part==0 y=strcat('0.',strtemp); else y=strcat(ni_part,'.',strtemp); end end%%%%%%%%%%%%函数(2)转换整数部分function y=d2b(n)strtemp='';strn=strtrim(num2str(n));if n0;T=abs(x);T1=fix(T);T2=T-T1;flag1=T1>0;flag2=T2>0;i=1;j=1;%for positive number%integer part origin codeif flag1==1;flag3=1;while T1>0N0(i)=mod(T1,2);T1=fix(T1/2);i=i+1;endn=length(N0);for ii=1:nN(ii)=N0(n-ii+1);endelseN=[];flag3=0;end%point part origin codeif flag2==1;flag4=1;while T2~=0 %maybe come out no ending situationM(j)=(T2*2>1)*fix(T2*2)+(T2*2==1);T2=(T2*2>1)*(T2*2-1)+(T2*2 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |