regression_real_scientific.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (c) 2010 Lars Kielhorn
  2. // Copyright (c) 2001-2010 Hartmut Kaiser
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config/warning_disable.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/karma.hpp>
  9. #include <iostream>
  10. #include <string>
  11. #include <iterator>
  12. namespace karma = boost::spirit::karma;
  13. // define a new real number formatting policy
  14. template <typename Num>
  15. struct scientific_policy : karma::real_policies<Num>
  16. {
  17. // we want the numbers always to be in scientific format
  18. static int floatfield(Num) { return std::ios_base::scientific; }
  19. };
  20. int main()
  21. {
  22. // define a new generator type based on the new policy
  23. typedef karma::real_generator<double, scientific_policy<double> >
  24. science_type;
  25. science_type const scientific = science_type();
  26. std::string output;
  27. typedef std::back_insert_iterator<std::string> output_iterator;
  28. output_iterator sink(output);
  29. // should output: 1.0e-01, but will output: 10.0e-02
  30. BOOST_TEST(karma::generate(sink, scientific, 0.1) && output == "1.0e-01");
  31. return boost::report_errors();
  32. }