ad_physics
Weight.hpp
Go to the documentation of this file.
1 /*
2  * ----------------- BEGIN LICENSE BLOCK ---------------------------------
3  *
4  * Copyright (C) 2018-2020 Intel Corporation
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * ----------------- END LICENSE BLOCK -----------------------------------
9  */
10 
18 #pragma once
19 
20 #include <cmath>
21 #include <iostream>
22 #include <limits>
23 #include <sstream>
24 #include <stdexcept>
25 #include "spdlog/fmt/ostr.h"
26 #include "spdlog/spdlog.h"
30 namespace ad {
34 namespace physics {
35 
39 #define AD_PHYSICS_WEIGHT_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_WEIGHT_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_WEIGHT_EXPLICIT_CONVERSION_
51 #endif
52 
59 class Weight
60 {
61 public:
65  static const double cMinValue;
66 
70  static const double cMaxValue;
71 
76  static const double cPrecisionValue;
77 
85  : mWeight(std::numeric_limits<double>::quiet_NaN())
86  {
87  }
88 
95  : mWeight(iWeight)
96  {
97  }
98 
102  Weight(const Weight &other) = default;
103 
107  Weight(Weight &&other) = default;
108 
116  Weight &operator=(const Weight &other) = default;
117 
125  Weight &operator=(Weight &&other) = default;
126 
134  bool operator==(const Weight &other) const
135  {
136  ensureValid();
137  other.ensureValid();
138  return std::fabs(mWeight - other.mWeight) < cPrecisionValue;
139  }
140 
148  bool operator!=(const Weight &other) const
149  {
150  return !operator==(other);
151  }
152 
162  bool operator>(const Weight &other) const
163  {
164  ensureValid();
165  other.ensureValid();
166  return (mWeight > other.mWeight) && operator!=(other);
167  }
168 
178  bool operator<(const Weight &other) const
179  {
180  ensureValid();
181  other.ensureValid();
182  return (mWeight < other.mWeight) && operator!=(other);
183  }
184 
194  bool operator>=(const Weight &other) const
195  {
196  ensureValid();
197  other.ensureValid();
198  return ((mWeight > other.mWeight) || operator==(other));
199  }
200 
210  bool operator<=(const Weight &other) const
211  {
212  ensureValid();
213  other.ensureValid();
214  return ((mWeight < other.mWeight) || operator==(other));
215  }
216 
227  Weight operator+(const Weight &other) const
228  {
229  ensureValid();
230  other.ensureValid();
231  Weight const result(mWeight + other.mWeight);
232  result.ensureValid();
233  return result;
234  }
235 
246  Weight &operator+=(const Weight &other)
247  {
248  ensureValid();
249  other.ensureValid();
250  mWeight += other.mWeight;
251  ensureValid();
252  return *this;
253  }
254 
265  Weight operator-(const Weight &other) const
266  {
267  ensureValid();
268  other.ensureValid();
269  Weight const result(mWeight - other.mWeight);
270  result.ensureValid();
271  return result;
272  }
273 
284  Weight operator-=(const Weight &other)
285  {
286  ensureValid();
287  other.ensureValid();
288  mWeight -= other.mWeight;
289  ensureValid();
290  return *this;
291  }
292 
303  Weight operator*(const double &scalar) const
304  {
305  ensureValid();
306  Weight const result(mWeight * scalar);
307  result.ensureValid();
308  return result;
309  }
310 
321  Weight operator/(const double &scalar) const
322  {
323  Weight const scalarWeight(scalar);
324  Weight const result(operator/(scalarWeight));
325  result.ensureValid();
326  return result;
327  }
328 
340  double operator/(const Weight &other) const
341  {
342  ensureValid();
343  other.ensureValidNonZero();
344  double const result = mWeight / other.mWeight;
345  return result;
346  }
347 
357  {
358  ensureValid();
359  Weight const result(-mWeight);
360  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
361  return result;
362  }
363 
371  {
372  return mWeight;
373  }
374 
382  bool isValid() const
383  {
384  auto const valueClass = std::fpclassify(mWeight);
385  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mWeight) && (mWeight <= cMaxValue);
386  }
387 
394  void ensureValid() const
395  {
396  if (!isValid())
397  {
398  spdlog::info("ensureValid(::ad::physics::Weight)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
399 #if (AD_PHYSICS_WEIGHT_THROWS_EXCEPTION == 1)
400  throw std::out_of_range("Weight value out of range"); // LCOV_EXCL_BR_LINE
401 #endif
402  }
403  }
404 
411  void ensureValidNonZero() const
412  {
413  ensureValid();
414  if (operator==(Weight(0.))) // LCOV_EXCL_BR_LINE
415  {
416  spdlog::info("ensureValid(::ad::physics::Weight)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
417 #if (AD_PHYSICS_WEIGHT_THROWS_EXCEPTION == 1)
418  throw std::out_of_range("Weight value is zero"); // LCOV_EXCL_BR_LINE
419 #endif
420  }
421  }
422 
426  static Weight getMin()
427  {
428  return Weight(cMinValue);
429  }
430 
434  static Weight getMax()
435  {
436  return Weight(cMaxValue);
437  }
438 
443  {
444  return Weight(cPrecisionValue);
445  }
446 
447 private:
451  double mWeight;
452 };
453 
454 } // namespace physics
455 } // namespace ad
467 inline ::ad::physics::Weight operator*(const double &other, ::ad::physics::Weight const &value)
468 {
469  return value.operator*(other);
470 }
471 
475 namespace std {
476 
480 inline ::ad::physics::Weight fabs(const ::ad::physics::Weight other)
481 {
482  ::ad::physics::Weight const result(std::fabs(static_cast<double>(other)));
483  return result;
484 }
485 
494 template <> class numeric_limits<::ad::physics::Weight> : public numeric_limits<double>
495 {
496 public:
500  static inline ::ad::physics::Weight lowest()
501  {
502  return ::ad::physics::Weight::getMin();
503  }
507  static inline ::ad::physics::Weight max()
508  {
509  return ::ad::physics::Weight::getMax();
510  }
511 
515  static inline ::ad::physics::Weight epsilon()
516  {
517  return ::ad::physics::Weight::getPrecision();
518  }
519 };
520 
521 } // namespace std
522 
526 #ifndef GEN_GUARD_AD_PHYSICS_WEIGHT
527 #define GEN_GUARD_AD_PHYSICS_WEIGHT
528 
531 namespace ad {
535 namespace physics {
536 
546 inline std::ostream &operator<<(std::ostream &os, Weight const &_value)
547 {
548  return os << double(_value);
549 }
550 
551 } // namespace physics
552 } // namespace ad
553 
554 namespace std {
558 inline std::string to_string(::ad::physics::Weight const &value)
559 {
560  return to_string(static_cast<double>(value));
561 }
562 } // namespace std
563 #endif // GEN_GUARD_AD_PHYSICS_WEIGHT
ad::physics::Weight::cMaxValue
static const double cMaxValue
constant defining the maximum valid Weight value (used in isValid())
Definition: Weight.hpp:70
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Weight::isValid
bool isValid() const
Definition: Weight.hpp:382
ad::physics::Weight::getMin
static Weight getMin()
get minimum valid Weight (i.e. cMinValue)
Definition: Weight.hpp:426
ad::physics::Weight::Weight
_AD_PHYSICS_WEIGHT_EXPLICIT_CONVERSION_ Weight(double const iWeight)
standard constructor
Definition: Weight.hpp:94
ad::physics::Weight::operator/
double operator/(const Weight &other) const
standard arithmetic operator
Definition: Weight.hpp:340
ad::physics::Weight::operator-=
Weight operator-=(const Weight &other)
standard arithmetic operator
Definition: Weight.hpp:284
ad::physics::Weight::operator-
Weight operator-() const
standard arithmetic operator
Definition: Weight.hpp:356
ad::physics::Weight::operator-
Weight operator-(const Weight &other) const
standard arithmetic operator
Definition: Weight.hpp:265
ad::physics::Weight::operator*
Weight operator*(const double &scalar) const
standard arithmetic operator
Definition: Weight.hpp:303
ad::physics::Weight::operator<
bool operator<(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:178
ad::physics::Weight::Weight
Weight()
default constructor
Definition: Weight.hpp:84
std::numeric_limits<::ad::physics::Weight >::epsilon
static inline ::ad::physics::Weight epsilon()
Definition: Weight.hpp:515
operator*
inline ::ad::physics::Weight operator*(const double &other, ::ad::physics::Weight const &value)
standard arithmetic operator
Definition: Weight.hpp:467
ad::physics::Weight::operator>
bool operator>(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:162
ad::physics::Weight::operator==
bool operator==(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:134
ad::physics::Weight::operator+
Weight operator+(const Weight &other) const
standard arithmetic operator
Definition: Weight.hpp:227
ad::physics::Weight::getPrecision
static Weight getPrecision()
get assumed accuracy of Weight (i.e. cPrecisionValue)
Definition: Weight.hpp:442
ad::physics::Weight::operator=
Weight & operator=(const Weight &other)=default
standard assignment operator
ad::physics::Weight::getMax
static Weight getMax()
get maximum valid Weight (i.e. cMaxValue)
Definition: Weight.hpp:434
std::numeric_limits<::ad::physics::Weight >::lowest
static inline ::ad::physics::Weight lowest()
Definition: Weight.hpp:500
_AD_PHYSICS_WEIGHT_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_WEIGHT_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Weight.hpp:50
ad::physics::Weight::operator/
Weight operator/(const double &scalar) const
standard arithmetic operator
Definition: Weight.hpp:321
ad::physics::Weight::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Weight is valid and non zero
Definition: Weight.hpp:411
ad::physics::Weight
DataType Weight.
Definition: Weight.hpp:59
std::fabs
inline ::ad::physics::Weight fabs(const ::ad::physics::Weight other)
overload of the std::fabs for Weight
Definition: Weight.hpp:480
ad::physics::Weight::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Weight value accuracy (used in comparison operator==(),...
Definition: Weight.hpp:76
std::fabs
inline ::ad::physics::Acceleration fabs(const ::ad::physics::Acceleration other)
overload of the std::fabs for Acceleration
Definition: Acceleration.hpp:481
std::numeric_limits<::ad::physics::Weight >::max
static inline ::ad::physics::Weight max()
Definition: Weight.hpp:507
ad::physics::Weight::cMinValue
static const double cMinValue
constant defining the minimum valid Weight value (used in isValid())
Definition: Weight.hpp:65
std::to_string
std::string to_string(::ad::physics::Acceleration const &value)
overload of the std::to_string for Acceleration
Definition: Acceleration.hpp:559
ad::physics::Weight::operator<=
bool operator<=(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:210
ad::physics::Weight::operator!=
bool operator!=(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:148
ad::physics::Weight::ensureValid
void ensureValid() const
ensure that the Weight is valid
Definition: Weight.hpp:394
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Weight::operator+=
Weight & operator+=(const Weight &other)
standard arithmetic operator
Definition: Weight.hpp:246
ad::physics::Weight::operator>=
bool operator>=(const Weight &other) const
standard comparison operator
Definition: Weight.hpp:194