| 41 | // Seminumerical Algorithms, Section 4.6.3 |
| 42 | template <typename T, typename Integer> |
| 43 | typename boost::enable_if<boost::is_integral<Integer>, T>::type |
| 44 | power (T x, Integer n) { |
| 45 | T y = 1; // Should be "T y{1};" |
| 46 | if (n == 0) return y; |
| 47 | while (true) { |
| 48 | if (n % 2 == 1) { |
| 49 | y = x * y; |
| 50 | if (n == 1) |
| 51 | return y; |
| 52 | } |
| 53 | n = n / 2; |
| 54 | x = x * x; |
| 55 | } |
| 56 | return y; |
| 57 | } |
| 58 | |
| 59 | /// \fn power ( T x, Integer n, Operation op ) |
| 60 | /// \return the value "x" raised to the power "n" |