Generated on for Gecode by doxygen 1.17.0
val.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
5 *
6 * Contributing authors:
7 * Christian Schulte <schulte@gecode.dev>
8 *
9 * Copyright:
10 * Christian Schulte, 2012
11 * Vincent Barichard, 2012
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining
18 * a copy of this software and associated documentation files (the
19 * "Software"), to deal in the Software without restriction, including
20 * without limitation the rights to use, copy, modify, merge, publish,
21 * distribute, sublicense, and/or sell copies of the Software, and to
22 * permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be
26 * included in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38namespace Gecode {
39
40 /*
41 * Floating point value: member functions
42 *
43 */
44 forceinline
46 forceinline
47 FloatVal::FloatVal(const FloatNum& n) : x(n) {}
48 forceinline
49 FloatVal::FloatVal(const FloatNum& l, const FloatNum& u) : x(l,u) {}
50 forceinline
52 forceinline
53 FloatVal::FloatVal(const FloatVal& v) : x(v.x) {}
54
55 forceinline FloatVal&
57 x = n; return *this;
58 }
59 forceinline FloatVal&
61 x = v.x; return *this;
62 }
63
64 forceinline void
65 FloatVal::assign(FloatNum const &l, FloatNum const &u) {
66 x.assign(l,u);
67 }
68
69 forceinline FloatNum
70 FloatVal::min(void) const {
71 return x.lower();
72 }
73 forceinline FloatNum
74 FloatVal::max(void) const {
75 return x.upper();
76 }
77 forceinline FloatNum
78 FloatVal::size(void) const {
79 return gecode_boost::numeric::width(x);
80 }
81 forceinline FloatNum
82 FloatVal::med(void) const {
83 const FloatNum l = x.lower();
84 const FloatNum u = x.upper();
85 const FloatNum inf = std::numeric_limits<FloatNum>::infinity();
86 const FloatNum max = std::numeric_limits<FloatNum>::max();
87 if (l == -inf) {
88 if (u == -inf)
89 return l;
90 // Preserve the extended endpoint for semi-infinite intervals.
91 return (u == inf) ? 0.0 : l;
92 }
93 if (u == inf)
94 return u;
95
97 const FloatNum h = max / 2.0;
98 if ((u > h) || (l < -h))
99 return std::ldexp(r.median(l / 2.0,u / 2.0),1);
100 return r.median(l,u);
101 }
102
103 forceinline bool
104 FloatVal::tight(void) const {
105 return (gecode_boost::numeric::singleton(x) ||
106 (nextafter(x.lower(),x.upper()) == x.upper()));
107 }
108 forceinline bool
110 return gecode_boost::numeric::singleton(x);
111 }
112 forceinline bool
114 return gecode_boost::numeric::in(n,x);
115 }
116 forceinline bool
117 FloatVal::zero_in(void) const {
118 return gecode_boost::numeric::zero_in(x);
119 }
120
121 forceinline FloatVal
123 FloatVal h(x,y); return h;
124 }
125 forceinline FloatVal
127 FloatVal p(gecode_boost::numeric::interval_lib::pi_half<FloatValImpType>());
128 return p;
129 }
130 forceinline FloatVal
132 FloatVal p(gecode_boost::numeric::interval_lib::pi<FloatValImpType>());
133 return p;
134 }
135 forceinline FloatVal
137 FloatVal p(gecode_boost::numeric::interval_lib::pi_twice<FloatValImpType>());
138 return p;
139 }
140
141 forceinline FloatVal&
143 x += n; return *this;
144 }
145 forceinline FloatVal&
147 x -= n; return *this;
148 }
149 forceinline FloatVal&
151 x *= n; return *this;
152 }
153 forceinline FloatVal&
155 x /= n; return *this;
156 }
157
158 forceinline FloatVal&
160 x += v.x; return *this;
161 }
162 forceinline FloatVal&
164 x -= v.x; return *this;
165 }
166 forceinline FloatVal&
168 x *= v.x; return *this;
169 }
170 forceinline FloatVal&
172 x /= v.x; return *this;
173 }
174
175 /*
176 * Operators and functions on float values
177 *
178 */
179
180 forceinline FloatVal
182 return FloatVal(+x.x);
183 }
184 forceinline FloatVal
186 FloatNum mmi = (x.min() == 0.0) ? 0.0 : -x.min();
187 FloatNum mma = (x.max() == 0.0) ? 0.0 :-x.max();
188 return FloatVal(mma,mmi);
189 }
190 forceinline FloatVal
191 operator +(const FloatVal& x, const FloatVal& y) {
192 return FloatVal(x.x+y.x);
193 }
194 forceinline FloatVal
195 operator +(const FloatVal& x, const FloatNum& y) {
196 return FloatVal(x.x+y);
197 }
198 forceinline FloatVal
199 operator +(const FloatNum& x, const FloatVal& y) {
200 return FloatVal(x+y.x);
201 }
202
203 forceinline FloatVal
204 operator -(const FloatVal& x, const FloatVal& y) {
205 return FloatVal(x.x-y.x);
206 }
207 forceinline FloatVal
208 operator -(const FloatVal& x, const FloatNum& y) {
209 return FloatVal(x.x-y);
210 }
211 forceinline FloatVal
212 operator -(const FloatNum& x, const FloatVal& y) {
213 return FloatVal(x-y.x);
214 }
215
216 forceinline FloatVal
217 operator *(const FloatVal& x, const FloatVal& y) {
218 return FloatVal(x.x*y.x);
219 }
220 forceinline FloatVal
221 operator *(const FloatVal& x, const FloatNum& y) {
222 return FloatVal(x.x*y);
223 }
224 forceinline FloatVal
225 operator *(const FloatNum& x, const FloatVal& y) {
226 return FloatVal(x*y.x);
227 }
228
229 forceinline FloatVal
230 operator /(const FloatVal& x, const FloatVal& y) {
231 return FloatVal(x.x/y.x);
232 }
233 forceinline FloatVal
234 operator /(const FloatVal& x, const FloatNum& y) {
235 return FloatVal(x.x/y);
236 }
237 forceinline FloatVal
238 operator /(const FloatNum& x, const FloatVal& y) {
239 return FloatVal(x/y.x);
240 }
241
242 inline bool
243 operator <(const FloatVal& x, const FloatVal& y) {
244 try {
245 return x.x < y.x;
246 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
247 return false;
248 }
249 }
250 inline bool
251 operator <(const FloatVal& x, const FloatNum& y) {
252 try {
253 return x.x < y;
254 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
255 return false;
256 }
257 }
258
259 inline bool
260 operator <=(const FloatVal& x, const FloatVal& y) {
261 try {
262 return x.x <= y.x;
263 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
264 return false;
265 }
266 }
267 inline bool
268 operator <=(const FloatVal& x, const FloatNum& y) {
269 try {
270 return x.x <= y;
271 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
272 return false;
273 }
274 }
275
276 inline bool
277 operator >(const FloatVal& x, const FloatVal& y) {
278 try {
279 return x.x > y.x;
280 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
281 return false;
282 }
283 }
284 inline bool
285 operator >(const FloatVal& x, const FloatNum& y) {
286 try {
287 return x.x > y;
288 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
289 return false;
290 }
291 }
292
293 inline bool
294 operator >=(const FloatVal& x, const FloatVal& y) {
295 try {
296 return x.x >= y.x;
297 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
298 return false;
299 }
300 }
301 inline bool
302 operator >=(const FloatVal& x, const FloatNum& y) {
303 try {
304 return x.x >= y;
305 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
306 return false;
307 }
308 }
309
310 inline bool
311 operator ==(const FloatVal& x, const FloatVal& y) {
312 try {
313 return x.x == y.x;
314 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
315 return false;
316 }
317 }
318 inline bool
319 operator ==(const FloatVal& x, const FloatNum& y) {
320 if (!gecode_boost::numeric::interval_lib::checking_strict<FloatNum>
321 ::is_empty(x.x.lower(), x.x.upper())) {
322 if ((x.x.lower() == y) && (x.x.upper() == y))
323 return true;
324 }
325 if (((x.x.lower() == y) &&
326 (nextafter(x.x.lower(),x.x.upper()) == x.x.upper())) ||
327 ((x.x.upper() == y) &&
328 (nextafter(x.x.upper(),x.x.lower()) == x.x.lower())))
329 return true;
330 return false;
331 }
332
333 inline bool
334 operator !=(const FloatVal& x, const FloatVal& y) {
335 try {
336 return x.x != y.x;
337 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
338 return false;
339 }
340 }
341 inline bool
342 operator !=(const FloatVal& x, const FloatNum& y) {
343 try {
344 return x.x != y;
345 } catch (gecode_boost::numeric::interval_lib::comparison_error&) {
346 return false;
347 }
348 }
349
350 forceinline bool
351 operator <(const FloatNum& x, const FloatVal& y) {
352 return y > x;
353 }
354 forceinline bool
355 operator <=(const FloatNum& x, const FloatVal& y) {
356 return y >= x;
357 }
358 forceinline bool
359 operator >(const FloatNum& x, const FloatVal& y) {
360 return y < x;
361 }
362 forceinline bool
363 operator >=(const FloatNum& x, const FloatVal& y) {
364 return y <= x;
365 }
366 forceinline bool
367 operator ==(const FloatNum& x, const FloatVal& y) {
368 return y == x;
369 }
370 forceinline bool
371 operator !=(const FloatNum& x, const FloatVal& y) {
372 return y != x;
373 }
374
375 template<class Char, class Traits>
376 std::basic_ostream<Char,Traits>&
377 operator <<(std::basic_ostream<Char,Traits>& os, const FloatVal& x) {
378 return os << '[' << x.min() << ".." << x.max() << ']';
379 }
380
381 forceinline FloatVal
382 abs(const FloatVal& x) {
383 return FloatVal(abs(x.x));
384 }
385 forceinline FloatVal
386 sqrt(const FloatVal& x) {
387 return FloatVal(sqrt(x.x));
388 }
389 forceinline FloatVal
390 sqr(const FloatVal& x) {
391 return FloatVal(square(x.x));
392 }
393 forceinline FloatVal
394 pow(const FloatVal& x, int n) {
395 return FloatVal(pow(x.x,n));
396 }
397 forceinline FloatVal
398 nroot(const FloatVal& x, int n) {
399 return FloatVal(nth_root(x.x,n));
400 }
401
402 forceinline FloatVal
403 max(const FloatVal& x, const FloatVal& y) {
404 return FloatVal(max(x.x,y.x));
405 }
406 forceinline FloatVal
407 max(const FloatVal& x, const FloatNum& y) {
408 return FloatVal(max(x.x,y));
409 }
410 forceinline FloatVal
411 max(const FloatNum& x, const FloatVal& y) {
412 return FloatVal(max(x,y.x));
413 }
414 forceinline FloatVal
415 min(const FloatVal& x, const FloatVal& y) {
416 return FloatVal(min(x.x,y.x));
417 }
418 forceinline FloatVal
419 min(const FloatVal& x, const FloatNum& y) {
420 return FloatVal(min(x.x,y));
421 }
422 forceinline FloatVal
423 min(const FloatNum& x, const FloatVal& y) {
424 return FloatVal(min(x,y.x));
425 }
426
427#ifdef GECODE_HAS_MPFR
428
429 forceinline FloatVal
430 exp(const FloatVal& x) {
431 return FloatVal(exp(x.x));
432 }
433 forceinline FloatVal
434 log(const FloatVal& x) {
435 return FloatVal(log(x.x));
436 }
437
438 forceinline FloatVal
439 fmod(const FloatVal& x, const FloatVal& y) {
440 return FloatVal(fmod(x.x,y.x));
441 }
442 forceinline FloatVal
443 fmod(const FloatVal& x, const FloatNum& y) {
444 return FloatVal(fmod(x.x,y));
445 }
446 forceinline FloatVal
447 fmod(const FloatNum& x, const FloatVal& y) {
448 return FloatVal(fmod(x,y.x));
449 }
450
451 forceinline FloatVal
452 sin(const FloatVal& x) {
453 return FloatVal(sin(x.x));
454 }
455 forceinline FloatVal
456 cos(const FloatVal& x) {
457 return FloatVal(cos(x.x));
458 }
459 forceinline FloatVal
460 tan(const FloatVal& x) {
461 return FloatVal(tan(x.x));
462 }
463 forceinline FloatVal
464 asin(const FloatVal& x) {
465 return FloatVal(asin(x.x));
466 }
467 forceinline FloatVal
468 acos(const FloatVal& x) {
469 return FloatVal(acos(x.x));
470 }
471 forceinline FloatVal
472 atan(const FloatVal& x) {
473 return FloatVal(atan(x.x));
474 }
475
476 forceinline FloatVal
477 sinh(const FloatVal& x) {
478 return FloatVal(sinh(x.x));
479 }
480 forceinline FloatVal
481 cosh(const FloatVal& x) {
482 return FloatVal(cosh(x.x));
483 }
484 forceinline FloatVal
485 tanh(const FloatVal& x) {
486 return FloatVal(tanh(x.x));
487 }
488 forceinline FloatVal
489 asinh(const FloatVal& x) {
490 return FloatVal(asinh(x.x));
491 }
492 forceinline FloatVal
493 acosh(const FloatVal& x) {
494 return FloatVal(acosh(x.x));
495 }
496 forceinline FloatVal
497 atanh(const FloatVal& x) {
498 return FloatVal(atanh(x.x));
499 }
500
501#endif
502}
503
504namespace Gecode { namespace Float {
505
506 forceinline bool
507 subset(const FloatVal& x, const FloatVal& y) {
508 return subset(x.x,y.x);
509 }
510 forceinline bool
511 proper_subset(const FloatVal& x, const FloatVal& y) {
512 return proper_subset(x.x,y.x);
513 }
514 forceinline bool
515 overlap(const FloatVal& x, const FloatVal& y) {
516 return overlap(x.x,y.x);
517 }
518
519 forceinline FloatVal
520 intersect(const FloatVal& x, const FloatVal& y) {
521 return FloatVal(intersect(x.x,y.x));
522 }
523 forceinline FloatVal
524 hull(const FloatVal& x, const FloatVal& y) {
525 return FloatVal(hull(x.x,y.x));
526 }
527 forceinline FloatVal
528 hull(const FloatVal& x, const FloatNum& y) {
529 return FloatVal(hull(x.x,y));
530 }
531 forceinline FloatVal
532 hull(const FloatNum& x, const FloatVal& y) {
533 return FloatVal(hull(x,y.x));
534 }
535 forceinline FloatVal
536 hull(const FloatNum& x, const FloatNum& y) {
537 return FloatVal(x,y);
538 }
539
540}}
541
542// STATISTICS: float-var
543
Float value type.
Definition float.hh:334
bool singleton(void) const
Test whether float is a singleton.
Definition val.hpp:109
friend FloatVal Float::hull(const FloatVal &x, const FloatVal &y)
FloatVal & operator/=(const FloatNum &n)
Divide by n.
Definition val.hpp:154
static FloatVal pi(void)
Return lower bound of .
Definition val.hpp:131
static FloatVal pi_twice(void)
Return .
Definition val.hpp:136
FloatValImpType x
Implementation of float value.
Definition float.hh:425
bool zero_in(void) const
Test whether zero is included.
Definition val.hpp:117
FloatVal & operator-=(const FloatNum &n)
Subtract by n.
Definition val.hpp:146
bool in(FloatNum n) const
Test whether n is included.
Definition val.hpp:113
FloatVal & operator*=(const FloatNum &n)
Multiply by n.
Definition val.hpp:150
FloatNum max(void) const
Return upper bound.
Definition val.hpp:74
void assign(FloatNum const &l, FloatNum const &u)
Assign lower bound l and upper bound u.
Definition val.hpp:65
FloatVal & operator+=(const FloatNum &n)
Increment by n.
Definition val.hpp:142
FloatNum med(void) const
Return median of float value.
Definition val.hpp:82
FloatVal & operator=(const FloatNum &n)
Assignment operator.
Definition val.hpp:56
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:403
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:415
bool tight(void) const
Test whether float is tight.
Definition val.hpp:104
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
static FloatVal pi_half(void)
Return .
Definition val.hpp:126
FloatVal(const FloatValImpType &i)
Initialize from implementation i.
Definition val.hpp:51
gecode_boost::numeric::interval< FloatNum, gecode_boost::numeric::interval_lib::policies< R, P > > FloatValImpType
Implementation type for float value.
Definition float.hh:423
FloatVal(void)
Default constructor.
Definition val.hpp:45
FloatNum min(void) const
Return lower bound.
Definition val.hpp:70
Floating point rounding policy.
Definition float.hh:154
FloatNum median(FloatNum x, FloatNum y)
Return median of x and y (domain: ).
Definition rounding.hpp:44
double FloatNum
Floating point number base type.
Definition float.hh:106
bool subset(const FloatVal &x, const FloatVal &y)
Definition val.hpp:507
bool proper_subset(const FloatVal &x, const FloatVal &y)
Definition val.hpp:511
FloatVal hull(const FloatVal &x, const FloatVal &y)
Definition val.hpp:524
bool overlap(const FloatVal &x, const FloatVal &y)
Definition val.hpp:515
FloatVal intersect(const FloatVal &x, const FloatVal &y)
Definition val.hpp:520
Gecode toplevel namespace
FloatVal operator/(const FloatVal &x, const FloatVal &y)
Definition val.hpp:230
FloatVal operator-(const FloatVal &x)
Definition val.hpp:185
FloatVal asinh(const FloatVal &x)
Definition val.hpp:489
bool operator<=(const FloatVal &x, const FloatVal &y)
Definition val.hpp:260
void sin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
FloatVal operator+(const FloatVal &x)
Definition val.hpp:181
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
void cos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool operator<(const FloatVal &x, const FloatVal &y)
Definition val.hpp:243
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void acos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool operator>(const FloatVal &x, const FloatVal &y)
Definition val.hpp:277
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool operator>=(const FloatVal &x, const FloatVal &y)
Definition val.hpp:294
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void atan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
FloatVal fmod(const FloatVal &x, const FloatVal &y)
Definition val.hpp:439
FloatVal sinh(const FloatVal &x)
Definition val.hpp:477
FloatVal acosh(const FloatVal &x)
Definition val.hpp:493
FloatVal tanh(const FloatVal &x)
Definition val.hpp:485
void asin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .
FloatVal atanh(const FloatVal &x)
Definition val.hpp:497
FloatVal operator*(const FloatVal &x, const FloatVal &y)
Definition val.hpp:217
FloatVal cosh(const FloatVal &x)
Definition val.hpp:481
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .
bool operator==(const FloatVal &x, const FloatVal &y)
Definition val.hpp:311
bool operator!=(const FloatVal &x, const FloatVal &y)
Definition val.hpp:334
void tan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .