-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.h
More file actions
216 lines (191 loc) · 5.27 KB
/
Copy pathsection.h
File metadata and controls
216 lines (191 loc) · 5.27 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
//
// Created by ydl on 18-6-16.
//
#ifndef PROJECT_SECTION_H
#define PROJECT_SECTION_H
#include <type_traits>
#include <climits>
#include <cfloat>
#include <cmath>
using namespace std;
static float add(float a, float b) {
if (FLT_MAX - max(a, b) < min(a, b))
return FLT_MAX;
else
return a + b;
}
static float multi(float a, float b) {
if (max(a, b) == 0)return 0;
if (FLT_MAX / max(a, b) < min(a, b))
return FLT_MAX;
else
return a * b;
}
static float divi(float a, float b) {
return a / b;
}
class section {
float l, r;
float inf;
public:
void print()
{
cout<<"{"<<l<<" , "<<r<<"}"<<endl;
}
section() = default;
section(float num) : l(num), r(num) {
inf = FLT_MAX;
}
section(float l_, float r_) : l(l_), r(r_) {
inf = FLT_MAX;
}
section(string op1, string op, float num, string tp, bool judge) {
inf = FLT_MAX;
if (tp == "int") {
if (op == "<") {
switch (judge) {
case true://op1t
{
l = -inf;
r = num - 1;
break;
}
case false://op1f
{
l = num;
r = inf;
break;
}
}
} else if (op == "<=") {
switch (judge) {
case true://op1t
{
l = -inf;
r = num;
break;
}
case false://op1f
{
l = num + 1;
r = inf;
break;
}
}
}else if(op=="=="){
switch (judge)
{
case true://op1t
{
l=num;
r=num;
break;
}
case false://op1f
{
l=-inf;
r=inf;
break;
}
}
} else{
switch (judge)
{
case false://op1f
{
l=num;
r=num;
break;
}
case true://op1t
{
l=-inf;
r=inf;
break;
}
}
}
} else{
if (op == "<="||op=="<") {
switch (judge) {
case true://op1t
{
l = -inf;
r = num;
break;
}
case false://op1f
{
l = num;
r = inf;
break;
}
}
}else if(op=="=="){
switch (judge)
{
case true://op1t
{
l=num;
r=num;
break;
}
case false://op1f
{
l=-inf;
r=inf;
break;
}
}
} else{
switch (judge)
{
case false://op1f
{
l=num;
r=num;
break;
}
case true://op1t
{
l=-inf;
r=inf;
break;
}
}
}
}
}
bool isEmpty() {
return l > r;
}
friend section operator+(const section &s1, const section &s2) {
return section(add(s1.l, s2.l), add(s1.r, s2.r));
}
friend section operator-(const section &s1, const section &s2) {
return section(add(s1.l, -s2.r), add(s1.r, -s2.l));
}
friend section operator*(const section &s1, const section &s2) {
set<float> tmp;
tmp.insert(multi(s1.l, s2.l));
tmp.insert(multi(s1.l, s2.r));
tmp.insert(multi(s1.r, s2.l));
tmp.insert(multi(s1.r, s2.r));
return section(*(tmp.begin()), *(tmp.rbegin()));
}
friend section operator/(const section &s1, const section &s2) {
return s1 * section((float) 1 / s2.l, (float) 1 / s2.r);
}
friend section uni(const section &s1, const section &s2) {
return section(max(s1.l, s2.l), min(s1.r, s2.r));
}
friend section inter(const section &s1, const section &s2) {
return section(min(s1.l, s2.l), max(s1.r, s2.r));
}
section multiadd(float a, float b) {
float x1 = add(b, multi(a, l));
float x2 = add(b, multi(a, r));
return section(min(x1, x2), max(x1, x2));
}
};
#endif //PROJECT_SECTION_H