template <class _Tp> inline _Tp identity_element(plus<_Tp>) { return _Tp(0); } template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) { return _Tp(1); }
template <class _Tp, class _Integer, class _MonoidOperation? > _Tp power(_Tp __x, _Integer __n, _MonoidOperation? __monoid_op) { if (__n == 0) return identity_element(__monoid_op); else { while ((__n & 1) == 0) { __n >>= 1; __x = __monoid_op(__x, __x); }
_Tp __result = __x; __n >>= 1; while (__n != 0) { __x = __monoid_op(__x, __x); if ((__n & 1) != 0) __result = __monoid_op(__result, __x); __n >>= 1; } return __result; } }
template <class _Tp, class _Integer> inline _Tp power(_Tp __x, _Integer __n) { return power(__x, __n, multiplies<_Tp>()); }
I have a few variations on std::accumulate that reorder the elements first to minimize round off errors in the accumulation. However, they are obviously only useful for commutative situations. Is there any interest in them?
John Phillips