% The following demonstrates effects of avoiding subtractions. 

format long e
x=1e-3;
a=single(sqrt(1+x));
b=1;
c=a-b;
fprintf('\n  Single precision: \n  a \t= \t%2.20f\n  b \t= \t%2.20f\n  c \t= \t%2.20f\n\n',a,b,c);

a=sqrt(1+x);
b=1;
c_double=a-b;
fprintf('\n  Double precision: \n  a \t= \t%2.20f\n  b \t= \t%2.20f\n  c \t= \t%2.20f\n\n',a,b,c_double);

fprintf('\n  OR IN FLOATING POINT FORM \n  c \t\t= \t%1.16e\n  c_double \t= \t%1.16e\n\n',c,c_double);

% Using 1/(sqrt(1+x)+1)
a=single(sqrt(1+x));
d=single(1/(a+1)); 
fprintf('\n  1/(sqrt(1+x)+1) in Single precision: \n  a      \t= \t%2.20f\n  1/(a+1) \t= \t%2.20f\n  x/(a+1) \t= \t%2.20f\n\n',a,d,d*x);
%
%The same values in double precsiion 
a=(sqrt(1+x));
d=(1/(a+1)); 
fprintf('\n  1/(sqrt(1+x)+1) in double precision: \n  a      \t= \t%2.20f\n  1/(a+1) \t= \t%2.20f\n  x/(a+1) \t= \t%2.20f\n\n',a,d,d*x);
%
