-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.cpp
223 lines (184 loc) · 4.64 KB
/
interval.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
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
#include<cstdio>
#include<cassert>
#include<iostream>
#include <algorithm>
using namespace std;
class Interval {
public:
float left;
float right;
Interval();
//Interval(float left);
Interval(float left, float right);
friend const Interval operator +(const Interval& x, const Interval& y);
friend const Interval operator -(const Interval& x, const Interval& y);
friend const Interval operator *(const Interval& x, const Interval& y);
friend const Interval operator *(const float& x, const Interval& y);
friend const Interval pow(Interval x, int i);
const Interval operator -();
friend bool operator <(const Interval& x, const Interval& y);
friend bool operator >(const Interval& x, const Interval& y);
friend const Interval inter(const Interval& x, const Interval& y);
friend const Interval uni(const Interval& x, const Interval& y);
Interval& operator =(const Interval& x);
friend bool operator <<(const float& x, const Interval& y);
float mid();
float len();
Interval lefthalf();
Interval righthalf();
friend ostream& operator<<(ostream& o, const Interval& x);
};
Interval::Interval(float x, float y) :left(x), right(y) { }
//Interval::Interval(float x) : left(x), right(x) { }
Interval::Interval() { }
const Interval operator +(const Interval& x, const Interval& y)
{
Interval ans;
ans.left = x.left + y.left;
ans.right = x.right + y.right;
return ans;
}
const Interval operator -(const Interval& x, const Interval& y)
{
Interval ans;
ans.left = x.left - y.right;
ans.right = x.right - y.left;
return ans;
}
const Interval operator *(const Interval& x, const Interval& y)
{
Interval ans;
ans.left = min(min(min(x.left * y.left, x.left * y.right), x.right * y.left), x.right * y.right);
ans.right= max(max(max(x.left * y.left, x.left * y.right), x.right * y.left), x.right * y.right);
return ans;
}
const Interval operator *(const float& x, const Interval& y)
{
Interval ans;
if (x > 0) {
ans.left = x * y.left;
ans.right = x * y.right;
}
else {
ans.left = x * y.right;
ans.right = x * y.left;
}
return ans;
}
const Interval pow(Interval x, int i) {
Interval res;
if (i == 0) {
res.left = 1;
res.right = 1;
}
else if (i % 2 != 0) {
res.left = pow(x.left, i);
res.right = pow(x.right, i);
}
else {
if (0 << x) {
res.left = 0;
res.right = max(pow(x.left, i), pow(x.right, i));
}
else {
res.left = min(pow(x.left, i), pow(x.right, i));
res.right = max(pow(x.left, i), pow(x.right, i));
}
}
return res;
}
const Interval Interval::operator -()
{
Interval x;
float temp = left;
x.left = -right;
x.right = -temp;
return x;
}
bool operator <(const Interval& x, const Interval& y) {
if (x.right <= y.left) {
return true;
}
else {
return false;
}
}
bool operator >(const Interval& x, const Interval& y) {
if (x.left <= y.right) {
return true;
}
else {
return false;
}
}
const Interval inter(const Interval& x, const Interval& y) {
Interval res;
if ((x.left > y.right) || (y.left > x.right)) {
res.left = 1;
res.right = 0;
}
else {
res.left = max(x.left, y.left);
res.right = min(x.right, y.right);
}
return res;
}
const Interval uni(const Interval& x, const Interval& y) {
Interval res;
res.left = min(x.left, y.left);
res.right = max(x.right, y.right);
return res;
}
Interval& Interval::operator =(const Interval& x)
{
left = x.left;
right = x.right;
return *this;
}
bool operator << (const float& x, const Interval& I)
{
if (I.left <= x && I.right >= x)
return true;
else
return false;
}
float Interval::mid() {
return (left + right) / 2;
}
float Interval::len(){
float res = right - left;
return res;
}
Interval Interval::lefthalf() {
return Interval(left, mid());
}
Interval Interval::righthalf() {
return Interval(mid(), right);
}
ostream& operator<<(ostream& o, const Interval& x)
{
o << "[" << x.left << "," << x.right << "]";
return o;
}
void testinterval()
{
Interval x(-3.9, 4.0), y(2.0, 16.0);
float v = 1.5;
cout << "len(x)=" << x.len() << endl;
cout << "x+y=" << x + y << endl;
cout << "x-y=" << x - y << endl;
cout << "x*y=" << x * y << endl;
cout << "2 << x=" << (2 << x) << endl;
cout << "-x" << -x << endl;
cout << "x^4" << pow(x, 4) << endl;
cout << "y^3" << pow(y, 3) << endl;
cout << "v*x=" << v * x << endl;
cout << "inter(x,y)=" << inter(x, y) << endl;
cout << "uni(x,y)=" << uni(x, y) << endl;
cout << "mid(x)=" << x.mid() << endl;
cout << "len(x)=" << x.len() << endl;
}
//int main() {
// testinterval();
// return 0;
//}