%% Floating Point Numbers
%

format short
1/2

format rat
1/2

%

d=4;
z=0:2^d-1

%

f=2^(-d)*z
F=1+f

% Change exponent
(1+f)*2^1

%

% Example to compute fl(x)
x=2*pi;
d=2;
z=0:2^d-1;
format short

% Find interval [ 2^e, 2^(e+1) )
e=floor(log2(x));            % Finds e
interval=(1+z*2^(-d))*2^e;

% Locate closest floating point number to x.
[val,ind]=min(abs(interval-x))
flx=interval(ind)

%

% Absolute accuracy
abs(flx-x)

% Relative accuracy
abs(flx-x)/abs(x)

% Accurate digits
-log10(ans)

%

% Finding the machine epsilon
n=0;
while 1+2^(-n)>1
    n=n+1;
end
n

%

% Verify
format long
% For this n, 1+2^(-n) = 1 in F
display(['(1+2^(-',num2str(n),')) -1 = ', num2str((1+2^(-n)))-1])

% For n-1, 1+2^(-n) > 1
display(['(1+2^(-',num2str(n-1),')) -1 = '])
(1+2^(-n+1))-1
eps