SPH
Approx.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
11 
13 template <typename Type>
14 class Approx {
15 private:
16  Float epsilon;
17  Type value;
18 
19 public:
20  INLINE explicit Approx(const Type value)
21  : epsilon(EPS)
22  , value(value) {}
23 
24  INLINE Approx operator()(const Type& value) {
25  Approx approx(value);
26  approx.setEpsilon(epsilon);
27  return approx;
28  }
29 
30  INLINE friend bool operator==(const Type& lhs, const Approx& rhs) {
31  return almostEqual(lhs, rhs.value, rhs.epsilon);
32  }
33 
34  INLINE friend bool operator==(const Approx& lhs, const Type& rhs) {
35  return operator==(rhs, lhs);
36  }
37 
38  INLINE friend bool operator!=(const Type& lhs, const Approx& rhs) {
39  return !operator==(lhs, rhs);
40  }
41 
42  INLINE friend bool operator!=(const Approx& lhs, const Type& rhs) {
43  return !operator==(rhs, lhs);
44  }
45 
46  INLINE Approx& setEpsilon(const Float newEps) {
47  epsilon = newEps;
48  return *this;
49  }
50 
51  friend std::ostream& operator<<(std::ostream& stream, const Approx& approx) {
52  stream << "~" << approx.value << " (eps = " << approx.epsilon << ")";
53  return stream;
54  }
55 };
56 
58 template <typename Type>
59 Approx<Type> approx(const Type& value, const Float eps = EPS) {
60  return Approx<Type>(value).setEpsilon(eps);
61 }
62 
63 
INLINE bool almostEqual(const AffineMatrix &m1, const AffineMatrix &m2, const Float eps=EPS)
Definition: AffineMatrix.h:278
Approx< Type > approx(const Type &value, const Float eps=EPS)
We have to wait till c++17 for type deduction in constructors ...
Definition: Approx.h:59
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Symmetric traceless 2nd order tensor.
This is more or less stolen from Catch unit-testing framework.
Definition: Approx.h:14
INLINE Approx(const Type value)
Definition: Approx.h:20
INLINE Approx & setEpsilon(const Float newEps)
Definition: Approx.h:46
friend std::ostream & operator<<(std::ostream &stream, const Approx &approx)
Definition: Approx.h:51
INLINE Approx operator()(const Type &value)
Definition: Approx.h:24
INLINE friend bool operator!=(const Approx &lhs, const Type &rhs)
Definition: Approx.h:42
INLINE friend bool operator==(const Approx &lhs, const Type &rhs)
Definition: Approx.h:34
INLINE friend bool operator!=(const Type &lhs, const Approx &rhs)
Definition: Approx.h:38
INLINE friend bool operator==(const Type &lhs, const Approx &rhs)
Definition: Approx.h:30