result_type.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Calculation of the Type of the Result</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.11.0">
  8. <link rel="up" href="../overview.html" title="Chapter&#160;1.&#160;Overview">
  9. <link rel="prev" href="namespaces.html" title="Namespaces">
  10. <link rel="next" href="error_handling.html" title="Error Handling">
  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="namespaces.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="error_handling.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="math_toolkit.result_type"></a><a class="link" href="result_type.html" title="Calculation of the Type of the Result">Calculation of the Type of the
  28. Result</a>
  29. </h2></div></div></div>
  30. <p>
  31. The functions in this library are all overloaded to accept mixed floating point
  32. (or mixed integer and floating point type) arguments. So for example:
  33. </p>
  34. <pre class="programlisting"><span class="identifier">foo</span><span class="special">(</span><span class="number">1.0</span><span class="special">,</span> <span class="number">2.0</span><span class="special">);</span>
  35. <span class="identifier">foo</span><span class="special">(</span><span class="number">1.0f</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
  36. <span class="identifier">foo</span><span class="special">(</span><span class="number">1.0</span><span class="special">,</span> <span class="number">2L</span><span class="special">);</span>
  37. </pre>
  38. <p>
  39. etc, are all valid calls, as long as "foo" is a function taking two
  40. floating-point arguments. But that leaves the question:
  41. </p>
  42. <div class="blurb">
  43. <div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div>
  44. <p>
  45. <span class="emphasis"><em>"Given a special function with N arguments of types T1, T2,
  46. T3 ... TN, then what type is the result?"</em></span>
  47. </p>
  48. </div>
  49. <p>
  50. <span class="bold"><strong>If all the arguments are of the same (floating point)
  51. type then the result is the same type as the arguments.</strong></span>
  52. </p>
  53. <p>
  54. Otherwise, the type of the result is computed using the following logic:
  55. </p>
  56. <div class="orderedlist"><ol class="orderedlist" type="1">
  57. <li class="listitem">
  58. Any arguments that are not template arguments are disregarded from further
  59. analysis.
  60. </li>
  61. <li class="listitem">
  62. For each type in the argument list, if that type is an integer type then
  63. it is treated as if it were of type <code class="computeroutput"><span class="keyword">double</span></code>
  64. for the purposes of further analysis.
  65. </li>
  66. <li class="listitem">
  67. If any of the arguments is a user-defined class type, then the result type
  68. is the first such class type that is constructible from all of the other
  69. argument types.
  70. </li>
  71. <li class="listitem">
  72. If any of the arguments is of type <code class="computeroutput"><span class="keyword">long</span>
  73. <span class="keyword">double</span></code>, then the result is of type
  74. <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>.
  75. </li>
  76. <li class="listitem">
  77. If any of the arguments is of type <code class="computeroutput"><span class="keyword">double</span></code>,
  78. then the result is of type <code class="computeroutput"><span class="keyword">double</span></code>.
  79. </li>
  80. <li class="listitem">
  81. Otherwise the result is of type <code class="computeroutput"><span class="keyword">float</span></code>.
  82. </li>
  83. </ol></div>
  84. <p>
  85. For example:
  86. </p>
  87. <pre class="programlisting"><span class="identifier">cyl_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0</span><span class="special">);</span>
  88. </pre>
  89. <p>
  90. Returns a <code class="computeroutput"><span class="keyword">double</span></code> result, as does:
  91. </p>
  92. <pre class="programlisting"><span class="identifier">cyl_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
  93. </pre>
  94. <p>
  95. as in this case the integer first argument is treated as a <code class="computeroutput"><span class="keyword">double</span></code>
  96. and takes precedence over the <code class="computeroutput"><span class="keyword">float</span></code>
  97. second argument. To get a <code class="computeroutput"><span class="keyword">float</span></code>
  98. result we would need all the arguments to be of type float:
  99. </p>
  100. <pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">2.0f</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
  101. </pre>
  102. <p>
  103. When one or more of the arguments is not a template argument then it doesn't
  104. effect the return type at all, for example:
  105. </p>
  106. <pre class="programlisting"><span class="identifier">sph_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
  107. </pre>
  108. <p>
  109. returns a <code class="computeroutput"><span class="keyword">float</span></code>, since the first
  110. argument is not a template argument and so doesn't effect the result: without
  111. this rule functions that take explicitly integer arguments could never return
  112. <code class="computeroutput"><span class="keyword">float</span></code>.
  113. </p>
  114. <p>
  115. And for user-defined types, typically <a href="../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>,
  116. </p>
  117. <p>
  118. All of the following return a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span></code>
  119. result:
  120. </p>
  121. <pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">0</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_bin_quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">));</span>
  122. <span class="identifier">cyl_bessel_j</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_bin_quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="number">3</span><span class="special">);</span>
  123. <span class="identifier">cyl_bessel_j</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_bin_quad_float</span><span class="special">(</span><span class="number">2</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_bin_quad_float</span><span class="special">(</span><span class="number">3</span><span class="special">));</span>
  124. </pre>
  125. <p>
  126. but rely on the parameters provided being exactly representable, avoiding loss
  127. of precision from construction from <code class="computeroutput"><span class="keyword">double</span></code>.
  128. </p>
  129. <div class="tip"><table border="0" summary="Tip">
  130. <tr>
  131. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../doc/src/images/tip.png"></td>
  132. <th align="left">Tip</th>
  133. </tr>
  134. <tr><td align="left" valign="top"><p>
  135. All new projects should use Boost.Multiprecision.
  136. </p></td></tr>
  137. </table></div>
  138. <p>
  139. During development of Boost.Math, <a href="http://www.shoup.net/ntl/" target="_top">NTL
  140. A Library for doing Number Theory</a> was invaluable to create highly precise
  141. tables.
  142. </p>
  143. <p>
  144. All of the following return an <code class="computeroutput"><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span></code> result:
  145. </p>
  146. <pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">2</span><span class="special">));</span>
  147. <span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="number">3</span><span class="special">);</span>
  148. <span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">3</span><span class="special">));</span>
  149. </pre>
  150. <p>
  151. In the last case, <code class="computeroutput"><span class="identifier">quad_float</span></code>
  152. is convertible to <code class="computeroutput"><span class="identifier">RR</span></code>, but not
  153. vice-versa, so the result will be an <code class="computeroutput"><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span></code>. Note
  154. that this assumes that you are using a <a class="link" href="high_precision/use_ntl.html" title="Using NTL Library">patched
  155. NTL library</a>.
  156. </p>
  157. <p>
  158. These rules are chosen to be compatible with the behaviour of <span class="emphasis"><em>ISO/IEC
  159. 9899:1999 Programming languages - C</em></span> and with the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf" target="_top">Draft
  160. Technical Report on C++ Library Extensions, 2005-06-24, section 5.2.1, paragraph
  161. 5</a>.
  162. </p>
  163. </div>
  164. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  165. <td align="left"></td>
  166. <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
  167. Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
  168. Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
  169. R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
  170. Daryle Walker and Xiaogang Zhang<p>
  171. Distributed under the Boost Software License, Version 1.0. (See accompanying
  172. 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>)
  173. </p>
  174. </div></td>
  175. </tr></table>
  176. <hr>
  177. <div class="spirit-nav">
  178. <a accesskey="p" href="namespaces.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="error_handling.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  179. </div>
  180. </body>
  181. </html>