• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Input Output</title>
5<link rel="stylesheet" href="../../multiprecision.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision">
8<link rel="up" href="../tut.html" title="Tutorial">
9<link rel="prev" href="limits/how_to_tell.html" title="How to Determine the Kind of a Number From std::numeric_limits">
10<link rel="next" href="hash.html" title="Hash Function Support">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="limits/how_to_tell.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hash.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_multiprecision.tut.input_output"></a><a class="link" href="input_output.html" title="Input Output">Input Output</a>
28</h3></div></div></div>
29<h5>
30<a name="boost_multiprecision.tut.input_output.h0"></a>
31        <span class="phrase"><a name="boost_multiprecision.tut.input_output.loopback_testing"></a></span><a class="link" href="input_output.html#boost_multiprecision.tut.input_output.loopback_testing">Loopback
32        testing</a>
33      </h5>
34<p>
35        <span class="emphasis"><em>Loopback</em></span> or <span class="emphasis"><em>round-tripping</em></span> refers
36        to writing out a value as a decimal digit string using <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">iostream</span></code>,
37        usually to a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code>, and then reading the string
38        back in to another value, and confirming that the two values are identical.
39        A trivial example using <code class="computeroutput"><span class="keyword">float</span></code>
40        is:
41      </p>
42<pre class="programlisting"><span class="keyword">float</span> <span class="identifier">write</span><span class="special">;</span> <span class="comment">// Value to round-trip.</span>
43<span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span> <span class="identifier">ss</span><span class="special">;</span>  <span class="comment">// Read and write std::stringstream.</span>
44<span class="identifier">ss</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">max_digits10</span><span class="special">);</span>  <span class="comment">// Ensure all potentially significant bits are output.</span>
45<span class="identifier">ss</span><span class="special">.</span><span class="identifier">flags</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">fmtflags</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">scientific</span><span class="special">));</span> <span class="comment">// Use scientific format.</span>
46<span class="identifier">ss</span> <span class="special">&lt;&lt;</span> <span class="identifier">write</span><span class="special">;</span> <span class="comment">// Output to string.</span>
47<span class="keyword">float</span> <span class="identifier">read</span><span class="special">;</span>  <span class="comment">// Expected.</span>
48<span class="identifier">ss</span> <span class="special">&gt;&gt;</span> <span class="identifier">read</span><span class="special">;</span> <span class="comment">// Read decimal digits string from stringstream.</span>
49<span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">write</span><span class="special">,</span> <span class="identifier">read</span><span class="special">);</span> <span class="comment">// Should be the same.</span>
50</pre>
51<p>
52        and this can be run in a loop for all possible values of a 32-bit float.
53        For other floating-point types <code class="computeroutput"><span class="identifier">T</span></code>,
54        including <a href="https://en.cppreference.com/w/cpp/language/types" target="_top">fundamental
55        (built-in)</a> <code class="computeroutput"><span class="keyword">double</span></code>, it
56        takes far too long to test all values, so a reasonable test strategy is to
57        use a large number of random values.
58      </p>
59<pre class="programlisting"><span class="identifier">T</span> <span class="identifier">write</span><span class="special">;</span>
60<span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span> <span class="identifier">ss</span><span class="special">;</span>
61<span class="identifier">ss</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">max_digits10</span><span class="special">);</span>  <span class="comment">// Ensure all potentially significant bits are output.</span>
62<span class="identifier">ss</span><span class="special">.</span><span class="identifier">flags</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span> <span class="comment">// Changed from default iostream format flags if desired.</span>
63<span class="identifier">ss</span> <span class="special">&lt;&lt;</span> <span class="identifier">write</span><span class="special">;</span> <span class="comment">// Output to stringstream.</span>
64
65<span class="identifier">T</span> <span class="identifier">read</span><span class="special">;</span>
66<span class="identifier">ss</span> <span class="special">&gt;&gt;</span> <span class="identifier">read</span><span class="special">;</span> <span class="comment">// Get read using operator&gt;&gt; from stringstream.</span>
67<span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">read</span><span class="special">,</span> <span class="identifier">write</span><span class="special">);</span>
68
69<span class="identifier">read</span> <span class="special">=</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">ss</span><span class="special">.</span><span class="identifier">str</span><span class="special">());</span> <span class="comment">// Get read by converting from decimal digits string representation of write.</span>
70<span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">read</span><span class="special">,</span> <span class="identifier">write</span><span class="special">);</span>
71
72<span class="identifier">read</span> <span class="special">=</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">write</span><span class="special">.</span><span class="identifier">str</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">f</span><span class="special">));</span>  <span class="comment">// Get read using format specified when written.</span>
73<span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">read</span><span class="special">,</span> <span class="identifier">write</span><span class="special">);</span>
74</pre>
75<p>
76        The test at <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../test/test_cpp_bin_float_io.cpp" target="_top">test_cpp_bin_float_io.cpp</a>
77        allows any floating-point type to be <span class="emphasis"><em>round_tripped</em></span> using
78        a wide range of fairly random values. It also includes tests compared a collection
79        of <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../test/string_data.ipp" target="_top">stringdata</a> test cases
80        in a file.
81      </p>
82<h5>
83<a name="boost_multiprecision.tut.input_output.h1"></a>
84        <span class="phrase"><a name="boost_multiprecision.tut.input_output.comparing_with_output_using_fund"></a></span><a class="link" href="input_output.html#boost_multiprecision.tut.input_output.comparing_with_output_using_fund">Comparing
85        with output using fundamental
86        (built-in) types</a>
87      </h5>
88<p>
89        One can make some comparisons with the output of
90      </p>
91<pre class="programlisting"><span class="special">&lt;</span><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="number">53</span><span class="special">,</span> <span class="identifier">digit_count_2</span><span class="special">&gt;</span> <span class="special">&gt;</span>
92</pre>
93<p>
94        which has the same number of significant bits (53) as 64-bit double precision
95        floating-point.
96      </p>
97<p>
98        However, although most outputs are identical, there are differences on some
99        platforms caused by the implementation-dependent behaviours allowed by the
100        C99 specification <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf" target="_top">C99
101        ISO/IEC 9899:TC2</a>, incorporated by C++.
102      </p>
103<div class="blockquote"><blockquote class="blockquote"><p>
104          <span class="emphasis"><em>"For e, E, f, F, g, and G conversions, if the number of
105          significant decimal digits is at most DECIMAL_DIG, then the result should
106          be correctly rounded. If the number of significant decimal digits is more
107          than DECIMAL_DIG but the source value is exactly representable with DECIMAL_DIG
108          digits, then the result should be an exact representation with trailing
109          zeros. Otherwise, the source value is bounded by two adjacent decimal strings
110          L &lt; U, both having DECIMAL_DIG significant digits; the value of the
111          resultant decimal string D should satisfy L&lt;= D &lt;= U, with the extra
112          stipulation that the error should have a correct sign for the current rounding
113          direction."</em></span>
114        </p></blockquote></div>
115<p>
116        So not only is correct rounding for the full number of digits not required,
117        but even if the <span class="bold"><strong>optional</strong></span> recommended practice
118        is followed, then the value of these last few digits is unspecified as long
119        as the value is within certain bounds.
120      </p>
121<div class="note"><table border="0" summary="Note">
122<tr>
123<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
124<th align="left">Note</th>
125</tr>
126<tr><td align="left" valign="top"><p>
127          Do not expect the output from different platforms to be <span class="bold"><strong>identical</strong></span>,
128          but <code class="computeroutput"><span class="identifier">cpp_dec_float</span></code>, <code class="computeroutput"><span class="identifier">cpp_bin_float</span></code> (and other backends) outputs
129          should be correctly rounded to the number of digits requested by the set
130          precision and format.
131        </p></td></tr>
132</table></div>
133<h5>
134<a name="boost_multiprecision.tut.input_output.h2"></a>
135        <span class="phrase"><a name="boost_multiprecision.tut.input_output.macro_boost_mp_min_exponent_digi"></a></span><a class="link" href="input_output.html#boost_multiprecision.tut.input_output.macro_boost_mp_min_exponent_digi">Macro
136        BOOST_MP_MIN_EXPONENT_DIGITS</a>
137      </h5>
138<p>
139        <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf" target="_top">C99
140        Standard</a> for format specifiers, 7.19.6 Formatted input/output functions
141        requires:
142      </p>
143<p>
144        "The exponent always contains at least two digits, and only as many
145        more digits as necessary to represent the exponent."
146      </p>
147<p>
148        So to conform to the C99 standard (incorporated by C++)
149      </p>
150<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MP_MIN_EXPONENT_DIGITS</span> <span class="number">2</span>
151</pre>
152<p>
153        Confusingly, Microsoft (and MinGW) do not conform to this standard and provide
154        <span class="bold"><strong>at least three digits</strong></span>, for example <code class="computeroutput"><span class="number">1e+001</span></code>. So if you want the output to match
155        that from <a href="https://en.cppreference.com/w/cpp/language/types" target="_top">fundamental
156        (built-in)</a> floating-point types on compilers that use Microsofts
157        runtime then use:
158      </p>
159<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MP_MIN_EXPONENT_DIGITS</span> <span class="number">3</span>
160</pre>
161<p>
162        Also useful to get the minimum exponent field width is
163      </p>
164<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MP_MIN_EXPONENT_DIGITS</span> <span class="number">1</span>
165</pre>
166<p>
167        producing a compact output like <code class="computeroutput"><span class="number">2e+4</span></code>,
168        useful when conserving space is important.
169      </p>
170<p>
171        Larger values are also supported, for example, value 4 for <code class="computeroutput"><span class="number">2e+0004</span></code> which may be useful to ensure that
172        columns line up.
173      </p>
174</div>
175<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
176<td align="left"></td>
177<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
178      Maddock and Christopher Kormanyos<p>
179        Distributed under the Boost Software License, Version 1.0. (See accompanying
180        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
181      </p>
182</div></td>
183</tr></table>
184<hr>
185<div class="spirit-nav">
186<a accesskey="p" href="limits/how_to_tell.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hash.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
187</div>
188</body>
189</html>
190