]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Use in non-template code</title> | |
5 | <link rel="stylesheet" href="../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../index.html" title="Math Toolkit 2.5.1"> | |
8 | <link rel="up" href="../tutorial.html" title="Tutorial"> | |
9 | <link rel="prev" href="../tutorial.html" title="Tutorial"> | |
10 | <link rel="next" href="templ.html" title="Use in template code"> | |
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="../tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="templ.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.tutorial.non_templ"></a><a class="link" href="non_templ.html" title="Use in non-template code">Use in non-template | |
28 | code</a> | |
29 | </h3></div></div></div> | |
30 | <p> | |
31 | When using the math constants at your chosen fixed precision in non-template | |
32 | code, you can simply add a <code class="computeroutput"><span class="keyword">using</span> <span class="keyword">namespace</span></code> declaration, for example, <code class="computeroutput"><span class="keyword">using</span> <span class="keyword">namespace</span> | |
33 | <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">double_constants</span></code>, to make the constants | |
34 | of the correct precision for your code visible in the current scope, and | |
35 | then use each constant <span class="emphasis"><em>as a simple variable - sans brackets</em></span>: | |
36 | </p> | |
37 | <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">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> | |
38 | ||
39 | <span class="keyword">double</span> <span class="identifier">area</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">r</span><span class="special">)</span> | |
40 | <span class="special">{</span> | |
41 | <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">double_constants</span><span class="special">;</span> | |
42 | <span class="keyword">return</span> <span class="identifier">pi</span> <span class="special">*</span> <span class="identifier">r</span> <span class="special">*</span> <span class="identifier">r</span><span class="special">;</span> | |
43 | <span class="special">}</span> | |
44 | </pre> | |
45 | <p> | |
46 | Had our function been written as taking a <code class="computeroutput"><span class="keyword">float</span></code> | |
47 | rather than a <code class="computeroutput"><span class="keyword">double</span></code>, we could | |
48 | have written instead: | |
49 | </p> | |
50 | <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">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> | |
51 | ||
52 | <span class="keyword">float</span> <span class="identifier">area</span><span class="special">(</span><span class="keyword">float</span> <span class="identifier">r</span><span class="special">)</span> | |
53 | <span class="special">{</span> | |
54 | <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">float_constants</span><span class="special">;</span> | |
55 | <span class="keyword">return</span> <span class="identifier">pi</span> <span class="special">*</span> <span class="identifier">r</span> <span class="special">*</span> <span class="identifier">r</span><span class="special">;</span> | |
56 | <span class="special">}</span> | |
57 | </pre> | |
58 | <p> | |
59 | Likewise, constants that are suitable for use at <code class="computeroutput"><span class="keyword">long</span> | |
60 | <span class="keyword">double</span></code> precision are available in | |
61 | the namespace <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">long_double_constants</span></code>. | |
62 | </p> | |
63 | <p> | |
64 | You can see the full list of available constants at <a class="link" href="../constants.html" title="The Mathematical Constants">math_toolkit.constants</a>. | |
65 | </p> | |
66 | <p> | |
67 | Some examples of using constants are at <a href="../../../../example/constants_eg1.cpp" target="_top">constants_eg1</a>. | |
68 | </p> | |
69 | </div> | |
70 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
71 | <td align="left"></td> | |
72 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
73 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
74 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
75 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
76 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
77 | 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>) | |
78 | </p> | |
79 | </div></td> | |
80 | </tr></table> | |
81 | <hr> | |
82 | <div class="spirit-nav"> | |
83 | <a accesskey="p" href="../tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="templ.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
84 | </div> | |
85 | </body> | |
86 | </html> |