計算機進制問題---浮點數轉換爲二進制數

-------------------------------------
典型例題 27:計算機進制問題---浮點數轉換爲二進制數
-------------------------------------
 1    // double2bin.cc
 2    #include <iostream>
 3    #include <math.h>
 4   
 5    using namespace std;
 6   
 7    void normalize(double &f,char &e)
 8    {
 9        e = 0;
10   
11        // numbers larger than 2 we reduce
12        // down to 1 plus a fraction,
13        // and a compensating exponent
14        while(fabs(f) > 2)
15            {
16                f /= 2;
17                e++;
18            }
19   
20        // numbers smaller than 1 we promote
21        // up to 1 plus a fraction,
22        // and a compensating exponent
23        while(fabs(f) < 1)
24            {
25                f *= 2;
26                e--;
27            }
28    }
29   
30    void double2bin(double f,char *b)
31    {
32        char e1;
33        int e;
34        int i;
35   
36        normalize(f,e1);
37   
38        // add the bias
39        e = int(e1) + 1023;
40   
41        // set the sign bit
42        b[0] = (f < 0) ? '1':'0';
43   
44        f = fabs(f);
45   
46        // remove the leftmost 1 bit
47        f -= 1;
48   
49        b[1]=b[13]=' ';
50   
51        // convert the exponent
52        for(i=11;i>0;i--)
53            {
54                b[i+1] = e%2 + '0';
55                e/=2;
56            }
57   
58        // convert the mantissa
59        for(i=1;i<=52;i++)
60            {
61                int bit = (f>=pow(2,-i));
62   
63                b[i+13] = (bit) ? '1':'0';
64                if (bit) f -= pow(2,-i);
65            }
66   
67        b[66]='/0';
68    }
69   
70    int myatoi(char *str)
71    {
72        int sum = 0;
73        int index=8;
74        while(*str!='/0')
75            {
76                if(index == 0)
77                    index = 1;
78               
79                sum+=(*str-'0')*index;
80                index = index/2;
81                str++;
82            }
83        return sum;
84    }
85   
86    void bin2hex(char *b,char *h)
87    {
88        char *ptr=b;
89        int i = 0;
90        int j = 0;
91        char temp[5];
92        int c;
93        char hex[65];
94        while(*ptr!='/0')
95            {
96                if(*ptr==' ')
97                    {
98                        ptr++;
99                        continue;
100                    }
101                hex[i]=*ptr;
102                ptr++;
103                i++;
104            }
105        hex[i] = '/0';
106        int count = 0;
107        for(i=0;i<64;)
108            {
109                for(j=0;j<=3;j++)
110                    {
111                        temp[j] = hex[i++];
112                    }
113                temp[j] = '/0';
114                c = myatoi(temp);
115                    switch(c)
116                        {
117                        case 0:
118                            h[count++] = '0';
119                            break;
120                        case 1:
121                            h[count++] = '1';
122                            break;
123                        case 2:
124                            h[count++] = '2';
125                            break;
126                        case 3:
127                            h[count++] = '3';
128                            break;
129                        case 4:
130                            h[count++] = '4';
131                            break;
132                        case 5:
133                            h[count++] = '5';
134                            break;
135                        case 6:
136                            h[count++] = '6';
137                            break;
138                        case 7:
139                            h[count++] = '7';
140                            break;
141                        case 8:
142                            h[count++] = '8';
143                            break;
144                        case 9:
145                            h[count++] = '9';
146                            break;
147                        case 10:
148                            h[count++] = 'A';
149                            break;
150                        case 11:
151                            h[count++] = 'B';
152                            break;
153                        case 12:
154                            h[count++] = 'C';
155                            break;
156                        case 13:
157                            h[count++] = 'D';
158                            break;
159                        case 14:
160                            h[count++] = 'E';
161                            break;
162                        case 15:
163                            h[count++] = 'F';
164                            break;
165                        default:
166                            cout<<"error number"<<endl;
167                            //break; don't need break statement;
168                        }
169            }
170        h[17] = '/0';
171       
172    }
173   
174    int main(void)
175    {
176        char b[67];
177        char h[18];
178        double f;
179        cout<<"Pleas input the double number:"<<endl;
180        cin>>f;
181       
182        bzero(b,67);
183        bzero(h,18);
184        double2bin(f,b);
185        bin2hex(b,h);
186        cout << f << " (base 10) = "
187             << b << " (floating point base 2)"<<endl;
188        cout << f << " (base 10) = "
189             <<"0X"<<h << " (floating point base 16)"<<endl;
190        return 0;
191    }
---------------------------
$ ./a.out
Pleas input the double number:
5.01
5.01 (base 10) = 0 10000000001 0100000010100011110101110000101000111101011100001010 (floating point base 2)
5.01 (base 10) = 0X40140A3D70A3D70A (floating point base 16)
----------------------------
 1    // float2bin.cc
 2    #include <iostream>
 3    #include <math.h>
 4   
 5    using namespace std;
 6   
 7    void normalize(float &f,char &e)
 8    {
 9        e = 0;
10   
11        // numbers larger than 2 we reduce
12        // down to 1 plus a fraction,
13        // and a compensating exponent
14        while(fabs(f) > 2)
15            {
16                f /= 2;
17                e++;
18            }
19   
20        // numbers smaller than 1 we promote
21        // up to 1 plus a fraction,
22        // and a compensating exponent
23        while(fabs(f) < 1)
24            {
25                f *= 2;
26                e--;
27            }
28    }
29   
30    void float2bin(float f,char *b)
31    {
32        char e1;
33        int e;
34        int i;
35   
36        normalize(f,e1);
37   
38        // add the bias
39        e = int(e1) + 127;
40   
41        // set the sign bit
42        b[0] = (f < 0) ? '1':'0';
43   
44        f = fabs(f);
45   
46        // remove the leftmost 1 bit
47        f -= 1;
48   
49        b[1]=b[10]=' ';
50   
51        // convert the exponent
52        for(i=8;i>0;i--)
53                  {
54                b[i+1] = e%2 + '0';
55                e/=2;
56            }
57   
58        // convert the mantissa
59        for(i=1;i<24;i++)
60            {
61                int bit = (f>=pow(2,-i));
62   
63                b[i+10] = (bit) ? '1':'0';
64                if (bit) f -= pow(2,-i);
65            }
66   
67        b[34]='/0';
68    }
69   
70    int main(void)
71    {
72        char b[35];
73        float f=5.01;
74   
75        float2bin(f,b);
76        cout << f << " (base 10) = "
77             << b << " (floating point base 2)"<<endl;
78        return 0;
79    }
--------------------------
$ ./a.out
5.01 (base 10) = 0 10000001 01000000101000111101100 (floating point base 2)
--------------------------
注意:doubletobin.cc裏面e1還是char,這裏肯定要出問題,聰明的讀者,這裏會有什麼問題呢?
你試一試,4.42367e+38輸入看結果對不?你能得到以下結果嗎?爲什麼?
4.42367e+38 (base 10) = 0X47F4CCCC900F55D3 (floating point base 16)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章