72e469da0a
[CI SKIP]
146 lines
19 KiB
HTML
146 lines
19 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Using without expression templates for Boost.Test and others</title>
|
|
<link rel="stylesheet" href="../../math.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
|
|
<link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
|
|
<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
|
|
<link rel="prev" href="use_ntl.html" title="Using NTL Library">
|
|
<link rel="next" href="../real_concepts.html" title="Conceptual Requirements for Real Number Types">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="use_ntl.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="../real_concepts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h3 class="title">
|
|
<a name="math_toolkit.high_precision.using_test"></a><a class="link" href="using_test.html" title="Using without expression templates for Boost.Test and others">Using without
|
|
expression templates for Boost.Test and others</a>
|
|
</h3></div></div></div>
|
|
<p>
|
|
As noted in the <a href="../../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>
|
|
documentation, certain program constructs will not compile when using expression
|
|
templates. One example that many users may encounter is Boost.Test (1.54
|
|
and earlier) when using macro BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION.
|
|
</p>
|
|
<p>
|
|
If, for example, you wish to use any multiprecision type like <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code> in place of <code class="computeroutput"><span class="keyword">double</span></code> to give more precision, you will need
|
|
to override the default <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">et_on</span></code>
|
|
with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">et_off</span></code>.
|
|
</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_dec_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
</pre>
|
|
<p>
|
|
To define a 50 decimal digit type using <code class="computeroutput"><span class="identifier">cpp_dec_float</span></code>,
|
|
you must pass two template parameters to <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">number</span></code>.
|
|
</p>
|
|
<p>
|
|
It may be more legible to use a two-staged type definition such as this:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float</span><span class="special"><</span><span class="number">50</span><span class="special">></span> <span class="identifier">mp_backend</span><span class="special">;</span>
|
|
<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">number</span><span class="special"><</span><span class="identifier">mp_backend</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">et_off</span><span class="special">></span> <span class="identifier">cpp_dec_float_50_noet</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
Here, we first define <code class="computeroutput"><span class="identifier">mp_backend</span></code>
|
|
as <code class="computeroutput"><span class="identifier">cpp_dec_float</span></code> with 50
|
|
digits. The second step passes this backend to <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">number</span></code>
|
|
with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">et_off</span></code>, an enumerated type.
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">number</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float</span><span class="special"><</span><span class="number">50</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">et_off</span><span class="special">></span>
|
|
<span class="identifier">cpp_dec_float_50_noet</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
You can reduce typing with a <code class="computeroutput"><span class="keyword">using</span></code>
|
|
directive <code class="computeroutput"><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></code>
|
|
if desired, as shown below.
|
|
</p>
|
|
<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>
|
|
</pre>
|
|
<p>
|
|
Now <code class="computeroutput"><span class="identifier">cpp_dec_float_50_noet</span></code>
|
|
or <code class="computeroutput"><span class="identifier">cpp_dec_float_50_et</span></code> can
|
|
be used as a direct replacement for built-in types like <code class="computeroutput"><span class="keyword">double</span></code>
|
|
etc.
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">cpp_float_test_check_close_noet</span><span class="special">)</span>
|
|
<span class="special">{</span> <span class="comment">// No expression templates/</span>
|
|
<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_dec_float</span><span class="special"><</span><span class="number">50</span><span class="special">>,</span> <span class="identifier">et_off</span><span class="special">></span> <span class="identifier">cpp_dec_float_50_noet</span><span class="special">;</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</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"><</span><span class="identifier">cpp_dec_float_50_noet</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">);</span> <span class="comment">// All significant digits.</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showpoint</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// Show trailing zeros.</span>
|
|
|
|
<span class="identifier">cpp_dec_float_50_noet</span> <span class="identifier">a</span> <span class="special">(</span><span class="string">"1.0"</span><span class="special">);</span>
|
|
<span class="identifier">cpp_dec_float_50_noet</span> <span class="identifier">b</span> <span class="special">(</span><span class="string">"1.0"</span><span class="special">);</span>
|
|
<span class="identifier">b</span> <span class="special">+=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_dec_float_50_noet</span><span class="special">>::</span><span class="identifier">epsilon</span><span class="special">();</span> <span class="comment">// Increment least significant decimal digit.</span>
|
|
|
|
<span class="identifier">cpp_dec_float_50_noet</span> <span class="identifier">eps</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_dec_float_50_noet</span><span class="special">>::</span><span class="identifier">epsilon</span><span class="special">();</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span><span class="string">"a = "</span> <span class="special"><<</span> <span class="identifier">a</span> <span class="special"><<</span> <span class="string">",\nb = "</span> <span class="special"><<</span> <span class="identifier">b</span> <span class="special"><<</span> <span class="string">",\neps = "</span> <span class="special"><<</span> <span class="identifier">eps</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
|
|
<span class="identifier">BOOST_CHECK_CLOSE</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">eps</span> <span class="special">*</span> <span class="number">100</span><span class="special">);</span> <span class="comment">// Expected to pass (because tolerance is as percent).</span>
|
|
<span class="identifier">BOOST_CHECK_CLOSE_FRACTION</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">eps</span><span class="special">);</span> <span class="comment">// Expected to pass (because tolerance is as fraction).</span>
|
|
|
|
|
|
|
|
<span class="special">}</span> <span class="comment">// BOOST_AUTO_TEST_CASE(cpp_float_test_check_close)</span>
|
|
|
|
<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">cpp_float_test_check_close_et</span><span class="special">)</span>
|
|
<span class="special">{</span> <span class="comment">// Using expression templates.</span>
|
|
<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_dec_float</span><span class="special"><</span><span class="number">50</span><span class="special">>,</span> <span class="identifier">et_on</span><span class="special">></span> <span class="identifier">cpp_dec_float_50_et</span><span class="special">;</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</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"><</span><span class="identifier">cpp_dec_float_50_et</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">);</span> <span class="comment">// All significant digits.</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showpoint</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// Show trailing zeros.</span>
|
|
|
|
<span class="identifier">cpp_dec_float_50_et</span> <span class="identifier">a</span><span class="special">(</span><span class="string">"1.0"</span><span class="special">);</span>
|
|
<span class="identifier">cpp_dec_float_50_et</span> <span class="identifier">b</span><span class="special">(</span><span class="string">"1.0"</span><span class="special">);</span>
|
|
<span class="identifier">b</span> <span class="special">+=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_dec_float_50_et</span><span class="special">>::</span><span class="identifier">epsilon</span><span class="special">();</span> <span class="comment">// Increment least significant decimal digit.</span>
|
|
|
|
<span class="identifier">cpp_dec_float_50_et</span> <span class="identifier">eps</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_dec_float_50_et</span><span class="special">>::</span><span class="identifier">epsilon</span><span class="special">();</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"a = "</span> <span class="special"><<</span> <span class="identifier">a</span> <span class="special"><<</span> <span class="string">",\nb = "</span> <span class="special"><<</span> <span class="identifier">b</span> <span class="special"><<</span> <span class="string">",\neps = "</span> <span class="special"><<</span> <span class="identifier">eps</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
|
|
<span class="identifier">BOOST_CHECK_CLOSE</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">eps</span> <span class="special">*</span> <span class="number">100</span><span class="special">);</span> <span class="comment">// Expected to pass (because tolerance is as percent).</span>
|
|
<span class="identifier">BOOST_CHECK_CLOSE_FRACTION</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">eps</span><span class="special">);</span> <span class="comment">// Expected to pass (because tolerance is as fraction).</span>
|
|
</pre>
|
|
<p>
|
|
Using <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code> with
|
|
the default expression template use switched on, the compiler error message
|
|
for `BOOST_CHECK_CLOSE_FRACTION(a, b, eps); would be:
|
|
</p>
|
|
<pre class="programlisting"><span class="comment">// failure floating_point_comparison.hpp(59): error C2440: 'static_cast' :</span>
|
|
<span class="comment">// cannot convert from 'int' to 'boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>'</span>
|
|
</pre>
|
|
<p>
|
|
A full example code is at <a href="../../../../example/test_cpp_float_close_fraction.cpp" target="_top">test_cpp_float_close_fraction.cpp</a>
|
|
</p>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
|
|
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
|
|
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
|
|
Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
|
|
Daryle Walker and Xiaogang Zhang<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
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>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="use_ntl.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="../real_concepts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|