summaryrefslogtreecommitdiff
path: root/1871/CH8/EX8.4/Ch08Ex4.sce
blob: cabb514e3421607dc079d6ccd96206a4b5dd002d (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
30
31
// Scilab code Ex8.4 : Pg:330(2008)
clc;clear;
function [bini]= decimal_binary(ni) // Function to convert decimal to binary
    bini = 0;
    i = 1;
    while (ni <> 0)
      rem = ni-fix(ni./2).*2; 
      ni = int(ni/2);
      bini = bini + rem*i;
      i = i * 10;
    end
endfunction   

function [binf]= decifrac_binfrac(nf) // Function to convert binary fraction to decimal fraction
    binf = 0; i = 0.1;
    while (nf <> 0)
      nf = nf*2;
      rem = int(nf); 
      nf = nf-rem;
      binf = binf + rem*i;
      i = i/10;
    end
endfunction   

n = 25.625;    // Initialize the decimal number
n_int = int(n);     // Extract the integral part
n_frac = n-n_int;   // Extract the fractional part
printf("Binary equivalent of %6.3f = %9.3f", n, decimal_binary(n_int)+decifrac_binfrac(n_frac)); 

// Result
// Binary equivalent of 25.625 = 11001.101