cpp_bin_float_ref.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>cpp_bin_float</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&#160;1.&#160;Boost.Multiprecision">
  8. <link rel="up" href="../ref.html" title="Reference">
  9. <link rel="prev" href="mpfr_ref.html" title="mpfr_float_backend">
  10. <link rel="next" href="cpp_dec_ref.html" title="cpp_dec_float">
  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="mpfr_ref.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="cpp_dec_ref.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.ref.cpp_bin_float_ref"></a><a class="link" href="cpp_bin_float_ref.html" title="cpp_bin_float">cpp_bin_float</a>
  28. </h3></div></div></div>
  29. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">multiprecision</span><span class="special">{</span>
  30. <span class="keyword">enum</span> <span class="identifier">digit_base_type</span>
  31. <span class="special">{</span>
  32. <span class="identifier">digit_base_2</span> <span class="special">=</span> <span class="number">2</span><span class="special">,</span>
  33. <span class="identifier">digit_base_10</span> <span class="special">=</span> <span class="number">10</span>
  34. <span class="special">};</span>
  35. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">Digits</span><span class="special">,</span> <span class="identifier">digit_base_type</span> <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">digit_base_10</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span> <span class="special">=</span> <span class="keyword">void</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Exponent</span> <span class="special">=</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">ExponentMin</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">ExponentMax</span> <span class="special">=</span> <span class="number">0</span><span class="special">&gt;</span>
  36. <span class="keyword">class</span> <span class="identifier">cpp_bin_float</span><span class="special">;</span>
  37. <span class="keyword">typedef</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">50</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">cpp_bin_float_50</span><span class="special">;</span>
  38. <span class="keyword">typedef</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">100</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">cpp_bin_float_100</span><span class="special">;</span>
  39. <span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">backends</span><span class="special">::</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="number">24</span><span class="special">,</span> <span class="identifier">backends</span><span class="special">::</span><span class="identifier">digit_base_2</span><span class="special">,</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">,</span> <span class="special">-</span><span class="number">126</span><span class="special">,</span> <span class="number">127</span><span class="special">&gt;,</span> <span class="identifier">et_off</span><span class="special">&gt;</span> <span class="identifier">cpp_bin_float_single</span><span class="special">;</span>
  40. <span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">backends</span><span class="special">::</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">backends</span><span class="special">::</span><span class="identifier">digit_base_2</span><span class="special">,</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">,</span> <span class="special">-</span><span class="number">1022</span><span class="special">,</span> <span class="number">1023</span><span class="special">&gt;,</span> <span class="identifier">et_off</span><span class="special">&gt;</span> <span class="identifier">cpp_bin_float_double</span><span class="special">;</span>
  41. <span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">backends</span><span class="special">::</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="number">64</span><span class="special">,</span> <span class="identifier">backends</span><span class="special">::</span><span class="identifier">digit_base_2</span><span class="special">,</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">,</span> <span class="special">-</span><span class="number">16382</span><span class="special">,</span> <span class="number">16383</span><span class="special">&gt;,</span> <span class="identifier">et_off</span><span class="special">&gt;</span> <span class="identifier">cpp_bin_float_double_extended</span><span class="special">;</span>
  42. <span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">backends</span><span class="special">::</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="number">113</span><span class="special">,</span> <span class="identifier">backends</span><span class="special">::</span><span class="identifier">digit_base_2</span><span class="special">,</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">,</span> <span class="special">-</span><span class="number">16382</span><span class="special">,</span> <span class="number">16383</span><span class="special">&gt;,</span> <span class="identifier">et_off</span><span class="special">&gt;</span> <span class="identifier">cpp_bin_float_quad</span><span class="special">;</span>
  43. <span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">backends</span><span class="special">::</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="number">237</span><span class="special">,</span> <span class="identifier">backends</span><span class="special">::</span><span class="identifier">digit_base_2</span><span class="special">,</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int32_t</span><span class="special">,</span> <span class="special">-</span><span class="number">262142</span><span class="special">,</span> <span class="number">262143</span><span class="special">&gt;,</span> <span class="identifier">et_off</span><span class="special">&gt;</span> <span class="identifier">cpp_bin_float_oct</span><span class="special">;</span>
  44. <span class="special">}}</span> <span class="comment">// namespaces</span>
  45. </pre>
  46. <p>
  47. Class template <code class="computeroutput"><span class="identifier">cpp_bin_float</span></code>
  48. fulfils all of the requirements for a <a class="link" href="backendconc.html" title="Backend Requirements">Backend</a>
  49. type. Its members and non-member functions are deliberately not documented:
  50. these are considered implementation details that are subject to change.
  51. </p>
  52. <p>
  53. The class takes six template parameters:
  54. </p>
  55. <div class="variablelist">
  56. <p class="title"><b></b></p>
  57. <dl class="variablelist">
  58. <dt><span class="term">Digits</span></dt>
  59. <dd><p>
  60. The number of digits precision the type should support. This is normally
  61. expressed as base-10 digits, but that can be changed via the second
  62. template parameter.
  63. </p></dd>
  64. <dt><span class="term">base</span></dt>
  65. <dd><p>
  66. An enumerated value (either <code class="computeroutput"><span class="identifier">digit_base_10</span></code>
  67. or <code class="computeroutput"><span class="identifier">digit_base_2</span></code>) that
  68. indicates whether <code class="computeroutput"><span class="identifier">Digits</span></code>
  69. is base-10 or base-2
  70. </p></dd>
  71. <dt><span class="term">Allocator</span></dt>
  72. <dd><p>
  73. The allocator used: defaults to type <code class="computeroutput"><span class="keyword">void</span></code>,
  74. meaning all storage is within the class, and no dynamic allocation
  75. is performed, but can be set to a standard library allocator if dynamic
  76. allocation makes more sense.
  77. </p></dd>
  78. <dt><span class="term">Exponent</span></dt>
  79. <dd><p>
  80. A signed integer type to use as the type of the exponent - defaults
  81. to <code class="computeroutput"><span class="keyword">int</span></code>.
  82. </p></dd>
  83. <dt><span class="term">ExponentMin</span></dt>
  84. <dd><p>
  85. The smallest (most negative) permitted exponent, defaults to zero,
  86. meaning "define as small as possible given the limitations of
  87. the type and our internal requirements".
  88. </p></dd>
  89. <dt><span class="term">ExponentMax</span></dt>
  90. <dd><p>
  91. The largest (most positive) permitted exponent, defaults to zero, meaning
  92. "define as large as possible given the limitations of the type
  93. and our internal requirements".
  94. </p></dd>
  95. </dl>
  96. </div>
  97. <p>
  98. The type of <code class="computeroutput"><span class="identifier">number_category</span><span class="special">&lt;</span><span class="identifier">cpp_bin_float</span><span class="special">&lt;</span><span class="identifier">Args</span><span class="special">...&gt;</span> <span class="special">&gt;::</span><span class="identifier">type</span></code> is <code class="computeroutput"><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special">&lt;</span><span class="identifier">number_kind_floating_point</span><span class="special">&gt;</span></code>.
  99. </p>
  100. <p>
  101. More information on this type can be found in the <a class="link" href="../tut/floats/cpp_bin_float.html" title="cpp_bin_float">tutorial</a>.
  102. </p>
  103. <h5>
  104. <a name="boost_multiprecision.ref.cpp_bin_float_ref.h0"></a>
  105. <span class="phrase"><a name="boost_multiprecision.ref.cpp_bin_float_ref.implementation_notes"></a></span><a class="link" href="cpp_bin_float_ref.html#boost_multiprecision.ref.cpp_bin_float_ref.implementation_notes">Implementation
  106. Notes</a>
  107. </h5>
  108. <p>
  109. Internally, an N-bit <code class="computeroutput"><span class="identifier">cpp_bin_float</span></code>
  110. is represented as an N-bit unsigned integer along with an exponent and a
  111. sign. The integer part is normalized so that it's most significant bit is
  112. always 1. The decimal point is assumed to be directly after the most significant
  113. bit of the integer part. The special values zero, infinity and NaN all have
  114. the integer part set to zero, and the exponent to one of 3 special values
  115. above the maximum permitted exponent.
  116. </p>
  117. <p>
  118. Multiplication is trivial: multiply the two N-bit integer mantissa's to obtain
  119. a 2N-bit number, then round and adjust the sign and exponent.
  120. </p>
  121. <p>
  122. Addition and subtraction proceed similarly - if the exponents are such that
  123. there is overlap between the two values, then left shift the larger value
  124. to produce a number with between N and 2N bits, then perform integer addition
  125. or subtraction, round, and adjust the exponent.
  126. </p>
  127. <p>
  128. Division proceeds as follows: first scale the numerator by some power of
  129. 2 so that integer division will produce either an N-bit or N+1 bit result
  130. plus a remainder. If we get an N bit result then the size of twice the remainder
  131. compared to the denominator gives us the rounding direction. Otherwise we
  132. have one extra bit in the result which we can use to determine rounding (in
  133. this case ties occur only if the remainder is zero and the extra bit is a
  134. 1).
  135. </p>
  136. <p>
  137. Square root uses integer square root in a manner analogous to division.
  138. </p>
  139. <p>
  140. Decimal string to binary conversion proceeds as follows: first parse the
  141. digits to produce an integer multiplied by a decimal exponent. Note that
  142. we stop parsing digits once we have parsed as many as can possibly effect
  143. the result - this stops the integer part growing too large when there are
  144. a very large number of input digits provided. At this stage if the decimal
  145. exponent is positive then the result is an integer and we can in principle
  146. simply multiply by 10^N to get an exact integer result. In practice however,
  147. that could produce some very large integers. We also need to be able to divide
  148. by 10^N in the event that the exponent is negative. Therefore calculation
  149. of the 10^N values plus the multiplication or division are performed using
  150. limited precision integer arithmetic, plus an exponent, and a track of the
  151. accumulated error. At the end of the calculation we will either be able to
  152. round unambiguously, or the error will be such that we can't tell which way
  153. to round. In the latter case we simply up the precision and try again until
  154. we have an unambiguously rounded result.
  155. </p>
  156. <p>
  157. Binary to decimal conversion proceeds very similarly to the above, our aim
  158. is to calculate <code class="computeroutput"><span class="identifier">mantissa</span> <span class="special">*</span> <span class="number">2</span><span class="special">^</span><span class="identifier">shift</span> <span class="special">*</span> <span class="number">10</span><span class="special">^</span><span class="identifier">E</span></code>
  159. where <code class="computeroutput"><span class="identifier">E</span></code> is the decimal exponent
  160. and <code class="computeroutput"><span class="identifier">shift</span></code> is calculated so
  161. that the result is an N bit integer assuming we want N digits printed in
  162. the result. As before we use limited precision arithmetic to calculate the
  163. result and up the precision as necessary until the result is unambiguously
  164. correctly rounded. In addition our initial calculation of the decimal exponent
  165. may be out by 1, so we have to correct that and loop as well in the that
  166. case.
  167. </p>
  168. </div>
  169. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  170. <td align="left"></td>
  171. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2019 John Maddock
  172. and Christopher Kormanyos<p>
  173. Distributed under the Boost Software License, Version 1.0. (See accompanying
  174. 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>)
  175. </p>
  176. </div></td>
  177. </tr></table>
  178. <hr>
  179. <div class="spirit-nav">
  180. <a accesskey="p" href="mpfr_ref.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="cpp_dec_ref.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  181. </div>
  182. </body>
  183. </html>