%converts *any* (base-10) number a into 
%base 2 expansion with a maximum of 2*n digits

a = input('convert this number to base 2: ') %convert a to base 2
n = 50;                       %2*n = maximum length of base 2 expansion,
                              %with n places on each side of decimal point
b = zeros(1,n);               %b = base 2 expansion of a with n digits

x = a;
for i=1:2*n
    if 0<=x && x<2^(n-i)
        b(i) = 0;
    else
        b(i) = 1;
    end
    x=x-b(i)*2^(n-i);
end

y = [];                 %y = string version of vector b, with decimal point

for i=1:n
    y=[y,num2str(b(i))];   %insert expansion to the left of decimal point
end

y=[y,'.'];              %insert decimal point

for i=n+1:2*n
    y=[y,num2str(b(i))];   %insert expansion to the right of decimal point
end

disp('the binary expansion is...')
y

        