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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
|
// Copyright (c) 1998-2005 Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org); 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; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Interval_arithmetic/include/CGAL/Interval_nt.h $
// $Id: Interval_nt.h 31497 2006-06-09 17:58:12Z spion $
//
//
// Author(s) : Sylvain Pion
#ifndef CGAL_INTERVAL_NT_H
#define CGAL_INTERVAL_NT_H
// This file contains the description of the following classes:
// - Interval_nt<false> It's a number type that needs the FPU rounding mode
// to be set to +inf. It is also typedef'd to
// Interval_nt_advanced for backward compatibility.
// - Interval_nt<true> Same but it does the rounding mode itself so you
// don't have to worry about it. But it's slower.
//
// Note: When rounding is towards +infinity, to make an operation rounded
// towards -infinity, it's enough to take the opposite of some of the operand,
// and the opposite of the result (see operator+, operator*,...).
#include <CGAL/basic.h>
#include <iostream>
#include <CGAL/FPU.h>
#include <CGAL/Uncertain.h>
#include <CGAL/Interval_nt_fwd.h>
#include <CGAL/number_utils_fwd.h>
#include <CGAL/functional_base.h> // Unary_function, Binary_function
CGAL_BEGIN_NAMESPACE
template <bool Protected = true>
class Interval_nt
{
typedef Interval_nt<Protected> IA;
typedef std::pair<double, double> Pair;
public:
typedef double value_type;
typedef Tag_false Has_gcd;
typedef Tag_true Has_division;
typedef Tag_true Has_sqrt;
// We may have to look back at these...
typedef Tag_false Has_exact_ring_operations;
typedef Tag_false Has_exact_division;
typedef Tag_false Has_exact_sqrt;
typedef std::exception unsafe_comparison;
typedef Checked_protect_FPU_rounding<Protected> Internal_protector;
typedef Protect_FPU_rounding<!Protected> Protector;
Interval_nt() {}
Interval_nt(int i)
: _inf(i), _sup(i) {}
Interval_nt(double d)
: _inf(d), _sup(d) {}
Interval_nt(double i, double s)
: _inf(i), _sup(s)
{
// VC++ should use instead : (i<=s) || !is_valid(i) || !is_valid(s)
// Or should I use is_valid() ? or is_valid_or_nan() ?
CGAL_assertion_msg(!(i>s),
" Variable used before being initialized (or CGAL bug)");
}
// This copy ctor, normally equivalent to the one created by the compiler,
// appears to fix a code generation problem with GCC 3.0.4...
// (see test/IA/gcc_3.0.bug.C).
Interval_nt(const Interval_nt & i)
: _inf(i._inf), _sup(i._sup) {}
Interval_nt(const Pair & p)
: _inf(p.first), _sup(p.second) {}
static unsigned number_of_failures()
{ return Uncertain<bool>::number_of_failures(); }
IA operator-() const { return IA (-sup(), -inf()); }
IA & operator+= (const IA &d) { return *this = *this + d; }
IA & operator-= (const IA &d) { return *this = *this - d; }
IA & operator*= (const IA &d) { return *this = *this * d; }
IA & operator/= (const IA &d) { return *this = *this / d; }
bool is_point() const
{
return sup() == inf();
}
bool is_same (const IA & d) const
{
return inf() == d.inf() && sup() == d.sup();
}
bool do_overlap (const IA & d) const
{
return !(d.inf() > sup() || d.sup() < inf());
}
const double & inf() const { return _inf; }
const double & sup() const { return _sup; }
std::pair<double, double> pair() const
{
return std::pair<double, double>(_inf, _sup);
}
static IA largest()
{
return IA(-CGALi::infinity, CGALi::infinity);
}
static IA smallest()
{
return IA(-CGAL_IA_MIN_DOUBLE, CGAL_IA_MIN_DOUBLE);
}
private:
// Pair inf_sup;
double _inf, _sup;
};
template <bool Protected>
inline
Uncertain<bool>
operator<(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{
if (a.sup() < b.inf()) return true;
if (a.inf() >= b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{ return b < a; }
template <bool Protected>
inline
Uncertain<bool>
operator<=(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{
if (a.sup() <= b.inf()) return true;
if (a.inf() > b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>=(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{ return b <= a; }
template <bool Protected>
inline
Uncertain<bool>
operator==(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{
if (b.inf() > a.sup() || b.sup() < a.inf()) return false;
if (b.inf() == a.sup() && b.sup() == a.inf()) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator!=(const Interval_nt<Protected> &a, const Interval_nt<Protected> &b)
{ return ! (a == b); }
// Mixed operators.
template <bool Protected>
inline
Uncertain<bool>
operator<(int a, const Interval_nt<Protected> &b)
{
if (a < b.inf()) return true;
if (a >= b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>(int a, const Interval_nt<Protected> &b)
{ return b < a; }
template <bool Protected>
inline
Uncertain<bool>
operator<=(int a, const Interval_nt<Protected> &b)
{
if (a <= b.inf()) return true;
if (a > b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>=(int a, const Interval_nt<Protected> &b)
{ return b <= a; }
template <bool Protected>
inline
Uncertain<bool>
operator==(int a, const Interval_nt<Protected> &b)
{
if (b.inf() > a || b.sup() < a) return false;
if (b.inf() == a && b.sup() == a) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator!=(int a, const Interval_nt<Protected> &b)
{ return ! (a == b); }
template <bool Protected>
inline
Uncertain<bool>
operator<(const Interval_nt<Protected> &a, int b)
{
if (a.sup() < b) return true;
if (a.inf() >= b) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>(const Interval_nt<Protected> &a, int b)
{ return b < a; }
template <bool Protected>
inline
Uncertain<bool>
operator<=(const Interval_nt<Protected> &a, int b)
{
if (a.sup() <= b) return true;
if (a.inf() > b) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator>=(const Interval_nt<Protected> &a, int b)
{ return b <= a; }
template <bool Protected>
inline
Uncertain<bool>
operator==(const Interval_nt<Protected> &a, int b)
{
if (b > a.sup() || b < a.inf()) return false;
if (b == a.sup() && b == a.inf()) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
operator!=(const Interval_nt<Protected> &a, int b)
{ return ! (a == b); }
template <bool Protected>
inline
double
to_double (const Interval_nt<Protected> & d)
{
return (d.sup() + d.inf()) * 0.5;
// This may overflow...
}
// Returns true if the interval is a unique representable double.
template <bool Protected>
inline
bool
fit_in_double (const Interval_nt<Protected> & d, double &r)
{
r = d.inf();
return d.is_point();
}
template <bool Protected>
inline
std::pair<double, double>
to_interval (const Interval_nt<Protected> & d)
{
return d.pair();
}
template <bool Protected>
inline
bool
is_valid (const Interval_nt<Protected> & d)
{
return CGAL::is_valid(d.inf()) &&
CGAL::is_valid(d.sup()) &&
d.inf() <= d.sup();
}
template <bool Protected>
inline
bool
is_finite (const Interval_nt<Protected> & d)
{
return CGAL::is_finite(d.inf()) && CGAL::is_finite(d.sup());
}
template <bool Protected>
inline
io_Operator
io_tag (const Interval_nt<Protected> &)
{
return io_Operator();
}
template <bool Protected>
std::ostream & operator<< (std::ostream &os, const Interval_nt<Protected> & I )
{
return os << "[" << I.inf() << ";" << I.sup() << "]";
}
template <bool Protected>
std::istream & operator>> (std::istream &is, Interval_nt<Protected> & I)
{
double d;
is >> d;
I = d;
return is;
}
typedef Interval_nt<false> Interval_nt_advanced; // for back-compatibility
template <bool Protected>
inline
Interval_nt<Protected>
operator+ (const Interval_nt<Protected> &a, const Interval_nt<Protected> & b)
{
typename Interval_nt<Protected>::Internal_protector P;
return Interval_nt<Protected> (-CGAL_IA_SUB(-a.inf(), b.inf()),
CGAL_IA_ADD(a.sup(), b.sup()));
}
template <bool Protected>
inline
Interval_nt<Protected>
operator+ (double a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)+b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator+ (const Interval_nt<Protected> & a, double b)
{
return a+Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator+ (int a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)+b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator+ (const Interval_nt<Protected> & a, int b)
{
return a+Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator- (const Interval_nt<Protected> &a, const Interval_nt<Protected> & b)
{
typename Interval_nt<Protected>::Internal_protector P;
return Interval_nt<Protected>(-CGAL_IA_SUB(b.sup(), a.inf()),
CGAL_IA_SUB(a.sup(), b.inf()));
}
template <bool Protected>
inline
Interval_nt<Protected>
operator- (double a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)-b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator- (const Interval_nt<Protected> & a, double b)
{
return a-Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator- (int a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)-b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator- (const Interval_nt<Protected> & a, int b)
{
return a-Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator* (const Interval_nt<Protected> &a, const Interval_nt<Protected> & b)
{
typedef Interval_nt<Protected> IA;
typename Interval_nt<Protected>::Internal_protector P;
if (a.inf() >= 0.0) // e>=0
{
// b>=0 [a.inf()*b.inf(); a.sup()*b.sup()]
// b<=0 [a.sup()*b.inf(); a.inf()*b.sup()]
// b~=0 [a.sup()*b.inf(); a.sup()*b.sup()]
double aa = a.inf(), bb = a.sup();
if (b.inf() < 0.0)
{
aa = bb;
if (b.sup() < 0.0)
bb = a.inf();
}
return IA(-CGAL_IA_MUL(aa, -b.inf()), CGAL_IA_MUL(bb, b.sup()));
}
else if (a.sup()<=0.0) // e<=0
{
// b>=0 [a.inf()*b.sup(); a.sup()*b.inf()]
// b<=0 [a.sup()*b.sup(); a.inf()*b.inf()]
// b~=0 [a.inf()*b.sup(); a.inf()*b.inf()]
double aa = a.sup(), bb = a.inf();
if (b.inf() < 0.0)
{
aa=bb;
if (b.sup() < 0.0)
bb=a.sup();
}
return IA(-CGAL_IA_MUL(bb, -b.sup()), CGAL_IA_MUL(aa, b.inf()));
}
else // 0 \in [inf();sup()]
{
if (b.inf()>=0.0) // d>=0
return IA(-CGAL_IA_MUL(-a.inf(), b.sup()),
CGAL_IA_MUL(a.sup(), b.sup()));
if (b.sup()<=0.0) // d<=0
return IA(-CGAL_IA_MUL(a.sup(), -b.inf()),
CGAL_IA_MUL(a.inf(), b.inf()));
// 0 \in d
double tmp1 = CGAL_IA_MUL(-a.inf(), b.sup());
double tmp2 = CGAL_IA_MUL( a.sup(), -b.inf());
double tmp3 = CGAL_IA_MUL( a.inf(), b.inf());
double tmp4 = CGAL_IA_MUL( a.sup(), b.sup());
return IA(-std::max(tmp1,tmp2), std::max(tmp3,tmp4));
}
}
template <bool Protected>
inline
Interval_nt<Protected>
operator* (double a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)*b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator* (const Interval_nt<Protected> & a, double b)
{
return a*Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator* (int a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)*b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator* (const Interval_nt<Protected> & a, int b)
{
return a*Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator/ (const Interval_nt<Protected> &a, const Interval_nt<Protected> & b)
{
typedef Interval_nt<Protected> IA;
typename Interval_nt<Protected>::Internal_protector P;
if (b.inf() > 0.0) // b>0
{
// e>=0 [a.inf()/b.sup(); a.sup()/b.inf()]
// e<=0 [a.inf()/b.inf(); a.sup()/b.sup()]
// e~=0 [a.inf()/b.inf(); a.sup()/b.inf()]
double aa = b.sup(), bb = b.inf();
if (a.inf() < 0.0)
{
aa = bb;
if (a.sup() < 0.0)
bb = b.sup();
};
return IA(-CGAL_IA_DIV(-a.inf(), aa), CGAL_IA_DIV(a.sup(), bb));
}
else if (b.sup()<0.0) // b<0
{
// e>=0 [a.sup()/b.sup(); a.inf()/b.inf()]
// e<=0 [a.sup()/b.inf(); a.inf()/b.sup()]
// e~=0 [a.sup()/b.sup(); a.inf()/b.sup()]
double aa = b.sup(), bb = b.inf();
if (a.inf() < 0.0)
{
bb = aa;
if (a.sup() < 0.0)
aa = b.inf();
};
return IA(-CGAL_IA_DIV(-a.sup(), aa), CGAL_IA_DIV(a.inf(), bb));
}
else // b~0
return IA::largest();
// We could do slightly better -> [0;infinity] when b.sup()==0,
// but is this worth ?
}
template <bool Protected>
inline
Interval_nt<Protected>
operator/ (double a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)/b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator/ (const Interval_nt<Protected> & a, double b)
{
return a/Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
operator/ (int a, const Interval_nt<Protected> & b)
{
return Interval_nt<Protected>(a)/b;
}
template <bool Protected>
inline
Interval_nt<Protected>
operator/ (const Interval_nt<Protected> & a, int b)
{
return a/Interval_nt<Protected>(b);
}
template <bool Protected>
inline
Interval_nt<Protected>
sqrt (const Interval_nt<Protected> & d)
{
typename Interval_nt<Protected>::Internal_protector P; // not optimal here.
// sqrt([+a,+b]) => [sqrt(+a);sqrt(+b)]
// sqrt([-a,+b]) => [0;sqrt(+b)] => assumes roundoff error.
// sqrt([-a,-b]) => [0;sqrt(-b)] => assumes user bug (unspecified result).
FPU_set_cw(CGAL_FE_DOWNWARD);
double i = (d.inf() > 0.0) ? CGAL_IA_SQRT(d.inf()) : 0.0;
FPU_set_cw(CGAL_FE_UPWARD);
return Interval_nt<Protected>(i, CGAL_IA_SQRT(d.sup()));
}
template <bool Protected>
inline
Interval_nt<Protected>
min (const Interval_nt<Protected> & d, const Interval_nt<Protected> & e)
{
return Interval_nt<Protected>(std::min(d.inf(), e.inf()),
std::min(d.sup(), e.sup()));
}
template <bool Protected>
inline
Interval_nt<Protected>
max (const Interval_nt<Protected> & d, const Interval_nt<Protected> & e)
{
return Interval_nt<Protected>(std::max(d.inf(), e.inf()),
std::max(d.sup(), e.sup()));
}
template <bool Protected>
inline
Interval_nt<Protected>
square (const Interval_nt<Protected> & d)
{
typename Interval_nt<Protected>::Internal_protector P;
if (d.inf()>=0.0)
return Interval_nt<Protected>(-CGAL_IA_MUL(d.inf(), -d.inf()),
CGAL_IA_MUL(d.sup(), d.sup()));
if (d.sup()<=0.0)
return Interval_nt<Protected>(-CGAL_IA_MUL(d.sup(), -d.sup()),
CGAL_IA_MUL(d.inf(), d.inf()));
return Interval_nt<Protected>(0.0, CGAL_IA_SQUARE(std::max(-d.inf(),
d.sup())));
}
template <bool Protected>
inline
Interval_nt<Protected>
abs (const Interval_nt<Protected> & d)
{
if (d.inf() >= 0.0) return d;
if (d.sup() <= 0.0) return -d;
return Interval_nt<Protected>(0.0, std::max(-d.inf(), d.sup()));
}
template <bool Protected>
inline
Uncertain<Sign>
sign (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return POSITIVE;
if (d.sup() < 0.0) return NEGATIVE;
if (d.inf() == d.sup()) return ZERO;
return Uncertain<Sign>::indeterminate();
}
template <bool Protected>
inline
Uncertain<Comparison_result>
compare (const Interval_nt<Protected> & d, const Interval_nt<Protected> & e)
{
if (d.inf() > e.sup()) return LARGER;
if (e.inf() > d.sup()) return SMALLER;
if (e.inf() == d.sup() && d.inf() == e.sup()) return EQUAL;
return Uncertain<Comparison_result>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_zero (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return false;
if (d.sup() < 0.0) return false;
if (d.inf() == d.sup()) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_one (const Interval_nt<Protected> & d)
{
if (d.inf() > 1) return false;
if (d.sup() < 1) return false;
if (d.inf() == d.sup()) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_positive (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return true;
if (d.sup() <= 0.0) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_negative (const Interval_nt<Protected> & d)
{
if (d.inf() >= 0.0) return false;
if (d.sup() < 0.0) return true;
return Uncertain<bool>::indeterminate();
}
inline
std::pair<double, double>
to_interval (const long & l)
{
#ifndef __BORLANDC__ // The stupid Borland compiler generates warnings...
if (sizeof(double) > sizeof(long)) {
// On 64bit platforms, a long doesn't fit exactly in a double.
// Well, a perfect fix would be to use std::numeric_limits<>, but...
Protect_FPU_rounding<true> P(CGAL_FE_TONEAREST);
Interval_nt<false> approx ((double) l);
FPU_set_cw(CGAL_FE_UPWARD);
approx += Interval_nt<false>::smallest();
return approx.pair();
}
else
#endif
return std::pair<double,double>(l,l);
}
// We also specialize some corresponding functors returning Uncertain<>.
template < bool b >
struct Equal_to < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x == y; }
};
template < bool b >
struct Not_equal_to < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x != y; }
};
template < bool b >
struct Greater < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x > y; }
};
template < bool b >
struct Less < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x < y; }
};
template < bool b >
struct Greater_equal < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x >= y; }
};
template < bool b >
struct Less_equal < Interval_nt<b>, Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x <= y; }
};
template < bool b >
struct Is_zero < Interval_nt<b> >
: public Unary_function< Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x) const
{ return CGAL_NTS is_zero(x); }
};
template < bool b >
struct Is_one < Interval_nt<b> >
: public Unary_function< Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x) const
{ return CGAL_NTS is_one(x); }
};
template < bool b >
struct Is_negative < Interval_nt<b> >
: public Unary_function< Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x) const
{ return CGAL_NTS is_negative(x); }
};
template < bool b >
struct Is_positive < Interval_nt<b> >
: public Unary_function< Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x) const
{ return CGAL_NTS is_positive(x); }
};
template < bool b >
struct Sgn < Interval_nt<b> >
: public Unary_function< Interval_nt<b>, Uncertain<Sign> >
{
Uncertain<Sign> operator()( const Interval_nt<b>& x) const
{ return CGAL_NTS sign(x); }
};
template < bool b >
struct Compare < Interval_nt<b> >
: public Binary_function< Interval_nt<b>, Interval_nt<b>,
Uncertain<Comparison_result> >
{
Uncertain<Comparison_result>
operator()( const Interval_nt<b>& x, const Interval_nt<b>& y) const
{ return CGAL_NTS compare(x, y); }
};
CGAL_END_NAMESPACE
#endif // CGAL_INTERVAL_NT_H
|