In: Computer Science
Write a program that converts a given floating point binary number with a 24-bit normalized mantissa and an 8-bit exponent to its decimal (i.e. base 10) equivalent. For the mantissa, use the representation that has a hidden bit, and for the exponent use a bias of 127 instead of a sign bit. Of course, you need to take care of negative numbers in the mantissa also. Use your program to answer the following questions: (a) Mantissa: 11110010 11000101 01101010, exponent: 01011000. What is the base-10 number? (b) What is the largest number (in base 10) the system can represent? (c) What is the smallest non-zero positive base-10 number the system can represent? (d) What is the smallest difference between two such numbers? Give your answer in base 10. (e) How many significant base-10 digits can we trust using such a representation?
Mention: Matlab
Solution for the above question is provided below. If any doubt please comment below.
a)
%Given inputs
expnt='01011000';
mant='111100101100010101101010';
% Signbit
signBit=str2num(mant(1));
%Find mantissa
manti=mant(2:24);
% compute exponent
exponent=0;
dj=length(expnt);
for di=1:length(expnt)
exponent=str2num(expnt(di))*2^(dj-1)+exponent;
dj=dj-1;
end
% Find bias
ex=-127+exponent;
decim=0;
%compute the value
for di=1:length(manti)
decim=str2num(manti(di))*2^(-di)+decim;
end
format long
% Result
res=(-1)^signBit*(1+decim)*2^(ex)
Output:
b)
To find it make exponent as all 1's and also make mantissa all as 1 except first bit in the above program
%Given inputs
expnt= '11111111';
mant='011111111111111111111111';
Output:
c)
%Given inputs
expnt= '00000000';
mant= '000000000000000000000000' ;
Output:
d)
First run the program using below mantissa and exponent
expnt='00000000';
mant='011111111111111111111111';
Second run the program using below mantissa and exponent
expnt='00000000';
mant='011111111111111111111110';
Find the difference of both result
Output:
e)
Number of significant base-10 digits = 2-23