ad_physics
Acceleration.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_ACCELERATION_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_ACCELERATION_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_ACCELERATION_EXPLICIT_CONVERSION_
51 #endif
52 
60 {
61 public:
65  static const double cMinValue;
66 
70  static const double cMaxValue;
71 
76  static const double cPrecisionValue;
77 
85  : mAcceleration(std::numeric_limits<double>::quiet_NaN())
86  {
87  }
88 
95  : mAcceleration(iAcceleration)
96  {
97  }
98 
102  Acceleration(const Acceleration &other) = default;
103 
107  Acceleration(Acceleration &&other) = default;
108 
116  Acceleration &operator=(const Acceleration &other) = default;
117 
125  Acceleration &operator=(Acceleration &&other) = default;
126 
134  bool operator==(const Acceleration &other) const
135  {
136  ensureValid();
137  other.ensureValid();
138  return std::fabs(mAcceleration - other.mAcceleration) < cPrecisionValue;
139  }
140 
148  bool operator!=(const Acceleration &other) const
149  {
150  return !operator==(other);
151  }
152 
162  bool operator>(const Acceleration &other) const
163  {
164  ensureValid();
165  other.ensureValid();
166  return (mAcceleration > other.mAcceleration) && operator!=(other);
167  }
168 
178  bool operator<(const Acceleration &other) const
179  {
180  ensureValid();
181  other.ensureValid();
182  return (mAcceleration < other.mAcceleration) && operator!=(other);
183  }
184 
194  bool operator>=(const Acceleration &other) const
195  {
196  ensureValid();
197  other.ensureValid();
198  return ((mAcceleration > other.mAcceleration) || operator==(other));
199  }
200 
210  bool operator<=(const Acceleration &other) const
211  {
212  ensureValid();
213  other.ensureValid();
214  return ((mAcceleration < other.mAcceleration) || operator==(other));
215  }
216 
227  Acceleration operator+(const Acceleration &other) const
228  {
229  ensureValid();
230  other.ensureValid();
231  Acceleration const result(mAcceleration + other.mAcceleration);
232  result.ensureValid();
233  return result;
234  }
235 
247  {
248  ensureValid();
249  other.ensureValid();
250  mAcceleration += other.mAcceleration;
251  ensureValid();
252  return *this;
253  }
254 
265  Acceleration operator-(const Acceleration &other) const
266  {
267  ensureValid();
268  other.ensureValid();
269  Acceleration const result(mAcceleration - other.mAcceleration);
270  result.ensureValid();
271  return result;
272  }
273 
285  {
286  ensureValid();
287  other.ensureValid();
288  mAcceleration -= other.mAcceleration;
289  ensureValid();
290  return *this;
291  }
292 
303  Acceleration operator*(const double &scalar) const
304  {
305  ensureValid();
306  Acceleration const result(mAcceleration * scalar);
307  result.ensureValid();
308  return result;
309  }
310 
321  Acceleration operator/(const double &scalar) const
322  {
323  Acceleration const scalarAcceleration(scalar);
324  Acceleration const result(operator/(scalarAcceleration));
325  result.ensureValid();
326  return result;
327  }
328 
340  double operator/(const Acceleration &other) const
341  {
342  ensureValid();
343  other.ensureValidNonZero();
344  double const result = mAcceleration / other.mAcceleration;
345  return result;
346  }
347 
357  {
358  ensureValid();
359  Acceleration const result(-mAcceleration);
360  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
361  return result;
362  }
363 
371  {
372  return mAcceleration;
373  }
374 
382  bool isValid() const
383  {
384  auto const valueClass = std::fpclassify(mAcceleration);
385  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mAcceleration)
386  && (mAcceleration <= cMaxValue);
387  }
388 
395  void ensureValid() const
396  {
397  if (!isValid())
398  {
399  spdlog::info("ensureValid(::ad::physics::Acceleration)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
400 #if (AD_PHYSICS_ACCELERATION_THROWS_EXCEPTION == 1)
401  throw std::out_of_range("Acceleration value out of range"); // LCOV_EXCL_BR_LINE
402 #endif
403  }
404  }
405 
412  void ensureValidNonZero() const
413  {
414  ensureValid();
415  if (operator==(Acceleration(0.))) // LCOV_EXCL_BR_LINE
416  {
417  spdlog::info("ensureValid(::ad::physics::Acceleration)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
418 #if (AD_PHYSICS_ACCELERATION_THROWS_EXCEPTION == 1)
419  throw std::out_of_range("Acceleration value is zero"); // LCOV_EXCL_BR_LINE
420 #endif
421  }
422  }
423 
428  {
429  return Acceleration(cMinValue);
430  }
431 
436  {
437  return Acceleration(cMaxValue);
438  }
439 
444  {
446  }
447 
448 private:
452  double mAcceleration;
453 };
454 
455 } // namespace physics
456 } // namespace ad
468 inline ::ad::physics::Acceleration operator*(const double &other, ::ad::physics::Acceleration const &value)
469 {
470  return value.operator*(other);
471 }
472 
476 namespace std {
477 
481 inline ::ad::physics::Acceleration fabs(const ::ad::physics::Acceleration other)
482 {
483  ::ad::physics::Acceleration const result(std::fabs(static_cast<double>(other)));
484  return result;
485 }
486 
495 template <> class numeric_limits<::ad::physics::Acceleration> : public numeric_limits<double>
496 {
497 public:
501  static inline ::ad::physics::Acceleration lowest()
502  {
503  return ::ad::physics::Acceleration::getMin();
504  }
508  static inline ::ad::physics::Acceleration max()
509  {
510  return ::ad::physics::Acceleration::getMax();
511  }
512 
516  static inline ::ad::physics::Acceleration epsilon()
517  {
518  return ::ad::physics::Acceleration::getPrecision();
519  }
520 };
521 
522 } // namespace std
523 
527 #ifndef GEN_GUARD_AD_PHYSICS_ACCELERATION
528 #define GEN_GUARD_AD_PHYSICS_ACCELERATION
529 
532 namespace ad {
536 namespace physics {
537 
547 inline std::ostream &operator<<(std::ostream &os, Acceleration const &_value)
548 {
549  return os << double(_value);
550 }
551 
552 } // namespace physics
553 } // namespace ad
554 
555 namespace std {
559 inline std::string to_string(::ad::physics::Acceleration const &value)
560 {
561  return to_string(static_cast<double>(value));
562 }
563 } // namespace std
564 #endif // GEN_GUARD_AD_PHYSICS_ACCELERATION
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Acceleration::Acceleration
Acceleration()
default constructor
Definition: Acceleration.hpp:84
ad::physics::Acceleration::operator-
Acceleration operator-() const
standard arithmetic operator
Definition: Acceleration.hpp:356
std::numeric_limits<::ad::physics::Acceleration >::max
static inline ::ad::physics::Acceleration max()
Definition: Acceleration.hpp:508
ad::physics::Acceleration::operator/
double operator/(const Acceleration &other) const
standard arithmetic operator
Definition: Acceleration.hpp:340
ad::physics::Acceleration::operator-
Acceleration operator-(const Acceleration &other) const
standard arithmetic operator
Definition: Acceleration.hpp:265
ad::physics::Acceleration::getMin
static Acceleration getMin()
get minimum valid Acceleration (i.e. cMinValue)
Definition: Acceleration.hpp:427
ad::physics::Acceleration::Acceleration
_AD_PHYSICS_ACCELERATION_EXPLICIT_CONVERSION_ Acceleration(double const iAcceleration)
standard constructor
Definition: Acceleration.hpp:94
operator*
inline ::ad::physics::Acceleration operator*(const double &other, ::ad::physics::Acceleration const &value)
standard arithmetic operator
Definition: Acceleration.hpp:468
ad::physics::Acceleration::operator!=
bool operator!=(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:148
ad::physics::Acceleration::operator>=
bool operator>=(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:194
ad::physics::Acceleration::getPrecision
static Acceleration getPrecision()
get assumed accuracy of Acceleration (i.e. cPrecisionValue)
Definition: Acceleration.hpp:443
ad::physics::Acceleration::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Acceleration value accuracy (used in comparison operator==(),...
Definition: Acceleration.hpp:76
ad::physics::Acceleration::operator<
bool operator<(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:178
ad::physics::Acceleration::operator<=
bool operator<=(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:210
ad::physics::Acceleration::operator/
Acceleration operator/(const double &scalar) const
standard arithmetic operator
Definition: Acceleration.hpp:321
ad::physics::Acceleration::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Acceleration is valid and non zero
Definition: Acceleration.hpp:412
ad::physics::Acceleration::cMinValue
static const double cMinValue
constant defining the minimum valid Acceleration value (used in isValid())
Definition: Acceleration.hpp:65
ad::physics::Acceleration::cMaxValue
static const double cMaxValue
constant defining the maximum valid Acceleration value (used in isValid())
Definition: Acceleration.hpp:70
ad::physics::Acceleration::operator==
bool operator==(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:134
ad::physics::Acceleration::operator+=
Acceleration & operator+=(const Acceleration &other)
standard arithmetic operator
Definition: Acceleration.hpp:246
ad::physics::Acceleration::operator-=
Acceleration operator-=(const Acceleration &other)
standard arithmetic operator
Definition: Acceleration.hpp:284
_AD_PHYSICS_ACCELERATION_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_ACCELERATION_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Acceleration.hpp:50
ad::physics::Acceleration::getMax
static Acceleration getMax()
get maximum valid Acceleration (i.e. cMaxValue)
Definition: Acceleration.hpp:435
std::numeric_limits<::ad::physics::Acceleration >::epsilon
static inline ::ad::physics::Acceleration epsilon()
Definition: Acceleration.hpp:516
std::numeric_limits<::ad::physics::Acceleration >::lowest
static inline ::ad::physics::Acceleration lowest()
Definition: Acceleration.hpp:501
ad::physics::Acceleration
DataType Acceleration.
Definition: Acceleration.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::Acceleration::operator=
Acceleration & operator=(const Acceleration &other)=default
standard assignment operator
std::fabs
inline ::ad::physics::Acceleration fabs(const ::ad::physics::Acceleration other)
overload of the std::fabs for Acceleration
Definition: Acceleration.hpp:481
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::Acceleration::isValid
bool isValid() const
Definition: Acceleration.hpp:382
ad::physics::Acceleration::operator>
bool operator>(const Acceleration &other) const
standard comparison operator
Definition: Acceleration.hpp:162
ad::physics::Acceleration::operator+
Acceleration operator+(const Acceleration &other) const
standard arithmetic operator
Definition: Acceleration.hpp:227
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Acceleration::ensureValid
void ensureValid() const
ensure that the Acceleration is valid
Definition: Acceleration.hpp:395
ad::physics::Acceleration::operator*
Acceleration operator*(const double &scalar) const
standard arithmetic operator
Definition: Acceleration.hpp:303