summaryrefslogtreecommitdiff
path: root/243/CH4/EX4.3/4_03.sce
blob: 74c85ffca9a82dfa9266f1b22f613cc32348348d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Example No. 4_03
//Pg No. 64
clear ; close ; clc ;

a = 0.1
b = 0.4
for i = 1:8
    afrac(i) = floor(a*2)
    a = a*2 - floor(a*2)
    bfrac(i) = floor(b*2)
    b = b*2 - floor(b*2)
end
afrac_s = '0' + '.' + strcat(string(afrac)) //string form binary equivalent of a i.e 0.1
bfrac_s = '0' + '.' + strcat(string(bfrac))
mprintf('\n 0.1_10 = %s \n 0.4_10 = %s \n ', afrac_s , bfrac_s)
for j = 8:-1:1
    summ(j) = afrac(j) + bfrac(j)
    if summ(j) > 1 then
        summ(j) = summ(j)-2
        afrac(j-1) = afrac(j-1) + 1
    end
end
summ_dec = 0
for k = 8:-1:1
    summ_dec = summ_dec + summ(k)
    summ_dec = summ_dec*1/2 
end
disp(summ_dec,'sum =')
disp('Note : The answer should be 0.5, but it is not so.This is due to the error in conversion from decimal to binary form.')