3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Changing the Policy Defaults
</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=
"../pol_tutorial.html" title=
"Policy Tutorial">
9 <link rel=
"prev" href=
"policy_usage.html" title=
"So How are Policies Used Anyway?">
10 <link rel=
"next" href=
"ad_hoc_dist_policies.html" title=
"Setting Policies for Distributions on an Ad Hoc Basis">
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>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"policy_usage.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_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=
"ad_hoc_dist_policies.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h3 class=
"title">
27 <a name=
"math_toolkit.pol_tutorial.changing_policy_defaults"></a><a class=
"link" href=
"changing_policy_defaults.html" title=
"Changing the Policy Defaults">Changing
28 the Policy Defaults
</a>
29 </h3></div></div></div>
31 The default policies used by the library are changed by the usual configuration
35 For example, passing
<code class=
"computeroutput"><span class=
"special">-
</span><span class=
"identifier">DBOOST_MATH_DOMAIN_ERROR_POLICY
</span><span class=
"special">=
</span><span class=
"identifier">errno_on_error
</span></code>
36 to your compiler will cause domain errors to set
<code class=
"computeroutput"><span class=
"special">::
</span><span class=
"identifier">errno
</span></code> and return a
<a href=
"http://en.wikipedia.org/wiki/NaN" target=
"_top">NaN
</a>
37 rather than the usual default behaviour of throwing a
<code class=
"computeroutput"><span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">domain_error
</span></code>
40 <div class=
"tip"><table border=
"0" summary=
"Tip">
42 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Tip]" src=
"../../../../../../doc/src/images/tip.png"></td>
43 <th align=
"left">Tip
</th>
45 <tr><td align=
"left" valign=
"top">
47 For Microsoft Visual Studio,you can add to the Project Property Page, C/C++,
48 Preprocessor, Preprocessor definitions like:
50 <pre class=
"programlisting"><span class=
"identifier">BOOST_MATH_ASSERT_UNDEFINED_POLICY
</span><span class=
"special">=
</span><span class=
"number">0</span>
51 <span class=
"identifier">BOOST_MATH_OVERFLOW_ERROR_POLICY
</span><span class=
"special">=
</span><span class=
"identifier">errno_on_error
</span></pre>
53 This may be helpful to avoid complications with pre-compiled headers that
54 may mean that the equivalent definitions in source code:
56 <pre class=
"programlisting"><span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_ASSERT_UNDEFINED_POLICY
</span> <span class=
"keyword">false
</span>
57 <span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_OVERFLOW_ERROR_POLICY
</span> <span class=
"identifier">errno_on_error
</span></pre>
59 <span class=
"bold"><strong>may be ignored
</strong></span>.
62 The compiler command line shows:
64 <pre class=
"programlisting"><span class=
"special">/
</span><span class=
"identifier">D
</span> <span class=
"string">"BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"</span>
65 <span class=
"special">/
</span><span class=
"identifier">D
</span> <span class=
"string">"BOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error"</span></pre>
69 There is however a very important caveat to this:
71 <div class=
"important"><table border=
"0" summary=
"Important">
73 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Important]" src=
"../../../../../../doc/src/images/important.png"></td>
74 <th align=
"left">Important
</th>
76 <tr><td align=
"left" valign=
"top">
78 <span class=
"bold"><strong><span class=
"emphasis"><em>Default policies changed by setting configuration
79 macros must be changed uniformly in every translation unit in the program.
</em></span></strong></span>
82 Failure to follow this rule may result in violations of the
"One Definition
83 Rule (ODR)" and result in unpredictable program behaviour.
88 That means there are only two safe ways to use these macros:
90 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
92 Edit them in
<a href=
"../../../../../../boost/math/tools/user.hpp" target=
"_top">boost/math/tools/user.hpp
</a>,
93 so that the defaults are set on an installation-wide basis. Unfortunately
94 this may not be convenient if you are using a pre-installed Boost distribution
95 (on Linux for example).
98 Set the defines in your project's Makefile or build environment, so that
99 they are set uniformly across all translation units.
103 What you should
<span class=
"bold"><strong>not
</strong></span> do is:
105 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; "><li class=
"listitem">
106 Set the defines in the source file using
<code class=
"computeroutput"><span class=
"preprocessor">#define
</span></code>
107 as doing so almost certainly will break your program, unless you're absolutely
108 certain that the program is restricted to a single translation unit.
111 And, yes, you will find examples in our test programs where we break this
112 rule: but only because we know there will always be a single translation
113 unit only:
<span class=
"emphasis"><em>don't say that you weren't warned!
</em></span>
116 The following example demonstrates the effect of setting the macro BOOST_MATH_DOMAIN_ERROR_POLICY
117 when an invalid argument is encountered. For the purposes of this example,
118 we'll pass a negative degrees of freedom parameter to the student's t distribution.
121 Since we know that this is a single file program we could just add:
123 <pre class=
"programlisting"><span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span> <span class=
"identifier">ignore_error
</span>
126 to the top of the source file to change the default policy to one that simply
127 returns a NaN when a domain error occurs. Alternatively we could use:
129 <pre class=
"programlisting"><span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span> <span class=
"identifier">errno_on_error
</span>
132 To ensure the
<code class=
"computeroutput"><span class=
"special">::
</span><span class=
"identifier">errno
</span></code>
133 is set when a domain error occurs as well as returning a NaN.
136 This is safe provided the program consists of a single translation unit
137 <span class=
"emphasis"><em>and
</em></span> we place the define
<span class=
"emphasis"><em>before
</em></span>
138 any #includes. Note that should we add the define after the includes then
139 it will have no effect! A warning such as:
141 <pre class=
"programlisting">warning C4005: 'BOOST_MATH_OVERFLOW_ERROR_POLICY' : macro redefinition
</pre>
143 is a certain sign that it will
<span class=
"emphasis"><em>not
</em></span> have the desired
147 We'll begin our sample program with the needed includes:
149 <pre class=
"programlisting"> <span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span> <span class=
"identifier">ignore_error
</span>
151 <span class=
"comment">// Boost
</span>
152 <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">students_t
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
153 <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">students_t
</span><span class=
"special">;
</span> <span class=
"comment">// Probability of students_t(df, t).
</span>
155 <span class=
"comment">// std
</span>
156 <span class=
"preprocessor">#include
</span> <span class=
"special"><</span><span class=
"identifier">iostream
</span><span class=
"special">></span>
157 <span class=
"keyword">using
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">cout
</span><span class=
"special">;
</span>
158 <span class=
"keyword">using
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">endl
</span><span class=
"special">;
</span>
160 <span class=
"preprocessor">#include
</span> <span class=
"special"><</span><span class=
"identifier">stdexcept
</span><span class=
"special">></span>
163 <span class=
"preprocessor">#include
</span> <span class=
"special"><</span><span class=
"identifier">cstddef
</span><span class=
"special">></span>
164 <span class=
"comment">// using ::errno
</span>
167 Next we'll define the program's main() to call the student's t distribution
168 with an invalid degrees of freedom parameter, the program is set up to handle
169 either an exception or a NaN:
171 <pre class=
"programlisting"><span class=
"keyword">int
</span> <span class=
"identifier">main
</span><span class=
"special">()
</span>
172 <span class=
"special">{
</span>
173 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"Example error handling using Student's t function. "</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
174 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"BOOST_MATH_DOMAIN_ERROR_POLICY is set to: "</span>
175 <span class=
"special"><<</span> <span class=
"identifier">BOOST_STRINGIZE
</span><span class=
"special">(
</span><span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
177 <span class=
"keyword">double
</span> <span class=
"identifier">degrees_of_freedom
</span> <span class=
"special">=
</span> <span class=
"special">-
</span><span class=
"number">1</span><span class=
"special">;
</span> <span class=
"comment">// A bad argument!
</span>
178 <span class=
"keyword">double
</span> <span class=
"identifier">t
</span> <span class=
"special">=
</span> <span class=
"number">10</span><span class=
"special">;
</span>
180 <span class=
"keyword">try
</span>
181 <span class=
"special">{
</span>
182 <span class=
"identifier">errno
</span> <span class=
"special">=
</span> <span class=
"number">0</span><span class=
"special">;
</span> <span class=
"comment">// Clear/reset.
</span>
183 <span class=
"identifier">students_t
</span> <span class=
"identifier">dist
</span><span class=
"special">(
</span><span class=
"identifier">degrees_of_freedom
</span><span class=
"special">);
</span> <span class=
"comment">// exception is thrown here if enabled.
</span>
184 <span class=
"keyword">double
</span> <span class=
"identifier">p
</span> <span class=
"special">=
</span> <span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">dist
</span><span class=
"special">,
</span> <span class=
"identifier">t
</span><span class=
"special">);
</span>
185 <span class=
"comment">// Test for error reported by other means:
</span>
186 <span class=
"keyword">if
</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">isnan
</span><span class=
"special">)(
</span><span class=
"identifier">p
</span><span class=
"special">))
</span>
187 <span class=
"special">{
</span>
188 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"cdf returned a NaN!"</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
189 <span class=
"keyword">if
</span> <span class=
"special">(
</span><span class=
"identifier">errno
</span> <span class=
"special">!=
</span> <span class=
"number">0</span><span class=
"special">)
</span>
190 <span class=
"special">{
</span> <span class=
"comment">// So errno has been set.
</span>
191 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"errno is set to: "</span> <span class=
"special"><<</span> <span class=
"identifier">errno
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
192 <span class=
"special">}
</span>
193 <span class=
"special">}
</span>
194 <span class=
"keyword">else
</span>
195 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"Probability of Student's t is "</span> <span class=
"special"><<</span> <span class=
"identifier">p
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
196 <span class=
"special">}
</span>
197 <span class=
"keyword">catch
</span><span class=
"special">(
</span><span class=
"keyword">const
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">exception
</span><span class=
"special">&</span> <span class=
"identifier">e
</span><span class=
"special">)
</span>
198 <span class=
"special">{
</span>
199 <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">cout
</span> <span class=
"special"><<</span>
200 <span class=
"string">"\n"</span><span class=
"string">"Message from thrown exception was:\n "</span> <span class=
"special"><<</span> <span class=
"identifier">e
</span><span class=
"special">.
</span><span class=
"identifier">what
</span><span class=
"special">()
</span> <span class=
"special"><<</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">endl
</span><span class=
"special">;
</span>
201 <span class=
"special">}
</span>
202 <span class=
"keyword">return
</span> <span class=
"number">0</span><span class=
"special">;
</span>
203 <span class=
"special">}
</span> <span class=
"comment">// int main()
</span>
206 Here's what the program output looks like with a default build (one that
207 <span class=
"bold"><strong>does throw exceptions
</strong></span>):
209 <pre class=
"programlisting">Example error handling using Student's t function.
210 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error
212 Message from thrown exception was:
213 Error in function boost::math::students_t_distribution
<double
>::students_t_distribution:
214 Degrees of freedom argument is -
1, but must be
> 0 !
217 Alternatively let's build with:
219 <pre class=
"programlisting"><span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span> <span class=
"identifier">ignore_error
</span>
222 Now the program output is:
224 <pre class=
"programlisting">Example error handling using Student's t function.
225 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: ignore_error
229 And finally let's build with:
231 <pre class=
"programlisting"><span class=
"preprocessor">#define
</span> <span class=
"identifier">BOOST_MATH_DOMAIN_ERROR_POLICY
</span> <span class=
"identifier">errno_on_error
</span>
234 Which gives the output show errno:
236 <pre class=
"programlisting">Example error handling using Student's t function.
237 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: errno_on_error
242 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
243 <td align=
"left"></td>
244 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
245 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
246 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
247 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
248 Distributed under the Boost Software License, Version
1.0. (See accompanying
249 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>)
254 <div class=
"spirit-nav">
255 <a accesskey=
"p" href=
"policy_usage.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_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=
"ad_hoc_dist_policies.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>