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 gprompt = '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)<1while abs(x_1)<1 n_exponent=n_exponent-1; x_1=x/(2.^n_exponent);endelse while abs(x_1)>=2n_exponent=n_exponent+1;x_1=x/(2.^n_exponent); endend%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