from
RECI:Calculates the reciprocal of a number with desired accuracy
by Divakar Roy
Reciprocal calculation giving answer with as much decimal digits as is wished.
|
| reci(num,acc)
|
% RECI:Calculates the reciprocal of a number with the desired accuracy
%
% Syntax:::::
% reci(a,b)
% where a is the number whose reciprocal is to be calculated
% and b is the max. no. of decimal digits you want in the answer
function reci(num,acc)
if(num<=1)
disp('THE NUMBER MUST BE GREATER THAN 1');
end
if(num>1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dvs=1;
a1=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dvsbg=[];
for(i=1:acc)
a2=a1*10;
while 1
if(a2<num)
a2=a2*10;
dvsbg=[dvsbg 0];
end
if(a2>=num)
break,end
end
dvs=1;
for(i=1:10)
a3=a2-num;
if(a3>=num)
dvs=dvs+1;
end
if(a3<num)
break,end
a2=a2-num;
end
dvsbg=[dvsbg dvs];
if(a3==0)
break,end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a1=a3;
end
if(length(dvsbg)<=acc)
data=dvsbg;
end
if(length(dvsbg)>acc)
data=[];
for(i=1:acc)
data(i)=dvsbg(i);
end
end
for(i=1:length(data))
datas(i)=int2str(data(i));
end
str1=['0.' datas];
disp('ans =')
disp(' ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gou=str1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[m n]=size(gou);
if(n/100>1)
for(coy=0:floor(n/100)-1)
for(i=1:100)
dion(i)=gou(i+coy*100);
end
disp(dion)
end
end
for(i=1:n-(floor(n/100)*100))
gion(i)=gou(i+floor(n/100)*100);
end
disp(gion);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
|
|
Contact us at files@mathworks.com