1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2010 Gaetano Mendola, 2011 Simon West. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_SUM_KAHAN_HPP_EAN_26_07_2010
9 #define BOOST_ACCUMULATORS_STATISTICS_SUM_KAHAN_HPP_EAN_26_07_2010
11 #include <boost/accumulators/framework/accumulator_base.hpp>
12 #include <boost/accumulators/framework/parameters/sample.hpp>
13 #include <boost/accumulators/statistics_fwd.hpp>
14 #include <boost/accumulators/statistics/sum.hpp>
15 #include <boost/accumulators/statistics/weighted_sum_kahan.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
18 namespace boost { namespace accumulators
25 # pragma float_control(push)
26 # pragma float_control(precise, on)
29 template<typename Sample, typename Tag>
33 typedef Sample result_type;
35 ////////////////////////////////////////////////////////////////////////////
38 @brief Kahan summation algorithm
40 The Kahan summation algorithm reduces the numerical error obtained with standard
44 template<typename Args>
45 sum_kahan_impl(Args const & args)
46 : sum(args[parameter::keyword<Tag>::get() | Sample()]),
47 compensation(boost::numeric_cast<Sample>(0.0))
51 template<typename Args>
53 #if BOOST_ACCUMULATORS_GCC_VERSION > 40305
54 __attribute__((__optimize__("no-associative-math")))
56 operator ()(Args const & args)
58 const Sample myTmp1 = args[parameter::keyword<Tag>::get()] - this->compensation;
59 const Sample myTmp2 = this->sum + myTmp1;
60 this->compensation = (myTmp2 - this->sum) - myTmp1;
64 result_type result(dont_care) const
75 # pragma float_control(pop)
80 ///////////////////////////////////////////////////////////////////////////////
82 // tag::sum_of_weights_kahan
83 // tag::sum_of_variates_kahan
93 typedef impl::sum_kahan_impl< mpl::_1, tag::sample > impl;
96 struct sum_of_weights_kahan
99 typedef mpl::true_ is_weight_accumulator;
102 typedef accumulators::impl::sum_kahan_impl<mpl::_2, tag::weight> impl;
105 template<typename VariateType, typename VariateTag>
106 struct sum_of_variates_kahan
111 typedef mpl::always<accumulators::impl::sum_kahan_impl<VariateType, VariateTag> > impl;
116 ///////////////////////////////////////////////////////////////////////////////
117 // extract::sum_kahan
118 // extract::sum_of_weights_kahan
119 // extract::sum_of_variates_kahan
123 extractor<tag::sum_kahan> const sum_kahan = {};
124 extractor<tag::sum_of_weights_kahan> const sum_of_weights_kahan = {};
125 extractor<tag::abstract_sum_of_variates> const sum_of_variates_kahan = {};
127 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_kahan)
128 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights_kahan)
129 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates_kahan)
130 } // namespace extract
132 using extract::sum_kahan;
133 using extract::sum_of_weights_kahan;
134 using extract::sum_of_variates_kahan;
136 // sum(kahan) -> sum_kahan
138 struct as_feature<tag::sum(kahan)>
140 typedef tag::sum_kahan type;
143 // sum_of_weights(kahan) -> sum_of_weights_kahan
145 struct as_feature<tag::sum_of_weights(kahan)>
147 typedef tag::sum_of_weights_kahan type;
150 // So that sum_kahan can be automatically substituted with
151 // weighted_sum_kahan when the weight parameter is non-void.
153 struct as_weighted_feature<tag::sum_kahan>
155 typedef tag::weighted_sum_kahan type;
159 struct feature_of<tag::weighted_sum_kahan>
160 : feature_of<tag::sum>
163 // for the purposes of feature-based dependency resolution,
164 // sum_kahan provides the same feature as sum
166 struct feature_of<tag::sum_kahan>
167 : feature_of<tag::sum>
171 // for the purposes of feature-based dependency resolution,
172 // sum_of_weights_kahan provides the same feature as sum_of_weights
174 struct feature_of<tag::sum_of_weights_kahan>
175 : feature_of<tag::sum_of_weights>
179 template<typename VariateType, typename VariateTag>
180 struct feature_of<tag::sum_of_variates_kahan<VariateType, VariateTag> >
181 : feature_of<tag::abstract_sum_of_variates>
185 }} // namespace boost::accumulators