MATLAB: I am writing a script to convert decimal to floating point binary IEEE754. However, the answer returned number binary values (e.g. 1009999900).

binaryconvert

As being new to matlab, Im trying to create a program to convert decimal to floating binary from first principle. However when specify minimum output string of 32 characters, they did not return binary values (e.g. 0 or 1) but instead containing 9s. Could this be due to rounding off because of the floating form (decimal) by the program?
code
%%Converting input of base 10
format long g
prompt = 'Input value of x: ';
x=input(prompt);
%Converting of mantissa (x_1) to between 1 and 2 of base-2 scientific notation
n_exponent=0;
x_1=x;
if abs(x)<1
while abs(x_1)<1
n_exponent=n_exponent-1;
x_1=x/(2.^n_exponent);
end
else
while abs(x_1)>=2
n_exponent=n_exponent+1;
x_1=x/(2.^n_exponent);
end
end
%Exponent to 8-bit binary (single precision 127)
exp_127=n_exponent+127;
bit8=0;
order=1;
for n=1:8
bit8=rem(exp_127,2)*order+bit8;
exp_127=fix(exp_127/2);
order=order*10;
end
% Converting fraction to 23-bit mantissa (leading 1 hidden)
fraction=x_1-1;
bit23=0;
order=1;
for n=1:23
fraction=fraction*2;
bit23=order*fix(fraction)+bit23;
fraction=rem(fraction,1);
order=order*10;
end
%Determining sign of x
sign=0;
if x<0
sign=1;
end
%Full IEEE 754 binary floating point
output=sign*(10.^31)+bit8*(10.^23)+bit23;
sprintf('%032.f',output)
ans = 00111101101011109990000000000000

Best Answer

You cannot acurately store a decimal number greater than flintmax (~9e15) as double. Any integer above that will be rounded to the nearest decimal representable as double. So neither your bit23 (max.value > 1e23) and certainly not your output (max.value > 1e64) can be acurately stored as double.
Even as uint64, the maximum value that can be stored ( intmax('uint64'))is about 1e19, so still not enough.
My suggestion, store your bits as a vector of 0 and 1 or as a char vector of '0' and '1', e.g.:
bit23 = zeros(1, 23); %bit8 = zeros(1, 8)
for n = 1:23
bit23(n) = fix(fraction); %bit8(n) = rem(exp_127,2);
%...
fprintf('%d', [sign, fliplr(bit8), fliplr(bit23)])