I am writing a script to convert decimal to floating point binary IEEE754. However, my answer returned number binary values (e.g. 1009999900).
Show older comments
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
1 Comment
SHAHMUSTAFA MUJAWAR
on 10 Apr 2018
hi, Kelvin this code is working fine for the whole number but for fraction values like (-0.001) it is showing some altered value in mantissa part and its reverse conversion in online 'single precision to decimal' calculator is (-0.0014), can you help me out from this problem
Accepted Answer
More Answers (0)
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!