weighted_sum_kahan.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // (C) Copyright Simon West 2011.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/test/unit_test.hpp>
  6. #include <boost/accumulators/accumulators.hpp>
  7. #include <boost/accumulators/statistics/stats.hpp>
  8. #include <boost/accumulators/statistics/weighted_sum_kahan.hpp>
  9. #include <boost/accumulators/statistics/variates/covariate.hpp>
  10. using namespace boost;
  11. using namespace unit_test;
  12. using namespace accumulators;
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // test_weighted_sum_kahan
  15. //
  16. void test_weighted_sum_kahan()
  17. {
  18. accumulator_set<float, stats<tag::weighted_sum_kahan>, float > acc;
  19. BOOST_CHECK_EQUAL(0.0f, weighted_sum_kahan(acc));
  20. for (size_t i = 0; i < 1e6; ++i) {
  21. acc(1, weight = 1e-6f);
  22. }
  23. BOOST_CHECK_EQUAL(1.0f, weighted_sum_kahan(acc));
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // test_weighted_sum_of_variates_kahan
  27. //
  28. void test_weighted_sum_of_variates_kahan()
  29. {
  30. accumulator_set<
  31. float,
  32. stats<tag::weighted_sum_of_variates_kahan<float, tag::covariate1> >,
  33. float
  34. >
  35. acc;
  36. BOOST_CHECK_EQUAL(0.0f, weighted_sum_of_variates_kahan(acc));
  37. for (size_t i = 0; i < 1e6; ++i) {
  38. acc(0, weight = 1.0f, covariate1 = 1e-6f);
  39. }
  40. BOOST_CHECK_EQUAL(1.0f, weighted_sum_of_variates_kahan(acc));
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // init_unit_test_suite
  44. //
  45. test_suite* init_unit_test_suite( int argc, char* argv[] )
  46. {
  47. test_suite *test = BOOST_TEST_SUITE("weighted sum kahan tests");
  48. test->add(BOOST_TEST_CASE(&test_weighted_sum_kahan));
  49. test->add(BOOST_TEST_CASE(&test_weighted_sum_of_variates_kahan));
  50. return test;
  51. }