-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloatTest.cpp
More file actions
296 lines (234 loc) · 5.74 KB
/
floatTest.cpp
File metadata and controls
296 lines (234 loc) · 5.74 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
bool IsNagative(int nType,unsigned int *pa);
const char * GetSignificand(char * const pDest,int nType,unsigned int *pa);
const char * GetExponent(char * const pDest,int nType,unsigned int *pa);
void PrintBit(int nType,unsigned int *pa);
void PrintFloatBit(unsigned int *pa);
void PrintDoubleBit(unsigned int *pa);
float BCharToFloat(const char * pchar);
double BCharToDouble(const char * pchar);
int BCharToInt(const char * pchar);
void PrintFloatAndDouble(int nType,unsigned int *pa);
float MakeFloat(int nType,bool bIsNagative, char * pExponent, const char * pSignificand);
double MakeDouble(int nType,bool bIsNagative, char * pExponent, const char * pSignificand);
#define FLOAT_TYPE 1
#define DOUBLE_TYPE 2
void PrintFloatAndDouble(int nType,unsigned int *pa)
{
char szExponent[12],szSignificand[53];
memset(szExponent,0,12);
memset(szSignificand,0,53);
int nExponent;
PrintBit(nType,pa);
GetExponent(szExponent,nType,pa);
GetSignificand(szSignificand,nType,pa);
nExponent=BCharToInt(szExponent);
cout<<": "<<szExponent;
cout<<" e:"<<nExponent<<", E:"<<nExponent - (nType==FLOAT_TYPE ? 127: 1023)<<" ,";
if (nType==FLOAT_TYPE)
{
cout<<*((float*)pa)<<endl;
cout<<" "<<szSignificand<<" ";
cout<<MakeFloat(nType,IsNagative(nType,pa),szExponent,szSignificand)<<endl<<endl;
}
else
{
cout<<*((double*)pa)<<endl;
cout<<" "<<szSignificand<<" ";
cout<<MakeDouble(nType,IsNagative(nType,pa),szExponent,szSignificand)<<endl<<endl;
}
}
float MakeFloat(int nType,bool bIsNagative, char * pExponent, const char * pSignificand)
{
long nPower=(BCharToInt(pExponent) - (nType==FLOAT_TYPE ? 127: 1023));
int nSign= bIsNagative ? -1:1;
if (BCharToInt(pExponent)==0 && BCharToFloat(pSignificand)==0)
{
return 0;
}
if (nPower>0)
{
return nSign*(1+BCharToFloat(pSignificand))*(1<<nPower);
}
else
{
return (nSign*(1+BCharToFloat(pSignificand)))/(1<<(-nPower));
}
}
double MakeDouble(int nType,bool bIsNagative, char * pExponent, const char * pSignificand)
{
long nPower=(BCharToInt(pExponent) - (nType==FLOAT_TYPE ? 127: 1023));
int nSign= bIsNagative ? -1:1;
if (BCharToInt(pExponent)==0 && BCharToDouble(pSignificand)==0)
{
return 0;
}
if (nPower>0)
{
return nSign*(1+BCharToDouble(pSignificand))*(1<<nPower);
}
else
{
return (nSign*(1+BCharToDouble(pSignificand)))/(1<<(-nPower));
}
}
int BCharToInt(const char * pchar)
{
int nResult=0;
int nLen=strlen(pchar);
for (int i=0;i<nLen;i++)
{
nResult+=(pchar[i] - '0')*(1<<(nLen - 1 - i));
}
return nResult;
}
float BCharToFloat(const char * pchar)
{
float nResult=0;
int nLen=strlen(pchar);
for (int i=0;i<nLen;i++)
{
nResult+=((float)(pchar[i] - '0'))/(1<<(i+1));
}
return nResult;
}
double BCharToDouble(const char * pchar)
{
double nResult=0;
int nLen=strlen(pchar);
if(nLen>=32)
{
nLen=31;
}
for (int i=0;i<nLen;i++)
{
nResult+=((double)(pchar[i] - '0'))/(1<<(i+1));
}
return nResult;
}
void PrintBit(int nType,unsigned int *pa)
{
if (nType==FLOAT_TYPE)
{
PrintFloatBit(pa);
}
else
{
PrintDoubleBit(pa);
}
}
void PrintFloatBit(unsigned int *pa)
{
int i;
for (i=31;i>=0;i--)
{
cout<<(*pa>>i & 1)<<( i==31 || i==23 ? "-":"");
}
cout<<endl;
}
void PrintDoubleBit(unsigned int *pa)
{
int i;
for (i=31;i>=0;i--)
{
cout<<(*(pa+1)>>i & 1)<<( i==31 || i==20 ? "-":"");
}
for (i=31;i>=0;i--)
{
cout<<(*pa>>i & 1);
}
cout<<endl;
}
const char * GetExponent(char * const pDest,int nType,unsigned int *pa)
{
int nBegin= nType==FLOAT_TYPE ? 30:62;
int nEnd = nType==FLOAT_TYPE ? 22: 51;
int i;
if (nType==FLOAT_TYPE)
{
for (i=nBegin;i>nEnd;i--)
{
pDest[nBegin - i]=(*pa>>i & 1)+'0';
}
}
else
{
for (i=nBegin;i>nEnd;i--)
{
pDest[nBegin - i]=(*(pa+1)>>(i - 32) & 1)+'0';
}
}
return pDest;
}
const char * GetSignificand(char * const pDest,int nType,unsigned int *pa)
{
int nBegin= nType==FLOAT_TYPE ? 22:51;
int nLen= nType==FLOAT_TYPE ? 22:51;
int i;
if (nType==FLOAT_TYPE)
{
for (i=nBegin;i>=0;i--)
{
pDest[nBegin - i]=(*pa>>i & 1)+'0';
}
}
else
{
for (i=nBegin;i>=32;i--)
{
pDest[nBegin - i]=(*(pa+1)>>(i - 32) & 1)+'0';
}
for (i=31;i>=0;i--)
{
pDest[nBegin - i]=(*pa>>i & 1)+'0';
}
}
return pDest;
}
bool IsNagative(int nType,unsigned int *pa)
{
unsigned int *pTemp= nType==FLOAT_TYPE ? pa: pa+1;
return (*pTemp>>31 & 1) ==1;
}
int main()
{
float a;
// a=0;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=-11.2345612345678;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=-1.001;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=19.2;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=1.2;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=0.000001;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
//
// a=0.0000001;
// PrintFloatAndDouble(FLOAT_TYPE,(unsigned int *)&a);
double b=19.2;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=1.2;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=1.00002;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=0.00102;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=-1.912345;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=-1.9;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
b=0;
PrintFloatAndDouble(DOUBLE_TYPE,(unsigned int *)&b);
return 0;
}