1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Internal Floating-point Promotion Policies</title> 5<link rel="stylesheet" href="../../math.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Math Toolkit 2.12.0"> 8<link rel="up" href="../pol_ref.html" title="Policy Reference"> 9<link rel="prev" href="error_handling_policies.html" title="Error Handling Policies"> 10<link rel="next" href="assert_undefined.html" title="Mathematically Undefined Function Policies"> 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="error_handling_policies.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_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="assert_undefined.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="math_toolkit.pol_ref.internal_promotion"></a><a class="link" href="internal_promotion.html" title="Internal Floating-point Promotion Policies">Internal Floating-point 28 Promotion Policies</a> 29</h3></div></div></div> 30<p> 31 Normally when evaluating a function at say <code class="computeroutput"><span class="keyword">float</span></code> 32 precision, maximal accuracy is assured by conducting the calculation at 33 <code class="computeroutput"><span class="keyword">double</span></code> precision internally, 34 and then rounding the result. There are two policies that control whether 35 internal promotion to a higher precision floating-point type takes place, 36 or not: 37 </p> 38<div class="informaltable"><table class="table"> 39<colgroup> 40<col> 41<col> 42</colgroup> 43<thead><tr> 44<th> 45 <p> 46 Policy 47 </p> 48 </th> 49<th> 50 <p> 51 Meaning 52 </p> 53 </th> 54</tr></thead> 55<tbody> 56<tr> 57<td> 58 <p> 59 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">promote_float</span><span class="special"><</span><span class="identifier">B</span><span class="special">></span></code> 60 </p> 61 </td> 62<td> 63 <p> 64 Indicates whether <code class="computeroutput"><span class="keyword">float</span></code> 65 arguments should be promoted to <code class="computeroutput"><span class="keyword">double</span></code> 66 precision internally: defaults to <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">promote_float</span><span class="special"><</span><span class="keyword">true</span><span class="special">></span></code> 67 </p> 68 </td> 69</tr> 70<tr> 71<td> 72 <p> 73 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">promote_double</span><span class="special"><</span><span class="identifier">B</span><span class="special">></span></code> 74 </p> 75 </td> 76<td> 77 <p> 78 Indicates whether <code class="computeroutput"><span class="keyword">double</span></code> 79 arguments should be promoted to <code class="computeroutput"><span class="keyword">long</span> 80 <span class="keyword">double</span></code> precision internally: 81 defaults to <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">promote_double</span><span class="special"><</span><span class="keyword">true</span><span class="special">></span></code> 82 </p> 83 </td> 84</tr> 85</tbody> 86</table></div> 87<h5> 88<a name="math_toolkit.pol_ref.internal_promotion.h0"></a> 89 <span class="phrase"><a name="math_toolkit.pol_ref.internal_promotion.examples"></a></span><a class="link" href="internal_promotion.html#math_toolkit.pol_ref.internal_promotion.examples">Examples</a> 90 </h5> 91<p> 92 Suppose we want <code class="computeroutput"><span class="identifier">tgamma</span></code> to 93 be evaluated without internal promotion to <code class="computeroutput"><span class="keyword">long</span> 94 <span class="keyword">double</span></code>, then we could use: 95 </p> 96<pre class="programlisting"><span class="preprocessor">#include</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">special_functions</span><span class="special">/</span><span class="identifier">gamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 97 98<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span> 99<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">;</span> 100 101<span class="comment">// Define a new policy *not* internally promoting RealType to double:</span> 102<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special"><</span> 103 <span class="identifier">promote_double</span><span class="special"><</span><span class="keyword">false</span><span class="special">></span> 104 <span class="special">></span> <span class="identifier">my_policy</span><span class="special">;</span> 105 106<span class="comment">// Call the function, applying the new policy:</span> 107<span class="keyword">double</span> <span class="identifier">t1</span> <span class="special">=</span> <span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">some_value</span><span class="special">,</span> <span class="identifier">my_policy</span><span class="special">());</span> 108 109<span class="comment">// Alternatively we could use helper function make_policy,</span> 110<span class="comment">// and concisely define everything at the call site:</span> 111<span class="keyword">double</span> <span class="identifier">t2</span> <span class="special">=</span> <span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">some_value</span><span class="special">,</span> <span class="identifier">make_policy</span><span class="special">(</span><span class="identifier">promote_double</span><span class="special"><</span><span class="keyword">false</span><span class="special">>()));</span> 112</pre> 113<p> 114 Alternatively, suppose we want a distribution to perform calculations without 115 promoting <code class="computeroutput"><span class="keyword">float</span></code> to <code class="computeroutput"><span class="keyword">double</span></code>, then we could use: 116 </p> 117<pre class="programlisting"><span class="preprocessor">#include</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">distributions</span><span class="special">/</span><span class="identifier">normal</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 118<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">normal_distribution</span><span class="special">;</span> 119 120<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span> 121 122<span class="comment">// Define a policy:</span> 123<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special"><</span> 124 <span class="identifier">promote_float</span><span class="special"><</span><span class="keyword">false</span><span class="special">></span> 125 <span class="special">></span> <span class="identifier">my_policy</span><span class="special">;</span> 126 127<span class="comment">// Define the new normal distribution using my_policy:</span> 128<span class="keyword">typedef</span> <span class="identifier">normal_distribution</span><span class="special"><</span><span class="keyword">float</span><span class="special">,</span> <span class="identifier">my_policy</span><span class="special">></span> <span class="identifier">my_norm</span><span class="special">;</span> 129 130<span class="comment">// Get a quantile:</span> 131<span class="keyword">float</span> <span class="identifier">q</span> <span class="special">=</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">my_norm</span><span class="special">(),</span> <span class="number">0.05f</span><span class="special">);</span> 132</pre> 133</div> 134<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 135<td align="left"></td> 136<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 137 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 138 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 139 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 140 Daryle Walker and Xiaogang Zhang<p> 141 Distributed under the Boost Software License, Version 1.0. (See accompanying 142 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>) 143 </p> 144</div></td> 145</tr></table> 146<hr> 147<div class="spirit-nav"> 148<a accesskey="p" href="error_handling_policies.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_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="assert_undefined.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 149</div> 150</body> 151</html> 152