diff options
Diffstat (limited to '1871/CH8/EX8.3')
-rwxr-xr-x | 1871/CH8/EX8.3/Ch08Ex3.sce | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/1871/CH8/EX8.3/Ch08Ex3.sce b/1871/CH8/EX8.3/Ch08Ex3.sce new file mode 100755 index 000000000..8862781e3 --- /dev/null +++ b/1871/CH8/EX8.3/Ch08Ex3.sce @@ -0,0 +1,32 @@ +// Scilab code Ex8.3 : Pg:328(2008)
+clc;clear;
+function [deci]= binary_decimal(ni) // Function to convert binary to decimal
+ deci = 0;
+ i = 0;
+ while (ni <> 0)
+ rem = ni-fix(ni./10).*10;
+ ni = int(ni/10);
+ deci = deci + rem*2.^i;
+ i = i + 1;
+ end
+endfunction
+
+function [decf]= binfrac_decifrac(nf) // Function to convert binary fraction to decimal fraction
+ decf = 0;
+ i = -1;
+ while (i >= -3)
+ nf = nf*10;
+ rem = round(nf);
+ nf = nf-rem;
+ decf = decf + rem*2.^i;
+ i = i - 1;
+ end
+endfunction
+
+n = 101.101; // Initialize the binary number
+n_int = int(n); // Extract the integral part
+n_frac = n-n_int; // Extract the fractional part
+printf("Decimal equivalent of %7.3f = %5.3f", n, binary_decimal(n_int)+binfrac_decifrac(n_frac));
+
+// Result
+// Decimal equivalent of 101.101 = 5.625
|