10#include "dlinear/util/RoundingModeGuard.hpp"
11#include "dlinear/util/exception.h"
15Interval::Interval(
const mpq_class &lb,
const mpq_class &ub) : lb_(lb), ub_(ub) {
16 DLINEAR_ASSERT(lb <= ub, "Interval: lb > ub
");
19std::pair<Interval, Interval> Interval::bisect(const mpq_class &p) const {
20 mpq_class midpoint{lb_ + p * (ub_ - lb_)};
21 return std::make_pair(Interval(lb_, midpoint), Interval(midpoint, ub_));
24Interval &Interval::operator+=(const Interval &o) {
29Interval &Interval::operator-=(const Interval &o) {
34Interval &Interval::operator*=(const Interval &o) {
35 const std::initializer_list<mpq_class> products{lb_ * o.lb_, lb_ * o.ub_, ub_ * o.lb_, ub_ * o.ub_};
36 const mpq_class lb{std::min(products)};
37 const mpq_class ub{std::max(products)};
42Interval &Interval::operator/=(const Interval &o) {
43 if (o.is_degenerated() && o.lb() == 0) DLINEAR_RUNTIME_ERROR("Division by zero
");
45 const std::initializer_list<mpq_class> quotients{lb_ / o.lb_, lb_ / o.ub_, ub_ / o.lb_, ub_ / o.ub_};
46 const mpq_class lb{std::min(quotients)};
47 const mpq_class ub{std::max(quotients)};
52Interval &Interval::operator+=(const mpq_class &o) {
57Interval &Interval::operator-=(const mpq_class &o) {
62Interval &Interval::operator*=(const mpq_class &o) {
63 const std::initializer_list<mpq_class> products{lb_ * o, lb_ * o, ub_ * o, ub_ * o};
64 const mpq_class lb{std::min(products)};
65 const mpq_class ub{std::max(products)};
70Interval &Interval::operator/=(const mpq_class &o) {
71 if (o == 0) DLINEAR_RUNTIME_ERROR("Division by zero
");
73 const std::initializer_list<mpq_class> quotients{lb_ / o, lb_ / o, ub_ / o, ub_ / o};
74 const mpq_class lb{std::min(quotients)};
75 const mpq_class ub{std::max(quotients)};
81std::ostream &Interval::PrintToStream(std::ostream &os, const mpq_class &ninfinity, const mpq_class &infinity) const {
82 if (is_empty()) return os << "[ empty ]
";
83 if (lb() <= ninfinity && ub() >= infinity) return os << "[ ENTIRE ]
";
86 if (lb() <= ninfinity) {
92 if (ub() >= infinity) {
100std::ostream &operator<<(std::ostream &os, const Interval &iv) {
101 if (iv.is_empty()) return os << "[ empty ]
";
102 return os << "[
" << iv.lb() << ",
" << iv.ub() << "]
";
105Interval Interval::FromString(const std::string &s) {
106 RoundingModeGuard guard(FE_UPWARD);
107 const double ub{stod(s)};
108 double lb = s[0] == '-' ? -stod(s.substr(1)) : -stod("-
" + s); // TODO: shouldn't this be -stod(s) or even -ub?
109 return Interval{lb, ub};
112} // namespace dlinear
Global namespace for the dlinear library.