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