• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Literal Types and constexpr Support</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="primetest.html" title="Primality Testing">
10<link rel="next" href="import_export.html" title="Importing and Exporting Data to and from cpp_int and cpp_bin_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="primetest.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="import_export.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.lits"></a><a class="link" href="lits.html" title="Literal Types and constexpr Support">Literal Types and <code class="computeroutput"><span class="keyword">constexpr</span></code> Support</a>
28</h3></div></div></div>
29<p>
30        There are two kinds of <code class="computeroutput"><span class="keyword">constexpr</span></code>
31        support in this library:
32      </p>
33<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
34<li class="listitem">
35            The more basic version requires only C++11 and allow the construction
36            of some number types as literals.
37          </li>
38<li class="listitem">
39            The more advanced support permits constexpr arithmetic and requires at
40            least C++14 constexpr support, and for many operations C++2a support
41          </li>
42</ul></div>
43<h5>
44<a name="boost_multiprecision.tut.lits.h0"></a>
45        <span class="phrase"><a name="boost_multiprecision.tut.lits.declaring_numeric_literals"></a></span><a class="link" href="lits.html#boost_multiprecision.tut.lits.declaring_numeric_literals">Declaring
46        numeric literals</a>
47      </h5>
48<p>
49        There are two backend types which are literals:
50      </p>
51<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
52<li class="listitem">
53            <a class="link" href="floats/float128.html" title="float128">float128</a>
54            (which requires GCC), and
55          </li>
56<li class="listitem">
57            Instantiations of <code class="computeroutput"><span class="identifier">cpp_int_backend</span></code>
58            where the Allocator parameter is type <code class="computeroutput"><span class="keyword">void</span></code>.
59            In addition, prior to C++14 the Checked parameter must be <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">unchecked</span></code>.
60          </li>
61</ul></div>
62<p>
63        For example:
64      </p>
65<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span>
66
67<span class="keyword">constexpr</span> <span class="identifier">float128</span>            <span class="identifier">f</span> <span class="special">=</span> <span class="number">0.1</span><span class="identifier">Q</span>   <span class="comment">// OK, float128's are always literals in C++11</span>
68
69<span class="keyword">constexpr</span> <span class="identifier">int128_t</span>            <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>     <span class="comment">// OK, fixed precision int128_t has no allocator.</span>
70<span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span>          <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xFFFFFFFF00000000uLL</span><span class="special">;</span>  <span class="comment">// OK, fixed precision uint1024_t has no allocator.</span>
71
72<span class="keyword">constexpr</span> <span class="identifier">checked_uint128_t</span>   <span class="identifier">k</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="comment">// OK from C++14 and later, not supported for C++11.</span>
73<span class="keyword">constexpr</span> <span class="identifier">checked_uint128_t</span>   <span class="identifier">k</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">;</span> <span class="comment">// Error, as this would normally lead to a runtime failure (exception).</span>
74<span class="keyword">constexpr</span> <span class="identifier">cpp_int</span>             <span class="identifier">l</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>  <span class="comment">// Error, type is not a literal as it performs memory management.</span>
75</pre>
76<p>
77        There is also support for user defined-literals with <a class="link" href="ints/cpp_int.html" title="cpp_int">cpp_int</a>
78        - these are limited to unchecked, fixed precision <code class="computeroutput"><span class="identifier">cpp_int</span></code>'s
79        which are specified in hexadecimal notation. The suffixes supported are:
80      </p>
81<div class="informaltable"><table class="table">
82<colgroup>
83<col>
84<col>
85</colgroup>
86<thead><tr>
87<th>
88                <p>
89                  Suffix
90                </p>
91              </th>
92<th>
93                <p>
94                  Meaning
95                </p>
96              </th>
97</tr></thead>
98<tbody>
99<tr>
100<td>
101                <p>
102                  _cppi
103                </p>
104              </td>
105<td>
106                <p>
107                  Specifies a value of type: <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>,
108                  where N is chosen to contain just enough digits to hold the number
109                  specified.
110                </p>
111              </td>
112</tr>
113<tr>
114<td>
115                <p>
116                  _cppui
117                </p>
118              </td>
119<td>
120                <p>
121                  Specifies a value of type: <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">unsigned_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>,
122                  where N is chosen to contain just enough digits to hold the number
123                  specified.
124                </p>
125              </td>
126</tr>
127<tr>
128<td>
129                <p>
130                  _cppi<span class="emphasis"><em>N</em></span>
131                </p>
132              </td>
133<td>
134                <p>
135                  Specifies a value of type <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>.
136                </p>
137              </td>
138</tr>
139<tr>
140<td>
141                <p>
142                  _cppui<span class="emphasis"><em>N</em></span>
143                </p>
144              </td>
145<td>
146                <p>
147                  Specifies a value of type <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>.
148                </p>
149              </td>
150</tr>
151</tbody>
152</table></div>
153<p>
154        In each case, use of these suffixes with hexadecimal values produces a <code class="computeroutput"><span class="keyword">constexpr</span></code> result.
155      </p>
156<p>
157        Examples:
158      </p>
159<pre class="programlisting"><span class="comment">// Any use of user defined literals requires that we import the literal-operators into current scope first:</span>
160<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">literals</span><span class="special">;</span>
161<span class="comment">//</span>
162<span class="comment">// To keep things simple in the example, we'll make our types used visible to this scope as well:</span>
163<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span>
164<span class="comment">//</span>
165<span class="comment">// The value zero as a number&lt;cpp_int_backend&lt;4,4,signed_magnitude,unchecked,void&gt; &gt;:</span>
166<span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">0x0</span><span class="identifier">_cppi</span><span class="special">;</span>
167<span class="comment">// The type of each constant has 4 bits per hexadecimal digit,</span>
168<span class="comment">// so this is of type uint256_t (ie number&lt;cpp_int_backend&lt;256,256,unsigned_magnitude,unchecked,void&gt; &gt;):</span>
169<span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui</span><span class="special">;</span>
170<span class="comment">//</span>
171<span class="comment">// Smaller values can be assigned to larger values:</span>
172<span class="identifier">int256_t</span> <span class="identifier">c</span> <span class="special">=</span> <span class="number">0x1234</span><span class="identifier">_cppi</span><span class="special">;</span> <span class="comment">// OK</span>
173<span class="comment">//</span>
174<span class="comment">// However, this only works in constexpr contexts from C++14 onwards:</span>
175<span class="keyword">constexpr</span> <span class="identifier">int256_t</span> <span class="identifier">d</span> <span class="special">=</span> <span class="number">0x1</span><span class="identifier">_cppi</span><span class="special">;</span> <span class="comment">// Compiler error in C++11, requires C++14</span>
176<span class="comment">//</span>
177<span class="comment">// Constants can be padded out with leading zeros to generate wider types:</span>
178<span class="keyword">constexpr</span> <span class="identifier">uint256_t</span> <span class="identifier">e</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">x0000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFF_cppui</span><span class="special">;</span> <span class="comment">// OK</span>
179<span class="comment">//</span>
180<span class="comment">// However, specific-width types are best produced with specific-width suffixes,</span>
181<span class="comment">// ones supported by default are `_cpp[u]i128`, `_cpp[u]i256`, `_cpp[u]i512`, `_cpp[u]i1024`.</span>
182<span class="comment">//</span>
183<span class="keyword">constexpr</span> <span class="identifier">int128_t</span> <span class="identifier">f</span> <span class="special">=</span> <span class="number">0x1234</span><span class="identifier">_cppi128</span><span class="special">;</span> <span class="comment">// OK, always produces an int128_t as the result.</span>
184<span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span> <span class="identifier">g</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccc_cppui1024</span><span class="special">;</span> <span class="comment">// OK,</span>
185<span class="comment">//  always produces an uint1024_t as the result.</span>
186<span class="comment">//</span>
187<span class="comment">// If other specific-width types are required, then there is a macro for generating the operators for these.</span>
188<span class="comment">// The macro can be used at namespace scope only:</span>
189<span class="comment">//</span>
190<span class="identifier">BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL</span><span class="special">(</span><span class="number">2048</span><span class="special">);</span>
191<span class="comment">//</span>
192<span class="comment">// Now we can create 2048-bit literals as well:</span>
193<span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">h</span> <span class="special">=</span> <span class="number">0xff</span><span class="identifier">_cppi2048</span><span class="special">;</span> <span class="comment">// h is of type number&lt;cpp_int_backend&lt;2048,2048,signed_magnitude,unchecked,void&gt; &gt;</span>
194<span class="comment">//</span>
195<span class="comment">// Finally, negative values are handled via the unary minus operator:</span>
196<span class="comment">//</span>
197<span class="keyword">constexpr</span> <span class="identifier">int1024_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="special">-</span><span class="number">0</span><span class="identifier">xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui1024</span><span class="special">;</span>
198<span class="comment">//</span>
199<span class="comment">// Which means this also works:</span>
200<span class="keyword">constexpr</span> <span class="identifier">int1024_t</span> <span class="identifier">j</span> <span class="special">=</span> <span class="special">-</span><span class="identifier">g</span><span class="special">;</span>   <span class="comment">// OK: unary minus operator is constexpr.</span>
201</pre>
202<h5>
203<a name="boost_multiprecision.tut.lits.h1"></a>
204        <span class="phrase"><a name="boost_multiprecision.tut.lits.constexpr_arithmetic"></a></span><a class="link" href="lits.html#boost_multiprecision.tut.lits.constexpr_arithmetic">constexpr
205        arithmetic</a>
206      </h5>
207<p>
208        The front end of the library is all <code class="computeroutput"><span class="keyword">constexpr</span></code>
209        from C++14 and later. Currently there are only two backend types that are
210        <code class="computeroutput"><span class="keyword">constexpr</span></code> aware: __float128
211        and <a class="link" href="ints/cpp_int.html" title="cpp_int">cpp_int</a>.
212        More backends will follow at a later date.
213      </p>
214<p>
215        Provided the compiler is GCC, type <a class="link" href="floats/float128.html" title="float128">float128</a>
216        support <code class="computeroutput"><span class="keyword">constexpr</span></code> operations
217        on all arithmetic operations from C++14, comparisons, <code class="computeroutput"><span class="identifier">abs</span></code>,
218        <code class="computeroutput"><span class="identifier">fabs</span></code>, <code class="computeroutput"><span class="identifier">fpclassify</span></code>,
219        <code class="computeroutput"><span class="identifier">isnan</span></code>, <code class="computeroutput"><span class="identifier">isinf</span></code>,
220        <code class="computeroutput"><span class="identifier">isfinite</span></code> and <code class="computeroutput"><span class="identifier">isnormal</span></code> are also fully supported, but
221        the transcendental functions are not.
222      </p>
223<p>
224        The <a class="link" href="ints/cpp_int.html" title="cpp_int">cpp_int</a>
225        types support constexpr arithmetic, provided it is a fixed precision type
226        with no allocator. It may also be a checked integer: in which case a compiler
227        error will be generated on overflow or undefined behaviour. In addition the
228        free functions <code class="computeroutput"><span class="identifier">abs</span></code>, <code class="computeroutput"><span class="identifier">swap</span></code>, <code class="computeroutput"><span class="identifier">multiply</span></code>,
229        <code class="computeroutput"><span class="identifier">add</span></code>, <code class="computeroutput"><span class="identifier">subtract</span></code>,
230        <code class="computeroutput"><span class="identifier">divide_qr</span></code>, <code class="computeroutput"><span class="identifier">integer_modulus</span></code>, <code class="computeroutput"><span class="identifier">powm</span></code>,
231        <code class="computeroutput"><span class="identifier">lsb</span></code>, <code class="computeroutput"><span class="identifier">msb</span></code>,
232        <code class="computeroutput"><span class="identifier">bit_test</span></code>, <code class="computeroutput"><span class="identifier">bit_set</span></code>,
233        <code class="computeroutput"><span class="identifier">bit_unset</span></code>, <code class="computeroutput"><span class="identifier">bit_flip</span></code>, <code class="computeroutput"><span class="identifier">sqrt</span></code>,
234        <code class="computeroutput"><span class="identifier">gcd</span></code>, <code class="computeroutput"><span class="identifier">lcm</span></code>
235        are all supported. Use of <a class="link" href="ints/cpp_int.html" title="cpp_int">cpp_int</a>
236        in this way requires either a C++2a compiler (one which supports <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">is_constant_evaluated</span><span class="special">()</span></code> - currently only gcc-9 or clang-9 or later),
237        or GCC-6 or later in C++14 mode. Compilers other than GCC and without <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">is_constant_evaluated</span><span class="special">()</span></code> will support a very limited set of operations:
238        expect to hit roadblocks rather easily.
239      </p>
240<p>
241        See <a href="https://en.cppreference.com/w/cpp/compiler_support" target="_top">compiler
242        support</a> for <a href="https://en.cppreference.com/w/cpp/types/is_constant_evaluated" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">is_constant_evaluated</span></code></a>;
243      </p>
244<p>
245        For example given:
246      </p>
247<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// For constant pi with full precision of type T.</span>
248<span class="comment">// using  boost::math::constants::pi;</span>
249
250<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
251<span class="keyword">inline</span> <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="identifier">circumference</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">radius</span><span class="special">)</span>
252<span class="special">{</span>
253   <span class="keyword">return</span> <span class="number">2</span> <span class="special">*</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">;</span>
254<span class="special">}</span>
255
256<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
257<span class="keyword">inline</span> <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="identifier">area</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">radius</span><span class="special">)</span>
258<span class="special">{</span>
259   <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span> <span class="special">*</span> <span class="identifier">radius</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">;</span>
260<span class="special">}</span>
261</pre>
262<p>
263        We can now calculate areas and circumferences, using all compile-time <code class="computeroutput"><span class="keyword">constexpr</span></code> arithmetic:
264      </p>
265<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">float128</span><span class="special">;</span>
266
267<span class="keyword">constexpr</span> <span class="identifier">float128</span> <span class="identifier">radius</span> <span class="special">=</span> <span class="number">2.25</span><span class="special">;</span>
268<span class="keyword">constexpr</span> <span class="identifier">float128</span> <span class="identifier">c</span>      <span class="special">=</span> <span class="identifier">circumference</span><span class="special">(</span><span class="identifier">radius</span><span class="special">);</span>
269<span class="keyword">constexpr</span> <span class="identifier">float128</span> <span class="identifier">a</span>      <span class="special">=</span> <span class="identifier">area</span><span class="special">(</span><span class="identifier">radius</span><span class="special">);</span>
270
271<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Circumference = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">c</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
272<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Area = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">a</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
273</pre>
274<p>
275        Note that these make use of the numeric constants from the <a href="https://www.boost.org/doc/libs/release/libs/math/doc/html/math_toolkit/constants.html" target="_top">Boost.Math
276        constants</a> library, which also happen to be <code class="computeroutput"><span class="keyword">constexpr</span></code>.
277        These usually have the full precision of the floating-point type, here 128-bit,
278        about 36 decimal digits.
279      </p>
280<h6>
281<a name="boost_multiprecision.tut.lits.h2"></a>
282        <span class="phrase"><a name="boost_multiprecision.tut.lits.hermite_poly_coeffics"></a></span><a class="link" href="lits.html#boost_multiprecision.tut.lits.hermite_poly_coeffics">Calculating
283        Hermite Polynomial coefficients at compile time</a>
284      </h6>
285<p>
286        For a more interesting example, in <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../example/constexpr_float_arithmetic_examples.cpp" target="_top">constexpr_float_arithmetic_examples.cpp</a>
287        we define a simple class for <code class="computeroutput"><span class="keyword">constexpr</span></code>
288        polynomial arithmetic:
289      </p>
290<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">Order</span><span class="special">&gt;</span>
291<span class="keyword">struct</span> <span class="identifier">const_polynomial</span><span class="special">;</span>
292</pre>
293<p>
294        Given this, we can use recurrence relations to calculate the coefficients
295        for various orthogonal polynomials - in the example we use the Hermite polynomials.
296        Only the constructor does any work - it uses the recurrence relations to
297        calculate the coefficient array:
298      </p>
299<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">Order</span><span class="special">&gt;</span>
300<span class="keyword">class</span> <span class="identifier">hermite_polynomial</span>
301<span class="special">{</span>
302   <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;</span> <span class="identifier">m_data</span><span class="special">;</span>
303
304 <span class="keyword">public</span><span class="special">:</span>
305   <span class="keyword">constexpr</span> <span class="identifier">hermite_polynomial</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">m_data</span><span class="special">(</span><span class="identifier">hermite_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Order</span> <span class="special">-</span> <span class="number">1</span><span class="special">&gt;().</span><span class="identifier">data</span><span class="special">()</span> <span class="special">*</span> <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">1</span><span class="special">&gt;{</span><span class="number">0</span><span class="special">,</span> <span class="number">2</span><span class="special">}</span> <span class="special">-</span> <span class="identifier">hermite_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Order</span> <span class="special">-</span> <span class="number">1</span><span class="special">&gt;().</span><span class="identifier">data</span><span class="special">().</span><span class="identifier">derivative</span><span class="special">())</span>
306   <span class="special">{</span>
307   <span class="special">}</span>
308   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;&amp;</span> <span class="identifier">data</span><span class="special">()</span> <span class="keyword">const</span>
309   <span class="special">{</span>
310      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">;</span>
311   <span class="special">}</span>
312   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">N</span><span class="special">)</span><span class="keyword">const</span>
313   <span class="special">{</span>
314      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">[</span><span class="identifier">N</span><span class="special">];</span>
315   <span class="special">}</span>
316   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span>
317   <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">U</span> <span class="identifier">val</span><span class="special">)</span><span class="keyword">const</span>
318   <span class="special">{</span>
319      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">(</span><span class="identifier">val</span><span class="special">);</span>
320   <span class="special">}</span>
321<span class="special">};</span>
322</pre>
323<p>
324        Now we just need to define <span class="emphasis"><em>H<sub>0</sub></em></span> and <span class="emphasis"><em>H<sub>1</sub></em></span>
325        as termination conditions for the recurrence:
326      </p>
327<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
328<span class="keyword">class</span> <span class="identifier">hermite_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">0</span><span class="special">&gt;</span>
329<span class="special">{</span>
330   <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">0</span><span class="special">&gt;</span> <span class="identifier">m_data</span><span class="special">;</span>
331
332 <span class="keyword">public</span><span class="special">:</span>
333   <span class="keyword">constexpr</span> <span class="identifier">hermite_polynomial</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">m_data</span><span class="special">{</span><span class="number">1</span><span class="special">}</span> <span class="special">{}</span>
334   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">0</span><span class="special">&gt;&amp;</span> <span class="identifier">data</span><span class="special">()</span> <span class="keyword">const</span>
335   <span class="special">{</span>
336      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">;</span>
337   <span class="special">}</span>
338   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">N</span><span class="special">)</span> <span class="keyword">const</span>
339   <span class="special">{</span>
340      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">[</span><span class="identifier">N</span><span class="special">];</span>
341   <span class="special">}</span>
342   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span>
343   <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">U</span> <span class="identifier">val</span><span class="special">)</span>
344   <span class="special">{</span>
345      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">(</span><span class="identifier">val</span><span class="special">);</span>
346   <span class="special">}</span>
347<span class="special">};</span>
348
349<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
350<span class="keyword">class</span> <span class="identifier">hermite_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">1</span><span class="special">&gt;</span>
351<span class="special">{</span>
352   <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">1</span><span class="special">&gt;</span> <span class="identifier">m_data</span><span class="special">;</span>
353
354 <span class="keyword">public</span><span class="special">:</span>
355   <span class="keyword">constexpr</span> <span class="identifier">hermite_polynomial</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">m_data</span><span class="special">{</span><span class="number">0</span><span class="special">,</span> <span class="number">2</span><span class="special">}</span> <span class="special">{}</span>
356   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">const_polynomial</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="number">1</span><span class="special">&gt;&amp;</span> <span class="identifier">data</span><span class="special">()</span> <span class="keyword">const</span>
357   <span class="special">{</span>
358      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">;</span>
359   <span class="special">}</span>
360   <span class="keyword">constexpr</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">N</span><span class="special">)</span> <span class="keyword">const</span>
361   <span class="special">{</span>
362      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">[</span><span class="identifier">N</span><span class="special">];</span>
363   <span class="special">}</span>
364   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span>
365   <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">U</span> <span class="identifier">val</span><span class="special">)</span>
366   <span class="special">{</span>
367      <span class="keyword">return</span> <span class="identifier">m_data</span><span class="special">(</span><span class="identifier">val</span><span class="special">);</span>
368   <span class="special">}</span>
369<span class="special">};</span>
370</pre>
371<p>
372        We can now declare <span class="emphasis"><em>H<sub>9</sub></em></span> as a <code class="computeroutput"><span class="keyword">constexpr</span></code>
373        object, access the coefficients, and evaluate at an abscissa value, all at
374        compile-time using <code class="computeroutput"><span class="keyword">constexpr</span></code>
375        arithmetic:
376      </p>
377<pre class="programlisting"><span class="keyword">constexpr</span> <span class="identifier">hermite_polynomial</span><span class="special">&lt;</span><span class="identifier">float128</span><span class="special">,</span> <span class="number">9</span><span class="special">&gt;</span> <span class="identifier">h9</span><span class="special">;</span>
378<span class="comment">//</span>
379<span class="comment">// Verify that the polynomial's coefficients match the known values:</span>
380<span class="comment">//</span>
381<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
382<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">==</span> <span class="number">30240</span><span class="special">);</span>
383<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">2</span><span class="special">]</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
384<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">3</span><span class="special">]</span> <span class="special">==</span> <span class="special">-</span><span class="number">80640</span><span class="special">);</span>
385<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">4</span><span class="special">]</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
386<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">5</span><span class="special">]</span> <span class="special">==</span> <span class="number">48384</span><span class="special">);</span>
387<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">6</span><span class="special">]</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
388<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">7</span><span class="special">]</span> <span class="special">==</span> <span class="special">-</span><span class="number">9216</span><span class="special">);</span>
389<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">8</span><span class="special">]</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
390<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">[</span><span class="number">9</span><span class="special">]</span> <span class="special">==</span> <span class="number">512</span><span class="special">);</span>
391<span class="comment">//</span>
392<span class="comment">// Define an abscissa value to evaluate at:</span>
393<span class="keyword">constexpr</span> <span class="identifier">float128</span> <span class="identifier">abscissa</span><span class="special">(</span><span class="number">0.5</span><span class="special">);</span>
394<span class="comment">//</span>
395<span class="comment">// Evaluate H_9(0.5) using all constexpr arithmetic, and check that it has the expected result:</span>
396<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">h9</span><span class="special">(</span><span class="identifier">abscissa</span><span class="special">)</span> <span class="special">==</span> <span class="number">6481</span><span class="special">);</span>
397</pre>
398<p>
399        See <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../example/constexpr_float_arithmetic_examples.cpp" target="_top">constexpr_float_arithmetic_examples.cpp</a>
400        for working code.
401      </p>
402<p>
403        Also since the coefficients to the Hermite polynomials are integers, we can
404        also generate the Hermite coefficients using (fixed precision) <code class="computeroutput"><span class="identifier">cpp_int</span></code>s: see <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../test/constexpr_test_cpp_int_6.cpp" target="_top">constexpr_test_cpp_int_6.cpp</a>.
405      </p>
406<h6>
407<a name="boost_multiprecision.tut.lits.h3"></a>
408        <span class="phrase"><a name="boost_multiprecision.tut.lits.factorial_constexpr"></a></span><a class="link" href="lits.html#boost_multiprecision.tut.lits.factorial_constexpr"><code class="computeroutput"><span class="keyword">constexpr</span></code> Factorials</a>
409      </h6>
410<p>
411        We can also generate integer factorials in <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../test/constexpr_test_cpp_int_5.cpp" target="_top">constexpr_test_cpp_int_5.cpp</a>
412        like so:
413      </p>
414<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
415<span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="identifier">factorial</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">)</span>
416<span class="special">{</span>
417   <span class="keyword">return</span> <span class="identifier">a</span> <span class="special">?</span> <span class="identifier">a</span> <span class="special">*</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">a</span> <span class="special">-</span> <span class="number">1</span><span class="special">)</span> <span class="special">:</span> <span class="number">1</span><span class="special">;</span>
418<span class="special">}</span>
419</pre>
420<p>
421        and validate the result:
422      </p>
423<pre class="programlisting"><span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span> <span class="identifier">f1</span> <span class="special">=</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">uint1024_t</span><span class="special">(</span><span class="number">31</span><span class="special">));</span> <span class="comment">// Factorial 31!</span>
424<span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">f1</span> <span class="special">==</span> <span class="number">0</span><span class="identifier">x1956ad0aae33a4560c5cd2c000000_cppi</span><span class="special">);</span> <span class="comment">// Expected result as an Boost.Multiprecision integer literal. </span>
425</pre>
426<h6>
427<a name="boost_multiprecision.tut.lits.h4"></a>
428        <span class="phrase"><a name="boost_multiprecision.tut.lits.random_constexpr"></a></span><a class="link" href="lits.html#boost_multiprecision.tut.lits.random_constexpr">Random
429        <code class="computeroutput"><span class="keyword">constexpr</span></code> values</a>
430      </h6>
431<p>
432        Another example in <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../test/constexpr_test_cpp_int_7.cpp" target="_top">constexpr_test_cpp_int_7.cpp</a>
433        generates a fresh multiprecision random number each time the file is compiled.
434        It includes an C++ template implementation of the <a href="https://en.wikipedia.org/wiki/KISS_(algorithm)" target="_top">KISS
435        random number algorithm by George Marsaglia</a> for <code class="computeroutput"><span class="identifier">cpp_int</span></code>
436        integers.
437      </p>
438<pre class="programlisting"><span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span> <span class="identifier">rand</span> <span class="special">=</span> <span class="identifier">nth_random_value</span><span class="special">&lt;</span><span class="identifier">uint1024_t</span><span class="special">&gt;(</span><span class="number">1000</span><span class="special">);</span>
439<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special">&lt;&lt;</span> <span class="identifier">rand</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
440</pre>
441<p>
442        See also the <a class="link" href="random.html" title="Generating Random Numbers">random number
443        generation</a> section.
444      </p>
445</div>
446<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
447<td align="left"></td>
448<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
449      Maddock and Christopher Kormanyos<p>
450        Distributed under the Boost Software License, Version 1.0. (See accompanying
451        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>)
452      </p>
453</div></td>
454</tr></table>
455<hr>
456<div class="spirit-nav">
457<a accesskey="p" href="primetest.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="import_export.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
458</div>
459</body>
460</html>
461