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

© Witold Gantzke and Kirill Lepskiy