ACF $AcfVersion:0$
CDouble.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QtMath>
7
8// ACF includes
9#include <iser/IArchive.h>
10#include <imath/TVector.h>
11
12
13namespace imath
14{
15
16
95class CDouble: public TVector<1>
96{
97public:
99
100 CDouble(double value = 0.0);
101 CDouble(const BaseClass& value);
102
106 CDouble GetRounded(int precision = 2) const;
107
111 bool IsRoundedEqual(const CDouble& value, int precision = 2) const;
112
116 CDouble GetRoundedDown(int precision = 2) const;
117
121 bool IsRoundedDownEqual(const CDouble& value, int precision = 2) const;
122
126 CDouble GetRoundedUp(int precision = 2) const;
127
131 bool IsRoundedUpEqual(const CDouble& value, int precision = 2) const;
132
136 bool IsSimiliar(const CDouble& value, double tolerance = I_BIG_EPSILON) const;
137
138 // operators
139 operator double() const;
140
141 bool operator==(const CDouble& value) const;
142 bool operator!=(const CDouble& value) const;
143 bool operator<(const CDouble& value) const;
144 bool operator>(const CDouble& value) const;
145 bool operator<=(const CDouble& value) const;
146 bool operator>=(const CDouble& value) const;
147
148 CDouble operator+(const CDouble& value) const;
149 CDouble operator-(const CDouble& value) const;
150 CDouble operator*(const CDouble& value) const;
151 CDouble operator/(const CDouble& value) const;
152
153 const CDouble& operator=(const CDouble& value);
154
155 const CDouble& operator+=(const CDouble& value);
156 const CDouble& operator-=(const CDouble& value);
157 const CDouble& operator*=(const CDouble& value);
158 const CDouble& operator/=(const CDouble& value);
159
160 // static methods
161 static double GetRounded(double value, int precision = 2);
162 static bool IsRoundedEqual(double value1, double value2, int precision = 2);
163 static double GetRoundedDown(double value, int precision = 2);
164 static bool IsRoundedDownEqual(double value1, double value2, int precision = 2);
165 static double GetRoundedUp(double value, int precision = 2);
166 static bool IsRoundedUpEqual(double value1, double value2, int precision = 2);
167 static bool IsSimiliar(double value1, double value2, double tolerance = I_BIG_EPSILON);
168};
169
170
171// inline methods
172
173inline CDouble::CDouble(double value)
174{
175 SetElement(0, value);
176}
177
178
179inline CDouble::CDouble(const TVector<1>& value)
180: BaseClass(value)
181{
182}
183
184
185inline CDouble::operator double() const
186{
187 return GetElement(0);
188}
189
190
191inline CDouble CDouble::GetRounded(int precision) const
192{
193 return GetRounded(GetElement(0), precision);
194}
195
196
197inline bool CDouble::IsRoundedEqual(const CDouble& value, int precision) const
198{
199 return IsRoundedEqual(*this, value, precision);
200}
201
202
203inline CDouble CDouble::GetRoundedDown(int precision) const
204{
205 return GetRoundedDown(*this, precision);
206}
207
208
209inline bool CDouble::IsRoundedDownEqual(const CDouble& value, int precision) const
210{
211 return IsRoundedDownEqual(*this, value, precision);
212}
213
214
215inline CDouble CDouble::GetRoundedUp(int precision) const
216{
217 return GetRoundedUp(*this, precision);
218}
219
220
221inline bool CDouble::IsRoundedUpEqual(const CDouble& value, int precision) const
222{
223 return IsRoundedUpEqual(*this, value, precision);
224}
225
226
227inline bool CDouble::IsSimiliar(const CDouble& value, double tolerance) const
228{
229 return IsSimiliar(*this, value, tolerance);
230}
231
232
233inline bool CDouble::operator==(const CDouble& value) const
234{
235 return GetElement(0) == value.GetElement(0);
236}
237
238
239inline bool CDouble::operator!=(const CDouble& value) const
240{
241 return GetElement(0) != value.GetElement(0);
242}
243
244
245inline bool CDouble::operator<(const CDouble& value) const
246{
247 return GetElement(0) < value.GetElement(0);
248}
249
250
251inline bool CDouble::operator>(const CDouble& value) const
252{
253 return GetElement(0) > value.GetElement(0);
254}
255
256
257inline bool CDouble::operator<=(const CDouble& value) const
258{
259 return GetElement(0) <= value.GetElement(0);
260}
261
262
263inline bool CDouble::operator>=(const CDouble& value) const
264{
265 return GetElement(0) >= value.GetElement(0);
266}
267
268
269inline CDouble CDouble::operator+(const CDouble& value) const
270{
271 return GetElement(0) + value.GetElement(0);
272}
273
274
275inline CDouble CDouble::operator-(const CDouble& value) const
276{
277 return GetElement(0) - value.GetElement(0);
278}
279
280
281inline CDouble CDouble::operator*(const CDouble& value) const
282{
283 return GetElement(0) * value.GetElement(0);
284}
285
286
287inline CDouble CDouble::operator/(const CDouble& value) const
288{
289 return GetElement(0) / value.GetElement(0);
290}
291
292
293inline const CDouble& CDouble::operator=(const CDouble& value)
294{
295 operator[](0) = value.GetElement(0);
296
297 return *this;
298}
299
300
301inline const CDouble& CDouble::operator+=(const CDouble& value)
302{
303 operator[](0) += value.GetElement(0);
304
305 return *this;
306}
307
308
309inline const CDouble& CDouble::operator-=(const CDouble& value)
310{
311 operator[](0) -= value.GetElement(0);
312
313 return *this;
314}
315
316
317inline const CDouble& CDouble::operator*=(const CDouble& value)
318{
319 operator[](0) *= value.GetElement(0);
320
321 return *this;
322}
323
324
325inline const CDouble& CDouble::operator/=(const CDouble& value)
326{
327 operator[](0) /= value.GetElement(0);
328
329 return *this;
330}
331
332
333// static methods
334
335inline double CDouble::GetRounded(double value, int precision)
336{
337 double scale = qPow(10.0, precision);
338
339 return qFloor(value * scale + 0.5) / scale;
340}
341
342
343inline bool CDouble::IsRoundedEqual(double value1, double value2, int precision)
344{
345 double scale = qPow(10.0, precision);
346
347 return qFloor(value1 * scale + 0.5) == qFloor(value2 * scale + 0.5);
348}
349
350
351inline double CDouble::GetRoundedDown(double value, int precision)
352{
353 double scale = qPow(10.0, precision);
354
355 return qFloor(value * scale) / scale;
356}
357
358
359inline bool CDouble::IsRoundedDownEqual(double value1, double value2, int precision)
360{
361 double scale = qPow(10.0, precision);
362
363 return qFloor(value1 * scale) == qFloor(value2 * scale);
364}
365
366
367inline double CDouble::GetRoundedUp(double value, int precision)
368{
369 double scale = qPow(10.0, precision);
370
371 return qCeil(value * scale) / scale;
372}
373
374
375inline bool CDouble::IsRoundedUpEqual(double value1, double value2, int precision)
376{
377 double scale = qPow(10.0, precision);
378
379 return qCeil(value1 * scale) == qCeil(value2 * scale);
380}
381
382
383inline bool CDouble::IsSimiliar(double value1, double value2, double tolerance)
384{
385 return qAbs(value1 - value2) <= tolerance;
386}
387
388
389} // namespace imath
390
391
Wrapper class for double precision floating-point values with rounding utilities.
Definition CDouble.h:96
const CDouble & operator+=(const CDouble &value)
Definition CDouble.h:301
CDouble operator+(const CDouble &value) const
Definition CDouble.h:269
bool operator<(const CDouble &value) const
Definition CDouble.h:245
CDouble operator/(const CDouble &value) const
Definition CDouble.h:287
const CDouble & operator*=(const CDouble &value)
Definition CDouble.h:317
bool IsRoundedUpEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding down.
Definition CDouble.h:221
CDouble GetRoundedUp(int precision=2) const
Get rounded down value with specified precision.
Definition CDouble.h:215
TVector< 1 > BaseClass
Definition CDouble.h:98
bool operator!=(const CDouble &value) const
Definition CDouble.h:239
bool operator>=(const CDouble &value) const
Definition CDouble.h:263
bool IsRoundedDownEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding down.
Definition CDouble.h:209
bool operator==(const CDouble &value) const
Definition CDouble.h:233
const CDouble & operator-=(const CDouble &value)
Definition CDouble.h:309
CDouble operator*(const CDouble &value) const
Definition CDouble.h:281
CDouble GetRounded(int precision=2) const
Get rounded value.
Definition CDouble.h:191
bool operator>(const CDouble &value) const
Definition CDouble.h:251
bool IsRoundedEqual(const CDouble &value, int precision=2) const
Check if two values are equal after rounding.
Definition CDouble.h:197
const CDouble & operator=(const CDouble &value)
Definition CDouble.h:293
CDouble GetRoundedDown(int precision=2) const
Get rounded down value with specified precision.
Definition CDouble.h:203
const CDouble & operator/=(const CDouble &value)
Definition CDouble.h:325
bool IsSimiliar(const CDouble &value, double tolerance=I_BIG_EPSILON) const
Check if two values are similiar with specified tolerance.
Definition CDouble.h:227
bool operator<=(const CDouble &value) const
Definition CDouble.h:257
CDouble(double value=0.0)
Definition CDouble.h:173
Implementation of fixed-size mathematical vector with specified type of elements.
Definition TVector.h:95
const double & operator[](int i) const
Definition TVector.h:882
TVector< Size, double > operator-() const
Definition TVector.h:817
void SetElement(int i, const double &value)
Sets the element at the specified index.
Definition TVector.h:560
const double & GetElement(int i) const
Gets the element at the specified index (read-only).
Definition TVector.h:546
static const double I_BIG_EPSILON
Definition istd.h:26
Package with mathematical functions and algebraical primitives.