From 277dfaafaecf0cc3372d4276e66e1e931dcb6a2c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 15 Aug 2016 18:42:43 +1000 Subject: [PATCH] maths: add integer summation path for fsum this allows us to simplify users of fsum which may not care what value_type they're passing for summation --- maths.hpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/maths.hpp b/maths.hpp index 426b5b5e..6513242c 100644 --- a/maths.hpp +++ b/maths.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -469,13 +470,16 @@ namespace util { /////////////////////////////////////////////////////////////////////////////// // kahan summation for long floating point sequences - template - typename std::iterator_traits::value_type - fsum (InputIt first, InputIt last) + template + std::enable_if_t< + std::is_floating_point< + typename std::iterator_traits::value_type + >::value, + typename std::iterator_traits::value_type + > + sum (InputT first, InputT last) { - using T = typename std::iterator_traits::value_type; - static_assert (std::is_floating_point::value, - "fsum only works for floating point types"); + using T = typename std::iterator_traits::value_type; T sum = 0; T c = 0; @@ -491,6 +495,21 @@ namespace util { } + //------------------------------------------------------------------------- + template + std::enable_if_t< + std::is_integral< + typename std::iterator_traits::value_type + >::value, + typename std::iterator_traits::value_type + > + sum (InputT first, InputT last) + { + using T = typename std::iterator_traits::value_type; + return std::accumulate (first, last, T{0}); + } + + /////////////////////////////////////////////////////////////////////////// /// Variadic minimum template