BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
reallimits.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/utils/reallimits.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "mvvm/utils/reallimits.h"
17 #include <limits>
18 
19 namespace {
20 const double lmin = std::numeric_limits<double>::lowest();
21 const double lmax = std::numeric_limits<double>::max();
22 const double poszero = std::numeric_limits<double>::min();
23 } // namespace
24 
25 using namespace ModelView;
26 
28  : m_has_lower_limit(false), m_has_upper_limit(false), m_lower_limit(lmin), m_upper_limit(lmax)
29 {
30 }
31 
32 RealLimits::RealLimits(bool has_lower_limit, bool has_upper_limit, double lower_limit,
33  double upper_limit)
34  : m_has_lower_limit(has_lower_limit)
35  , m_has_upper_limit(has_upper_limit)
36  , m_lower_limit(lower_limit)
37  , m_upper_limit(upper_limit)
38 {
39 }
40 
42 {
43  return RealLimits(true, false, bound_value, lmax);
44 }
45 
47 {
48  return lowerLimited(poszero);
49 }
50 
52 {
53  return lowerLimited(0.);
54 }
55 
57 {
58  return RealLimits(false, true, lmin, bound_value);
59 }
60 
61 RealLimits RealLimits::limited(double left_bound_value, double right_bound_value)
62 {
63  return RealLimits(true, true, left_bound_value, right_bound_value);
64 }
65 
67 {
68  return RealLimits();
69 }
70 
72 {
73  return m_has_lower_limit;
74 }
75 
76 double RealLimits::lowerLimit() const
77 {
78  return m_lower_limit;
79 }
80 
82 {
83  return m_has_upper_limit;
84 }
85 
86 double RealLimits::upperLimit() const
87 {
88  return m_upper_limit;
89 }
90 
92 {
94 }
95 
96 bool RealLimits::isInRange(double value) const
97 {
98  if (hasLowerLimit() && value < m_lower_limit)
99  return false;
100  if (hasUpperLimit() && value >= m_upper_limit)
101  return false;
102  return true;
103 }
104 
105 bool RealLimits::operator==(const RealLimits& other) const
106 {
107  // Intenional 'unsafe' double comparison to have RealLimits::positive and
108  // RealLimits::nonnegative different.
109  // FIXME Is there better solution? Can we drop either positive or non-negative?
110  return (m_has_lower_limit == other.m_has_lower_limit)
112  && m_upper_limit == other.m_upper_limit;
113 }
114 
115 bool RealLimits::operator!=(const RealLimits& other) const
116 {
117  return !(*this == other);
118 }
119 
120 bool RealLimits::operator<(const RealLimits& other) const
121 {
122  return m_lower_limit < other.m_lower_limit && m_upper_limit < other.m_upper_limit;
123 }
124 
126 {
127  return !hasLowerLimit() && !hasUpperLimit();
128 }
129 
131 {
132  // intenional 'unsafe' double comparison
133  return hasLowerLimit() && !hasUpperLimit() && lowerLimit() == poszero;
134 }
135 
137 {
138  return hasLowerLimit() && !hasUpperLimit() && lowerLimit() == 0.0;
139 }
140 
142 {
143  return !isPositive() && !isNonnegative() && hasLowerLimit() && !hasUpperLimit();
144 }
145 
147 {
148  return !hasLowerLimit() && hasUpperLimit();
149 }
150 
152 {
153  return hasLowerLimit() && hasUpperLimit();
154 }
Limits for double.
Definition: reallimits.h:25
bool isLimited() const
Definition: reallimits.cpp:151
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: reallimits.cpp:66
static RealLimits upperLimited(double bound_value)
Creates an object bounded from the right.
Definition: reallimits.cpp:56
bool isLowerLimited() const
Definition: reallimits.cpp:141
bool isPositive() const
Definition: reallimits.cpp:130
bool operator==(const RealLimits &other) const
Definition: reallimits.cpp:105
bool hasUpperLimit() const
if has upper limit
Definition: reallimits.cpp:81
static RealLimits lowerLimited(double bound_value)
Creates an object bounded from the left.
Definition: reallimits.cpp:41
bool hasLowerAndUpperLimits() const
if has lower and upper limit
Definition: reallimits.cpp:91
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: reallimits.cpp:46
double m_lower_limit
parameter has upper bound
Definition: reallimits.h:81
bool m_has_upper_limit
parameter has lower bound
Definition: reallimits.h:80
bool isInRange(double value) const
returns true if proposed value is in limits range
Definition: reallimits.cpp:96
double upperLimit() const
Returns upper limit.
Definition: reallimits.cpp:86
double lowerLimit() const
Returns lower limit.
Definition: reallimits.cpp:76
bool operator<(const RealLimits &other) const
Definition: reallimits.cpp:120
bool isNonnegative() const
Definition: reallimits.cpp:136
bool operator!=(const RealLimits &other) const
Definition: reallimits.cpp:115
bool isLimitless() const
Definition: reallimits.cpp:125
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: reallimits.cpp:51
bool isUpperLimited() const
Definition: reallimits.cpp:146
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: reallimits.cpp:61
bool hasLowerLimit() const
if has lower limit
Definition: reallimits.cpp:71
double m_upper_limit
minimum allowed value
Definition: reallimits.h:82
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?