faif
Power.hpp
1 #ifndef FAIF_UTILS_POWER
2 #define FAIF_UTILS_POWER
3 
4 namespace faif {
5 
6  /** the template metaprogramming for calculating power of double,
7  based on sqare and multiply method, i.e. if n = 2^m (n is m-th power of 2),
8  x^n = (...((x^2)^2)...)^2 (m-times)
9  */
10  template <unsigned n> double int_power(double x) {
11  return int_power<2>( int_power<n/2>(x) )*int_power<n%2>(x);
12  }
13 
14  /** the specialisation for power = 2 */
15  template <> double int_power<2>(double x) {
16  return x*x;
17  }
18 
19  /** the specialisation for power = 1 */
20  template <> double int_power<1>(double x) {
21  return x;
22  }
23 
24  /** the template specialisation for power = 0 */
25  template <> double int_power<0>(double) {
26  return 1.0;
27  }
28 }
29 
30 
31 #endif
Definition: Chain.h:17