CDouble.h
Go to the documentation of this file.
1 #ifndef istd_CDouble_included
2 #define istd_CDouble_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtMath>
7 
8 // ACF includes
9 #include <iser/IArchive.h>
10 
11 #include <imath/TVector.h>
12 
13 
14 namespace imath
15 {
16 
17 
21 class CDouble: public TVector<1>
22 {
23 public:
25 
26  CDouble(double value = 0.0);
27  CDouble(const BaseClass& value);
28 
32  CDouble GetRounded(int precision = 2) const;
33 
37  bool IsRoundedEqual(const CDouble& value, int precision = 2) const;
38 
42  CDouble GetRoundedDown(int precision = 2) const;
43 
47  bool IsRoundedDownEqual(const CDouble& value, int precision = 2) const;
48 
52  CDouble GetRoundedUp(int precision = 2) const;
53 
57  bool IsRoundedUpEqual(const CDouble& value, int precision = 2) const;
58 
62  bool IsSimiliar(const CDouble& value, double tolerance = I_BIG_EPSILON) const;
63 
64  // operators
65  operator double() const;
66 
67  bool operator==(const CDouble& value) const;
68  bool operator!=(const CDouble& value) const;
69  bool operator<(const CDouble& value) const;
70  bool operator>(const CDouble& value) const;
71  bool operator<=(const CDouble& value) const;
72  bool operator>=(const CDouble& value) const;
73 
74  CDouble operator+(const CDouble& value) const;
75  CDouble operator-(const CDouble& value) const;
76  CDouble operator*(const CDouble& value) const;
77  CDouble operator/(const CDouble& value) const;
78 
79  const CDouble& operator=(const CDouble& value);
80 
81  const CDouble& operator+=(const CDouble& value);
82  const CDouble& operator-=(const CDouble& value);
83  const CDouble& operator*=(const CDouble& value);
84  const CDouble& operator/=(const CDouble& value);
85 
86  // static methods
87  static double GetRounded(double value, int precision = 2);
88  static bool IsRoundedEqual(double value1, double value2, int precision = 2);
89  static double GetRoundedDown(double value, int precision = 2);
90  static bool IsRoundedDownEqual(double value1, double value2, int precision = 2);
91  static double GetRoundedUp(double value, int precision = 2);
92  static bool IsRoundedUpEqual(double value1, double value2, int precision = 2);
93  static bool IsSimiliar(double value1, double value2, double tolerance = I_BIG_EPSILON);
94 };
95 
96 
97 // inline methods
98 
99 inline CDouble::CDouble(double value)
100 {
101  SetElement(0, value);
102 }
103 
104 
105 inline CDouble::CDouble(const TVector<1>& value)
106 : BaseClass(value)
107 {
108 }
109 
110 
111 inline CDouble::operator double() const
112 {
113  return GetElement(0);
114 }
115 
116 
117 inline CDouble CDouble::GetRounded(int precision) const
118 {
119  return GetRounded(GetElement(0), precision);
120 }
121 
122 
123 inline bool CDouble::IsRoundedEqual(const CDouble& value, int precision) const
124 {
125  return IsRoundedEqual(*this, value, precision);
126 }
127 
128 
129 inline CDouble CDouble::GetRoundedDown(int precision) const
130 {
131  return GetRoundedDown(*this, precision);
132 }
133 
134 
135 inline bool CDouble::IsRoundedDownEqual(const CDouble& value, int precision) const
136 {
137  return IsRoundedDownEqual(*this, value, precision);
138 }
139 
140 
141 inline CDouble CDouble::GetRoundedUp(int precision) const
142 {
143  return GetRoundedUp(*this, precision);
144 }
145 
146 
147 inline bool CDouble::IsRoundedUpEqual(const CDouble& value, int precision) const
148 {
149  return IsRoundedUpEqual(*this, value, precision);
150 }
151 
152 
153 inline bool CDouble::IsSimiliar(const CDouble& value, double tolerance) const
154 {
155  return IsSimiliar(*this, value, tolerance);
156 }
157 
158 
159 inline bool CDouble::operator==(const CDouble& value) const
160 {
161  return GetElement(0) == value.GetElement(0);
162 }
163 
164 
165 inline bool CDouble::operator!=(const CDouble& value) const
166 {
167  return GetElement(0) != value.GetElement(0);
168 }
169 
170 
171 inline bool CDouble::operator<(const CDouble& value) const
172 {
173  return GetElement(0) < value.GetElement(0);
174 }
175 
176 
177 inline bool CDouble::operator>(const CDouble& value) const
178 {
179  return GetElement(0) > value.GetElement(0);
180 }
181 
182 
183 inline bool CDouble::operator<=(const CDouble& value) const
184 {
185  return GetElement(0) <= value.GetElement(0);
186 }
187 
188 
189 inline bool CDouble::operator>=(const CDouble& value) const
190 {
191  return GetElement(0) >= value.GetElement(0);
192 }
193 
194 
195 inline CDouble CDouble::operator+(const CDouble& value) const
196 {
197  return GetElement(0) + value.GetElement(0);
198 }
199 
200 
201 inline CDouble CDouble::operator-(const CDouble& value) const
202 {
203  return GetElement(0) - value.GetElement(0);
204 }
205 
206 
207 inline CDouble CDouble::operator*(const CDouble& value) const
208 {
209  return GetElement(0) * value.GetElement(0);
210 }
211 
212 
213 inline CDouble CDouble::operator/(const CDouble& value) const
214 {
215  return GetElement(0) / value.GetElement(0);
216 }
217 
218 
219 inline const CDouble& CDouble::operator=(const CDouble& value)
220 {
221  operator[](0) = value.GetElement(0);
222 
223  return *this;
224 }
225 
226 
227 inline const CDouble& CDouble::operator+=(const CDouble& value)
228 {
229  operator[](0) += value.GetElement(0);
230 
231  return *this;
232 }
233 
234 
235 inline const CDouble& CDouble::operator-=(const CDouble& value)
236 {
237  operator[](0) -= value.GetElement(0);
238 
239  return *this;
240 }
241 
242 
243 inline const CDouble& CDouble::operator*=(const CDouble& value)
244 {
245  operator[](0) *= value.GetElement(0);
246 
247  return *this;
248 }
249 
250 
251 inline const CDouble& CDouble::operator/=(const CDouble& value)
252 {
253  operator[](0) /= value.GetElement(0);
254 
255  return *this;
256 }
257 
258 
259 // static methods
260 
261 inline double CDouble::GetRounded(double value, int precision)
262 {
263  double scale = qPow(10.0, precision);
264 
265  return qFloor(value * scale + 0.5) / scale;
266 }
267 
268 
269 inline bool CDouble::IsRoundedEqual(double value1, double value2, int precision)
270 {
271  double scale = qPow(10.0, precision);
272 
273  return qFloor(value1 * scale + 0.5) == qFloor(value2 * scale + 0.5);
274 }
275 
276 
277 inline double CDouble::GetRoundedDown(double value, int precision)
278 {
279  double scale = qPow(10.0, precision);
280 
281  return qFloor(value * scale) / scale;
282 }
283 
284 
285 inline bool CDouble::IsRoundedDownEqual(double value1, double value2, int precision)
286 {
287  double scale = qPow(10.0, precision);
288 
289  return qFloor(value1 * scale) == qFloor(value2 * scale);
290 }
291 
292 
293 inline double CDouble::GetRoundedUp(double value, int precision)
294 {
295  double scale = qPow(10.0, precision);
296 
297  return qCeil(value * scale) / scale;
298 }
299 
300 
301 inline bool CDouble::IsRoundedUpEqual(double value1, double value2, int precision)
302 {
303  double scale = qPow(10.0, precision);
304 
305  return qCeil(value1 * scale) == qCeil(value2 * scale);
306 }
307 
308 
309 inline bool CDouble::IsSimiliar(double value1, double value2, double tolerance)
310 {
311  return qAbs(value1 - value2) <= tolerance;
312 }
313 
314 
315 } // namespace imath
316 
317 
318 #endif // !istd_CDouble_included
319 
320 
const CDouble & operator=(const CDouble &value)
Definition: CDouble.h:219
bool operator==(const CDouble &value) const
Definition: CDouble.h:159
bool IsRoundedEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding.
Definition: CDouble.h:123
bool operator>=(const CDouble &value) const
Definition: CDouble.h:189
const CDouble & operator-=(const CDouble &value)
Definition: CDouble.h:235
TVector< Size, double > operator-() const
void SetElement(int i, const double &value)
Set element at specified i.
const CDouble & operator*=(const CDouble &value)
Definition: CDouble.h:243
bool IsSimiliar(const CDouble &value, double tolerance=I_BIG_EPSILON) const
Check if two values are similiar with specified tolerance.
Definition: CDouble.h:153
bool operator<=(const CDouble &value) const
Definition: CDouble.h:183
CDouble GetRounded(int precision=2) const
Get rounded value.
Definition: CDouble.h:117
bool operator>(const CDouble &value) const
Definition: CDouble.h:177
bool IsRoundedUpEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding down.
Definition: CDouble.h:147
CDouble GetRoundedDown(int precision=2) const
Get rounded down value with specified precision.
Definition: CDouble.h:129
CDouble(double value=0.0)
Definition: CDouble.h:99
CDouble operator/(const CDouble &value) const
Definition: CDouble.h:213
CDouble operator+(const CDouble &value) const
Definition: CDouble.h:195
Implementation of fixed-size mathematical vector with specified type of elements. ...
Definition: TVector.h:27
bool IsRoundedDownEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding down.
Definition: CDouble.h:135
CDouble operator*(const CDouble &value) const
Definition: CDouble.h:207
const CDouble & operator/=(const CDouble &value)
Definition: CDouble.h:251
const double & GetElement(int i) const
Get element at specified i.
static const double I_BIG_EPSILON
Definition: istd.h:26
TVector< 1 > BaseClass
Definition: CDouble.h:24
const CDouble & operator+=(const CDouble &value)
Definition: CDouble.h:227
Simple wrapper of real value represented as double type.
Definition: CDouble.h:21
bool operator<(const CDouble &value) const
Definition: CDouble.h:171
bool operator!=(const CDouble &value) const
Definition: CDouble.h:165
CDouble GetRoundedUp(int precision=2) const
Get rounded down value with specified precision.
Definition: CDouble.h:141
const double & operator[](int i) const

© 2007-2017 Witold Gantzke and Kirill Lepskiy