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
This commit is contained in:
parent
c0601bfc7c
commit
277dfaafae
31
maths.hpp
31
maths.hpp
@ -28,6 +28,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <numeric>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@ -469,13 +470,16 @@ namespace util {
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// kahan summation for long floating point sequences
|
// kahan summation for long floating point sequences
|
||||||
|
|
||||||
template <class InputIt>
|
template <class InputT>
|
||||||
typename std::iterator_traits<InputIt>::value_type
|
std::enable_if_t<
|
||||||
fsum (InputIt first, InputIt last)
|
std::is_floating_point<
|
||||||
|
typename std::iterator_traits<InputT>::value_type
|
||||||
|
>::value,
|
||||||
|
typename std::iterator_traits<InputT>::value_type
|
||||||
|
>
|
||||||
|
sum (InputT first, InputT last)
|
||||||
{
|
{
|
||||||
using T = typename std::iterator_traits<InputIt>::value_type;
|
using T = typename std::iterator_traits<InputT>::value_type;
|
||||||
static_assert (std::is_floating_point<T>::value,
|
|
||||||
"fsum only works for floating point types");
|
|
||||||
|
|
||||||
T sum = 0;
|
T sum = 0;
|
||||||
T c = 0;
|
T c = 0;
|
||||||
@ -491,6 +495,21 @@ namespace util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <class InputT>
|
||||||
|
std::enable_if_t<
|
||||||
|
std::is_integral<
|
||||||
|
typename std::iterator_traits<InputT>::value_type
|
||||||
|
>::value,
|
||||||
|
typename std::iterator_traits<InputT>::value_type
|
||||||
|
>
|
||||||
|
sum (InputT first, InputT last)
|
||||||
|
{
|
||||||
|
using T = typename std::iterator_traits<InputT>::value_type;
|
||||||
|
return std::accumulate (first, last, T{0});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
/// Variadic minimum
|
/// Variadic minimum
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
Loading…
Reference in New Issue
Block a user