%plots machine numbers for a machine that can represent all base 2
%numbers of the form 0.1a_1a_2...a_n*2^k, where |k|<=m

m=2;                %maximum absolute value of exponent
n=4;                %length of mantissa
x=zeros(2^n,n+1);   %rows of x are the machine numbers in binary
x(:,1) = ones(2^n,1);

for i=1:2^n-1
    j=i;
    while j>0
        for k=1:n
            if 2^(k-1)<=j && j<2^k
                x(i+1,k+1)=1;
                j=j-2^(k-1);
            end
        end
    end
end

disp('the mantissas are...')
x

y=zeros((2*m+1)*2^n,1);    %rows of y are machine numbers in decimal form
z=zeros((2*m+1)*2^n,1);    %for plotting purposes only

for i=1:(2*m+1)*2^n
    k=floor((i-1)*2^(-n));
    l=i-k*2^n;
    for j=1:n+1
        y(i)=y(i)+x(l,j)*2^(-j);
    end
    y(i)=y(i)*2^(k-m);
end

plot(y,z,'.b',-y,z,'.b',0,0,'.b')    %plots all the machine numbers
axis([-2.1 2.1 -1 1])