npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
absDifference.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSDIFFERENCE_HH_
2 #define NPSTAT_ABSDIFFERENCE_HH_
3 
4 /*!
5 // \file absDifference.hh
6 //
7 // \brief Calculate absolute value of a difference between two numbers
8 // for an extended set of types
9 //
10 // Author: I. Volobouev
11 //
12 // October 2009
13 */
14 
15 #include <cmath>
16 #include <complex>
17 
18 #include "geners/IOIsUnsigned.hh"
19 
20 namespace npstat {
21  namespace Private {
22  template <typename T>
24  {
25  typedef T type;
26  };
27 
28  template <typename T>
29  struct AbsReturnType<std::complex<T> >
30  {
31  typedef T type;
32  };
33 
34  template <typename T>
35  struct AbsReturnType<const std::complex<T> >
36  {
37  typedef T type;
38  };
39 
40  template <typename T>
41  struct AbsReturnType<volatile std::complex<T> >
42  {
43  typedef T type;
44  };
45 
46  template <typename T>
47  struct AbsReturnType<const volatile std::complex<T> >
48  {
49  typedef T type;
50  };
51 
52  // Signed type
53  template <typename T, int Unsigned=0>
54  struct AbsHelper
55  {
56  typedef typename Private::AbsReturnType<T>::type return_type;
57 
58  inline static return_type delta(const T& v1, const T& v2)
59  {return std::abs(v1 - v2);}
60 
61  inline static return_type value(const T& v1)
62  {return std::abs(v1);}
63  };
64 
65  // Unsigned type
66  template <typename T>
67  struct AbsHelper<T, 1>
68  {
69  typedef typename Private::AbsReturnType<T>::type return_type;
70 
71  inline static return_type delta(const T& v1, const T& v2)
72  {return v1 > v2 ? v1 - v2 : v2 - v1;}
73 
74  inline static return_type value(const T& v1)
75  {return v1;}
76  };
77  }
78 
79  /**
80  // Absolute value of the difference between two numbers.
81  // Works for all standard numeric types, including unsigned and complex.
82  */
83  template<typename T>
84  inline typename Private::AbsReturnType<T>::type
85  absDifference(const T& v1, const T& v2)
86  {
88  }
89 
90  /**
91  // Absolute value of a number.
92  // Works for all standard numeric types, including unsigned and complex.
93  */
94  template<typename T>
95  inline typename Private::AbsReturnType<T>::type
96  absValue(const T& v1)
97  {
99  }
100 }
101 
102 #endif // NPSTAT_ABSDIFFERENCE_HH_
Definition: AbsArrayProjector.hh:14
Private::AbsReturnType< T >::type absValue(const T &v1)
Definition: absDifference.hh:96
Private::AbsReturnType< T >::type absDifference(const T &v1, const T &v2)
Definition: absDifference.hh:85
Definition: absDifference.hh:55
Definition: absDifference.hh:24