DGtal  1.5.beta
BasicMathFunctions.h
1 
17 #pragma once
18 
33 #if defined(BasicMathFunctions_RECURSES)
34 #error Recursive header files inclusion detected in BasicMathFunctions.h
35 #else // defined(BasicMathFunctions_RECURSES)
37 #define BasicMathFunctions_RECURSES
38 
39 #if !defined BasicMathFunctions_h
41 #define BasicMathFunctions_h
42 
44 // Inclusions
45 #include <algorithm>
46 #include <functional>
47 #include "DGtal/base/Common.h"
48 #include "DGtal/base/Bits.h"
49 #include "DGtal/kernel/NumberTraits.h"
51 
52 namespace DGtal
53 {
54  namespace functions
55  {
56 
72  template<typename T>
73  T power(const T&aVal, const unsigned int exponent)
74  {
75  unsigned int q=exponent;
76  T p(aVal);
77 
78  if (exponent == 0) { return 1; }
79 
80  T result = NumberTraits<T>::ONE;
81  while (q != 0)
82  {
83  if (q % 2 == 1) { // q is odd
84  result *= p;
85  q--;
86  }
87  p *= p;
88  q /= 2;
89  }
90  return result;
91  }
92 
93 
101  template<typename T>
102  T roundToUpperPowerOfTwo(const T &n){
103  return (T) 1 << (1+DGtal::Bits::mostSignificantBit( (unsigned int) n-1 ) );
104  }
105 
115  template<typename T>
116  T abs(const T & a)
117  {
118  BOOST_CONCEPT_ASSERT((boost::LessThanComparable<T>));
119  if (a<0)
120  return -a;
121  else
122  return a;
123  }
124 
131  template <typename T>
132  inline
133  T square( T x )
134  { return x * x; }
135 
142  template <typename T>
143  inline
144  T cube( T x )
145  { return x * x * x; }
146 
147  } // namespace functions
148 } // namespace DGTal
149 
150 
151 
153 
154 #endif // !defined BasicMathFunctions_h
155 
156 #undef BasicMathFunctions_RECURSES
157 #endif // else defined(BasicMathFunctions_RECURSES)
T power(const T &aVal, const unsigned int exponent)
T roundToUpperPowerOfTwo(const T &n)
DGtal is the top-level namespace which contains all DGtal functions and types.
static unsigned int mostSignificantBit(DGtal::uint8_t n)
Definition: Bits.h:343
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:564
Go to http://www.sgi.com/tech/stl/LessThanComparable.html.
Definition: Boost.dox:48