arctan 在verilog 里是1qn或2qn格式,所以要把浮点数转换成1qn格式
1、dec2bin(十进制整数变为二进制)
Convert decimal to binary number in string
expand all in page
Syntax
str = dec2bin(d)
str = dec2bin(d,n)
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 n<0
fprintf(' %f is not a valid number\n',n)
end
while n~=0
strtemp=strcat(num2str(mod(n,2)),strtemp);
n=floor(n/2);
end
y=strtemp;
方案二:
function Mydec2bin(x)
%x----------the decimal number
%
%Recod of version:
% Date Programmer Description of change
% ======= ========= =================
% 9/26/2009 Iptest Original code
flag=x>0;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 code
if flag1==1;
flag3=1;
while T1>0
N0(i)=mod(T1,2);T1=fix(T1/2);
i=i+1;
end
n=length(N0);
for ii=1:n
N(ii)=N0(n-ii+1);
end
else
N=[];flag3=0;
end
%point part origin code
if flag2==1;
flag4=1;
while T2~=0 %maybe come out no ending situation
M(j)=(T2*2>1)*fix(T2*2)+(T2*2==1);
T2=(T2*2>1)*(T2*2-1)+(T2*2<1)*T2*2;
j=j+1;
end
else
M=[];flag4=0;
end
%for real situation
if flag==1%for positive number
if flag3==1;
disp('integer part origin code');
disp(N);
disp('integer part reverse code:');
disp(N);
else
disp('Not have integer part !');
end
if flag4==0;
disp('Not have point part !');
else
disp('point part origin code:');
disp(M);
disp('point part reverse code:');
disp(M);
end
else %for negative number
N=[1 N];
if flag1==1
if flag3==1;
disp('integer part origin code');
disp(N);
disp('integer part reverse code:');
N1=[N(1) ~N(2:end)];
disp(N1);
else
disp('Not have integer part !');
end
if flag4==0;
disp('Not have point part !');
else
disp('point part origin code:');
disp(M);
disp('point part reverse code:');
M1=[~M];
disp(M1);
end
else
disp('integer part reverse code for symbol code:');
disp(N);
if flag4==0;
disp('Not have point part !');
else
disp('point part origin code:');
disp(M);
disp('point part reverse code:');
M1=[~M];
disp(M1);
end
end
end
disp ('if there have some problem for the code please turn it out,thanks!')