forked from ksaifahmed/FPA_TEST_CASE_GEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPA_TC_GEN.cpp
135 lines (112 loc) · 3.54 KB
/
FPA_TC_GEN.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
#include <bits/stdc++.h>
using namespace std;
#define BIAS 7
//no of test cases
#define CASE_AMOUNT 30
//range of values
#define LO -255
#define HI 255
#define NUM_SYS 2
//decimal places
#define PRECISION 5
string binar(int dig)
{
string bin = ""; int rem;
while(dig)
{
rem = dig%NUM_SYS; dig/=2;
bin.append(to_string(rem));
}
reverse(bin.begin(), bin.end());
return bin;
}
tuple<string, string, string> f_to_b(float f)
{
int sgn = 0;
if(abs(f)!=f && f!=0.0) sgn = 1;
string sign = to_string(sgn);
string bin = binar(abs(int(f)));
string d_str = to_string(abs(f));
if(d_str.find(".")<d_str.length()) d_str = d_str.substr(d_str.find("."));
if(d_str.length()-1>PRECISION) d_str = d_str.substr(0, PRECISION+1);
float a_dec = stof(d_str); d_str = "";
while(!(a_dec<(1.0/pow(10, PRECISION))))
{
a_dec *= 2;
d_str.append(to_string(int(a_dec)));
a_dec = a_dec - float(int(a_dec));
}
return make_tuple(bin, d_str, sign);
}
tuple<string, int> pop_zeroes(string bin_l)
{
int i = 11, expbase = 0;
while(i--)
{
if(bin_l.at(0) == '0')
{
bin_l = bin_l.substr(1);
expbase--;
}else break;
if(i==0)
return make_tuple("0", 0);
}
if(bin_l.length()>1) bin_l = bin_l.substr(1);
expbase--;
return make_tuple(bin_l, (expbase+BIAS));
}
string standardize(string bin_f, string bin_l, string sgn)
{
string bin = ""; int exp;
if(bin_f.length()>1)
{
exp = bin_f.length()-1+BIAS;
bin = bin_f.substr(1) + bin_l;
if(bin.length()>11) bin = bin.substr(0, 11);
return sgn+" "+binar(exp)+" "+bin;
}
if(bin_f.empty())
{
tie(bin_l, exp) = pop_zeroes(bin_l);
return sgn+" "+binar(exp)+" "+bin_l;
}
return sgn + " " + binar(BIAS) + " " +bin_l;
}
int main(void){
int itr = CASE_AMOUNT;
srand(time(0));
float inp1, inp2, ans;
string bin_f, bin_l, exp;
freopen("test_cases.txt", "w", stdout);
cout << "large values:\n======================= "<<endl<<endl;
while(itr--)
{
inp1 = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
inp2 = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
ans = inp1 + inp2;
//std::cout << std::setprecision(PRECISION) << std::fixed;
tie(bin_f, bin_l, exp) = f_to_b(inp1);
cout << inp1 << " : " << standardize(bin_f, bin_l, exp) <<endl;
tie(bin_f, bin_l, exp) = f_to_b(inp2);
cout << inp2 << " : " << standardize(bin_f, bin_l, exp) <<endl;
tie(bin_f, bin_l, exp) = f_to_b(ans);
cout << ans << " : " << standardize(bin_f, bin_l, exp) <<endl<<endl<<endl;
}
cout << "small values:\n======================= "<<endl<<endl;
itr = CASE_AMOUNT;
while(itr--)
{
inp1 = -1 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2.0)));
inp2 = -1 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2.0)));
ans = inp1 + inp2;
//std::cout << std::setprecision(PRECISION) << std::fixed;
tie(bin_f, bin_l, exp) = f_to_b(inp1);
cout << inp1 << " : " << standardize(bin_f, bin_l, exp) <<endl;
tie(bin_f, bin_l, exp) = f_to_b(inp2);
cout << inp2 << " : " << standardize(bin_f, bin_l, exp) <<endl;
tie(bin_f, bin_l, exp) = f_to_b(ans);
cout << ans << " : " << standardize(bin_f, bin_l, exp) <<endl<<endl<<endl;
}
fclose(stdout);
return 0;
}