Opened 8 years ago
Closed 20 months ago
#11056 closed Bugs (fixed)
karma real generator does not produce enough digits to guarantee round trip through spirit
Reported by: | Owned by: | Joel de Guzman | |
---|---|---|---|
Milestone: | To Be Determined | Component: | spirit |
Version: | Boost 1.57.0 | Severity: | Problem |
Keywords: | karma, real_generator | Cc: |
Description
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:
// 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).
Change History (2)
comment:1 by , 8 years ago
Keywords: | karma real_generator added |
---|
comment:2 by , 20 months ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
fixed in #586 https://github.com/boostorg/spirit/pull/586