-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19112064_Formats.cpp
168 lines (167 loc) · 4.2 KB
/
19112064_Formats.cpp
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
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string tostring(int n, int T)
{
string out = "";
for(int i = 0; i < T; i++)
{
int x = (n & (1<<(T-i-1)));
if(x != 0)
out+='1';
else
out+='0';
}
return out;
}
void solve(string n, int T, int E, fstream& fout)
{
char sign_temp = n[0];
int integer=0;
int k =0;
int i;
for(i=n.length()-1;i>0;i--)
{
if(n[i]=='1')
{
integer = integer + pow(2,k);
}
k++;
}
if(sign_temp=='1')
integer *= -1;
float fixed_point = 0;
k =0;
for(i=E;i>0;i--)
{
if(n[i]=='1')
{
fixed_point = fixed_point + pow(2,k);
}
k++;
}
k=1;
for(i=E+1;i<n.length();i++)
{
if(n[i]=='1')
{
fixed_point = fixed_point + pow(2,-k);
}
k++;
}
if(sign_temp=='1' && fixed_point!=0)
fixed_point *= -1;
float fraction = 0;
int exponent=0;
k=1;
for(i=E+1;i<n.length();i++)
{
if(n[i]=='1')
{
fraction = fraction + pow(2,-k);
}
k++;
}
k=0;
for(i=E;i>0;i--)
{
if(n[i]=='1')
{
exponent = exponent + pow(2,k);
}
k++;
}
int bias = pow(2,E-1)-1;
float fp;
string fpstring;
int stringflag = 0;
if(exponent==pow(2,E)-1 )
{
stringflag = 1;
if(fraction==0)
{
if(sign_temp=='0')
fpstring = "+infinity";
else
fpstring = "-infinity";
}
else
{
fpstring = "NAN";
}
}
else if(exponent == 0)
{
if(fraction==0)
fp=0;
else
{
stringflag = 2;
fpstring = " Denormal number ";
fp=(0+fraction) * pow(2,1-bias);
if(sign_temp=='1')
fp *= -1;
}
}
else
{
fp = (1+fraction) * pow(2,exponent-bias);
if(sign_temp=='1')
fp *= -1;
}
if(stringflag == 0)
fout << n << setw(20) << integer << setw(20) << fixed_point <<setw(20) <<fp;
else if(stringflag == 1)
fout << n << setw(20) << integer << setw(20) << fixed_point <<setw(20) <<fpstring;
else
fout << n << setw(20) << integer << setw(20) << fixed_point << fpstring <<fp;
fout << "\n";
}
int main(int argc,char* argv[])
{
string infile = argv[1];
string T_temp = argv[1];
string E_temp = argv[2];
string option = argv[3];
string n;
if(argc == 5)
n = argv[4];
int T=0,E=0,i;
for(i=0;i<T_temp.length();i++)
{
char temp = T_temp[i];
int tempk = int(temp-'0');
T = T*10 + tempk;
}
for(int i=0;i<E_temp.length();i++)
{
char temp = E_temp[i];
int tempk = int(temp-'0');
E = E*10 + tempk;
}
if(option!="All")
{
fstream fout;
string fname = "19112064_" + T_temp + "_" + E_temp + "_Single_" + n + ".txt";;
fout.open(fname.data(),ios::out);
fout << "combinations integer fixed point FP\n";
solve(n,T,E,fout);
}
else
{
fstream fout;
string fname = "19112064_" + T_temp + "_" + E_temp + "_All.txt";
fout.open(fname.data(),ios::out);
fout << "combinations integer fixed point FP\n";
int i = 0;
for(;i < pow(2,T);i++)
{
string n = tostring(i,T);
solve(n,T,E,fout);
}
}
return 0;
}