-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaa_interval.cpp
More file actions
263 lines (227 loc) · 6.63 KB
/
aa_interval.cpp
File metadata and controls
263 lines (227 loc) · 6.63 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
/*
* aa_interval.c -- Implementation of the interval class
* Copyright (c) 2003 EPFL (Ecole Polytechnique Federale de Lausanne)
* Copyright (c) 2004 LIRIS (University Claude Bernard Lyon 1)
* Copyright (C) 2009 LUH (Leibniz Universitaet Hannover)
*
* This file is part of aaflib.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libaa; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "aa_interval.h"
#include "aa_rounding.h"
#include <cmath>
#include <cstdio>
#include <iostream>
#define PI (4 * atan(1.0))
// AAInterval(), modlo(), modhi(), modlohi() are not
// inline functions as they arent used by the lib
// but they are useful for applications
/************************************************************
* Method: AAInterval
* Author & Date: ??? - ???
* Description:
* Create a null interval
* Useful if we only declare a variable
*
* Input : -
* Output : -
************************************************************/
AAInterval::AAInterval()
: lo(0)
, hi(0)
{
}
/************************************************************
* Method: Operator =
* Author & Date: ??? - ???
* Description:
* Affectation operator
*
* Input : const AAInterval : input Interval
* Output : -
************************************************************/
AAInterval& AAInterval::operator=(const AAInterval& I)
{
if (&I != this) {
lo = I.lo;
hi = I.hi;
}
return *this;
}
/************************************************************
* Method: modlo
* Author & Date: ??? - ???
* Description:
* Modify the lower bound of an Interval
*
* Input : double : value to modify lo
* Output : -
************************************************************/
void AAInterval::modlo(const double low)
{
lo = low;
}
/************************************************************
* Method: modhi
* Author & Date: ??? - ???
* Description:
* Modify the higher bound of an Interval
*
* Input : double : value to modify hi
* Output : -
************************************************************/
void AAInterval::modhi(const double high)
{
hi = high;
}
/************************************************************
* Method: modboth
* Author & Date: ??? - ???
* Description:
* Modify the two bounds of an Interval
*
* Input : double : value to modify lo
* double : value to modify hi
* Output : -
************************************************************/
void AAInterval::modlohi(const double low, const double high)
{
lo = low;
hi = high;
}
/************************************************************
* Method: mid
* Author & Date: ??? - ???
* Description:
* Calculate the midpoint of an Interval
*
* Input : double : midpoint
* Output : -
************************************************************/
double AAInterval::mid() const
{
double t0, t1;
aa_rnd_t mode = aa_fegetround();
aa_fesetround(AA_DOWNWARD);
t0 = lo * 0.5;
aa_fesetround(AA_UPWARD);
t1 = hi * 0.5;
aa_fesetround(mode);
return t0 + t1;
}
/************************************************************
* Method: radius
* Author & Date: ??? - ???
* Description:
* Calculate the radius of an Interval
*
* Input : double : radius
* Output : -
************************************************************/
double AAInterval::radius() const
{
double m = mid();
double t0, t1;
aa_rnd_t mode = aa_fegetround();
aa_fesetround(AA_DOWNWARD);
t0 = m - lo;
aa_fesetround(AA_UPWARD);
t1 = hi - m;
aa_fesetround(mode);
return (t0 >= t1 ? t0 : t1);
}
/************************************************************
* Method: operator >>
* Author & Date: ??? - ???
* Description:
* Istream input of an Interval
* Accepts the forms x or [x] or [x,y]
*
* Input : std::istream & : input stream
* Output : AAInterval & : interval
************************************************************/
std::istream& operator>>(std::istream& s, AAInterval& I)
{
double lo = 0, hi = 0;
char c = 0;
s >> c;
if (c == '[') {
s >> lo >> c;
if (c == ',')
s >> hi >> c;
//if (c != ']') s.clear(ios_base::badbit);
} else {
//s.putback(c);
s >> lo;
}
if (s)
I = AAInterval(lo, hi);
return s;
}
/************************************************************
* Method: intvprint
* Author & Date: ??? - ???
* Description:
* Print an Interval to stdout
*
* Input : -
* Output : -
************************************************************/
void AAInterval::intvprint() const
{
printf("[%f,%f]\n", lo, hi);
}
/************************************************************
* Method: operator <<
* Author & Date: ??? - ???
* Description:
* Ostream output of an Interval
*
* Input : std::ostream & : output stream
* AAInterval & : interval
* Output : -
************************************************************/
std::ostream& operator<<(std::ostream& s, const AAInterval& I)
{
// s.setf(0, ios_base::floatfield);
// cause we don't want to display in scientific format
s << "[" << I.getlo() << "," << I.gethi() << "]\n";
return s;
}
/************************************************************
* Method: mintrigo
* Author & Date: ??? - ???
* Description:
* Caculate the minimal 2PI periodic interval
* of an interval, e.g. : [5*PI, 6*PI] -> [PI, 2PI]
* In fact it is the closer interval to 0
*
* Input : const AAInterval & : interval
* Output : AAInterval : interval
************************************************************/
AAInterval mintrigo(const AAInterval& I)
{
// This function is no more needed
// for our new algorithm of the sine of an AAF
double a = I.getlo();
double b = I.gethi();
double t1, t2;
t1 = floor(a / (2 * PI));
t2 = b - a;
a = a - (t1 * 2 * PI);
b = a + t2;
return AAInterval(a, b);
}