Ticket #9405: spirit_karma_generator_bug.cxx

File spirit_karma_generator_bug.cxx, 929 bytes (added by Josef Zlomek <josef@…>, 9 years ago)

Test that prints the number incorrectly

Line 
1#include <string>
2#include <iostream>
3#include <boost/spirit/include/karma.hpp>
4
5/** Custom policy for floating point number generators */
6template<typename T>
7struct real_policy : boost::spirit::karma::real_policies<T>
8{
9 static int floatfield(T)
10 {
11 return boost::spirit::karma::real_policies<T>::fmtflags::fixed;
12 }
13
14 static unsigned int precision(T)
15 {
16 return std::numeric_limits<T>::digits10 + 1;
17 }
18
19};
20
21void write_double(std::back_insert_iterator<std::string> &sink, double &value)
22{
23 boost::spirit::karma::real_generator<double, real_policy<double> > generator;
24 boost::spirit::karma::generate(sink, generator, value);
25}
26
27int main()
28{
29 std::string s;
30 s.reserve(100);
31 std::back_insert_iterator<std::string> sink(s);
32
33 double value = 0.09999999999999987;
34 write_double(sink, value);
35 std::cout << s << " != " << value << std::endl;
36 return 0;
37}