ad_physics
Probability.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_PROBABILITY_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_
51 #endif
52 
59 {
60 public:
64  static const double cMinValue;
65 
69  static const double cMaxValue;
70 
75  static const double cPrecisionValue;
76 
84  : mProbability(std::numeric_limits<double>::quiet_NaN())
85  {
86  }
87 
94  : mProbability(iProbability)
95  {
96  }
97 
101  Probability(const Probability &other) = default;
102 
106  Probability(Probability &&other) = default;
107 
115  Probability &operator=(const Probability &other) = default;
116 
124  Probability &operator=(Probability &&other) = default;
125 
133  bool operator==(const Probability &other) const
134  {
135  ensureValid();
136  other.ensureValid();
137  return std::fabs(mProbability - other.mProbability) < cPrecisionValue;
138  }
139 
147  bool operator!=(const Probability &other) const
148  {
149  return !operator==(other);
150  }
151 
161  bool operator>(const Probability &other) const
162  {
163  ensureValid();
164  other.ensureValid();
165  return (mProbability > other.mProbability) && operator!=(other);
166  }
167 
177  bool operator<(const Probability &other) const
178  {
179  ensureValid();
180  other.ensureValid();
181  return (mProbability < other.mProbability) && operator!=(other);
182  }
183 
193  bool operator>=(const Probability &other) const
194  {
195  ensureValid();
196  other.ensureValid();
197  return ((mProbability > other.mProbability) || operator==(other));
198  }
199 
209  bool operator<=(const Probability &other) const
210  {
211  ensureValid();
212  other.ensureValid();
213  return ((mProbability < other.mProbability) || operator==(other));
214  }
215 
226  Probability operator+(const Probability &other) const
227  {
228  ensureValid();
229  other.ensureValid();
230  Probability const result(mProbability + other.mProbability);
231  result.ensureValid();
232  return result;
233  }
234 
246  {
247  ensureValid();
248  other.ensureValid();
249  mProbability += other.mProbability;
250  ensureValid();
251  return *this;
252  }
253 
264  Probability operator-(const Probability &other) const
265  {
266  ensureValid();
267  other.ensureValid();
268  Probability const result(mProbability - other.mProbability);
269  result.ensureValid();
270  return result;
271  }
272 
284  {
285  ensureValid();
286  other.ensureValid();
287  mProbability -= other.mProbability;
288  ensureValid();
289  return *this;
290  }
291 
302  Probability operator*(const double &scalar) const
303  {
304  ensureValid();
305  Probability const result(mProbability * scalar);
306  result.ensureValid();
307  return result;
308  }
309 
320  Probability operator/(const double &scalar) const
321  {
322  Probability const scalarProbability(scalar);
323  Probability const result(operator/(scalarProbability));
324  result.ensureValid();
325  return result;
326  }
327 
339  double operator/(const Probability &other) const
340  {
341  ensureValid();
342  other.ensureValidNonZero();
343  double const result = mProbability / other.mProbability;
344  return result;
345  }
346 
356  {
357  ensureValid();
358  Probability const result(-mProbability);
359  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
360  return result;
361  }
362 
370  {
371  return mProbability;
372  }
373 
381  bool isValid() const
382  {
383  auto const valueClass = std::fpclassify(mProbability);
384  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mProbability)
385  && (mProbability <= cMaxValue);
386  }
387 
394  void ensureValid() const
395  {
396  if (!isValid())
397  {
398  spdlog::info("ensureValid(::ad::physics::Probability)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
399 #if (AD_PHYSICS_PROBABILITY_THROWS_EXCEPTION == 1)
400  throw std::out_of_range("Probability value out of range"); // LCOV_EXCL_BR_LINE
401 #endif
402  }
403  }
404 
411  void ensureValidNonZero() const
412  {
413  ensureValid();
414  if (operator==(Probability(0.))) // LCOV_EXCL_BR_LINE
415  {
416  spdlog::info("ensureValid(::ad::physics::Probability)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
417 #if (AD_PHYSICS_PROBABILITY_THROWS_EXCEPTION == 1)
418  throw std::out_of_range("Probability value is zero"); // LCOV_EXCL_BR_LINE
419 #endif
420  }
421  }
422 
427  {
428  return Probability(cMinValue);
429  }
430 
435  {
436  return Probability(cMaxValue);
437  }
438 
443  {
445  }
446 
447 private:
451  double mProbability;
452 };
453 
454 } // namespace physics
455 } // namespace ad
467 inline ::ad::physics::Probability operator*(const double &other, ::ad::physics::Probability const &value)
468 {
469  return value.operator*(other);
470 }
471 
475 namespace std {
476 
480 inline ::ad::physics::Probability fabs(const ::ad::physics::Probability other)
481 {
482  ::ad::physics::Probability const result(std::fabs(static_cast<double>(other)));
483  return result;
484 }
485 
494 template <> class numeric_limits<::ad::physics::Probability> : public numeric_limits<double>
495 {
496 public:
500  static inline ::ad::physics::Probability lowest()
501  {
502  return ::ad::physics::Probability::getMin();
503  }
507  static inline ::ad::physics::Probability max()
508  {
509  return ::ad::physics::Probability::getMax();
510  }
511 
515  static inline ::ad::physics::Probability epsilon()
516  {
517  return ::ad::physics::Probability::getPrecision();
518  }
519 };
520 
521 } // namespace std
522 
526 #ifndef GEN_GUARD_AD_PHYSICS_PROBABILITY
527 #define GEN_GUARD_AD_PHYSICS_PROBABILITY
528 
531 namespace ad {
535 namespace physics {
536 
546 inline std::ostream &operator<<(std::ostream &os, Probability 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::Probability const &value)
559 {
560  return to_string(static_cast<double>(value));
561 }
562 } // namespace std
563 #endif // GEN_GUARD_AD_PHYSICS_PROBABILITY
ad::physics::Probability::operator<=
bool operator<=(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:209
ad::physics::Probability::operator>=
bool operator>=(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:193
ad
namespace ad
Definition: Acceleration.hpp:30
_AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Probability.hpp:50
std::numeric_limits<::ad::physics::Probability >::lowest
static inline ::ad::physics::Probability lowest()
Definition: Probability.hpp:500
ad::physics::Probability::getMin
static Probability getMin()
get minimum valid Probability (i.e. cMinValue)
Definition: Probability.hpp:426
ad::physics::Probability::cMaxValue
static const double cMaxValue
constant defining the maximum valid Probability value (used in isValid())
Definition: Probability.hpp:69
ad::physics::Probability::operator/
double operator/(const Probability &other) const
standard arithmetic operator
Definition: Probability.hpp:339
ad::physics::Probability::operator+=
Probability & operator+=(const Probability &other)
standard arithmetic operator
Definition: Probability.hpp:245
std::numeric_limits<::ad::physics::Probability >::epsilon
static inline ::ad::physics::Probability epsilon()
Definition: Probability.hpp:515
ad::physics::Probability::operator+
Probability operator+(const Probability &other) const
standard arithmetic operator
Definition: Probability.hpp:226
ad::physics::Probability::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Probability value accuracy (used in comparison operator==(),...
Definition: Probability.hpp:75
ad::physics::Probability::operator!=
bool operator!=(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:147
std::numeric_limits<::ad::physics::Probability >::max
static inline ::ad::physics::Probability max()
Definition: Probability.hpp:507
ad::physics::Probability::operator>
bool operator>(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:161
ad::physics::Probability::Probability
_AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ Probability(double const iProbability)
standard constructor
Definition: Probability.hpp:93
ad::physics::Probability::ensureValid
void ensureValid() const
ensure that the Probability is valid
Definition: Probability.hpp:394
ad::physics::Probability::operator-
Probability operator-(const Probability &other) const
standard arithmetic operator
Definition: Probability.hpp:264
operator*
inline ::ad::physics::Probability operator*(const double &other, ::ad::physics::Probability const &value)
standard arithmetic operator
Definition: Probability.hpp:467
ad::physics::Probability
DataType Probability.
Definition: Probability.hpp:58
ad::physics::Probability::operator*
Probability operator*(const double &scalar) const
standard arithmetic operator
Definition: Probability.hpp:302
ad::physics::Probability::operator==
bool operator==(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:133
ad::physics::Probability::operator=
Probability & operator=(const Probability &other)=default
standard assignment operator
ad::physics::Probability::operator-=
Probability operator-=(const Probability &other)
standard arithmetic operator
Definition: Probability.hpp:283
ad::physics::Probability::operator/
Probability operator/(const double &scalar) const
standard arithmetic operator
Definition: Probability.hpp:320
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
ad::physics::Probability::Probability
Probability()
default constructor
Definition: Probability.hpp:83
ad::physics::Probability::operator<
bool operator<(const Probability &other) const
standard comparison operator
Definition: Probability.hpp:177
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::Probability::getMax
static Probability getMax()
get maximum valid Probability (i.e. cMaxValue)
Definition: Probability.hpp:434
ad::physics::Probability::operator-
Probability operator-() const
standard arithmetic operator
Definition: Probability.hpp:355
ad::physics::Probability::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Probability is valid and non zero
Definition: Probability.hpp:411
ad::physics::Probability::isValid
bool isValid() const
Definition: Probability.hpp:381
ad::physics::Probability::cMinValue
static const double cMinValue
constant defining the minimum valid Probability value (used in isValid())
Definition: Probability.hpp:64
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Probability::getPrecision
static Probability getPrecision()
get assumed accuracy of Probability (i.e. cPrecisionValue)
Definition: Probability.hpp:442