[Boost-bugs] [Boost C++ Libraries] #11056: karma real generator does not produce enough digits to guarantee round trip through spirit

Subject: [Boost-bugs] [Boost C++ Libraries] #11056: karma real generator does not produce enough digits to guarantee round trip through spirit
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-02-25 05:01:34


#11056: karma real generator does not produce enough digits to guarantee round trip
through spirit
------------------------------+---------------------
 Reporter: reallyasi9@… | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost 1.57.0 | Severity: Problem
 Keywords: |
------------------------------+---------------------
 The real_inserter::call_n method in
 [http://svn.boost.org/svn/boost/trunk/boost/spirit/home/karma/numeric/detail/real_utils.hpp]
 limits the number of output characters representing a real (floating-
 point) number of type U to std::numeric_limits<U>::digits10. However,
 digits10 represents the number of decimal digits that are guaranteed to
 survive a round-trip conversion for the type. This is not the same as the
 number of decimal digits that a type can represent, which is
 std::numeric_limits<U>::max_digits10 (in c++11).

 The following code describes the problem when attempting to round-trip
 using karma and spirit vs. using stringstreams, picking a particular value
 that fails:

 {{{
 #!c++
 // define a new real number formatting policy
 struct precise_policy : real_policies<float>
 {
   // Limited to digits10, no matter what I put here
   static unsigned int precision(float) {
     return std::numeric_limits<float>::max_digits10;
   }
 };

 float a = 135.477005;

 std::stringstream ss;
 ss.precision(std::numeric_limits<float>::max_digits10);
 ss << a;
 float b;
 ss >> b;

 assert(a == b); // succeeds

 std::string st;
 typedef std::back_insert_iterator<std::string> sink_type;
 sink_type sink(st);
 typedef real_generator<float, precise_policy> precise_type;
 precise_type const precise = precise_type();
 generate(sink, precise, a);

 float c;
 parse(st.begin(), st.end(), float_, c);

 assert(a == c); // fails, even though enough digits were requested
                 // with precise_policy!
 }}}

 One solution would be to change the maximum number of digits to
 std::numeric_limits<U>::max_digits10 (or the equivalent in non-c++11
 code).

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11056>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:17 UTC