Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65149 - trunk/libs/math/example
From: pbristow_at_[hidden]
Date: 2010-08-31 09:55:30


Author: pbristow
Date: 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
New Revision: 65149
URL: http://svn.boost.org/trac/boost/changeset/65149

Log:
Avoid name ambiguity.
Text files modified:
   trunk/libs/math/example/policy_eg_1.cpp | 42 +++++++++---
   trunk/libs/math/example/policy_eg_10.cpp | 132 +++++++++++++++++++++------------------
   trunk/libs/math/example/policy_eg_9.cpp | 36 ++++------
   trunk/libs/math/example/policy_ref_snip1.cpp | 75 ++++++++++++++++------
   trunk/libs/math/example/policy_ref_snip9.cpp | 27 +++++---
   5 files changed, 188 insertions(+), 124 deletions(-)

Modified: trunk/libs/math/example/policy_eg_1.cpp
==============================================================================
--- trunk/libs/math/example/policy_eg_1.cpp (original)
+++ trunk/libs/math/example/policy_eg_1.cpp 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
@@ -1,17 +1,20 @@
 // Copyright John Maddock 2007.
+// Copyright Paul A> Bristow 2010
 // Use, modification and distribution are subject to the
 // Boost Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
 #include <iostream>
+using std::cout; using std::endl;
 
 //[policy_eg_1
 
 #include <boost/math/special_functions/gamma.hpp>
-//
+using boost::math::tgamma;
+
 // Define the policy to use:
-//
 using namespace boost::math::policies;
+
 typedef policy<
    domain_error<errno_on_error>,
    pole_error<errno_on_error>,
@@ -20,17 +23,34 @@
> c_policy;
 //
 // Now use the policy when calling tgamma:
-//
+
+// http://msdn.microsoft.com/en-us/library/t3ayayh1.aspx
+// Microsoft errno declared in STDLIB.H as "extern int errno;"
+
 int main()
 {
- errno = 0;
- std::cout << "Result of tgamma(30000) is: "
- << boost::math::tgamma(30000, c_policy()) << std::endl;
- std::cout << "errno = " << errno << std::endl;
- std::cout << "Result of tgamma(-10) is: "
- << boost::math::tgamma(-10, c_policy()) << std::endl;
- std::cout << "errno = " << errno << std::endl;
-}
+ errno = 0; // Reset.
+ cout << "Result of tgamma(30000) is: "
+ << tgamma(30000, c_policy()) << endl; // Too big parameter
+ cout << "errno = " << errno << endl; // errno 34 Numerical result out of range.
+ cout << "Result of tgamma(-10) is: "
+ << boost::math::tgamma(-10, c_policy()) << endl; // Negative parameter.
+ cout << "errno = " << errno << endl; // error 33 Numerical argument out of domain.
+} // int main()
 
 //]
 
+/* Output
+
+policy_eg_1.cpp
+ Generating code
+ Finished generating code
+ policy_eg_1.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_eg_1.exe
+ Result of tgamma(30000) is: 1.#INF
+ errno = 34
+ Result of tgamma(-10) is: 1.#QNAN
+ errno = 33
+
+*/
+
+

Modified: trunk/libs/math/example/policy_eg_10.cpp
==============================================================================
--- trunk/libs/math/example/policy_eg_10.cpp (original)
+++ trunk/libs/math/example/policy_eg_10.cpp 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
@@ -1,4 +1,5 @@
 // Copyright John Maddock 2007.
+// Copyright Paul A. Bristow 2010
 // Use, modification and distribution are subject to the
 // Boost Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -16,22 +17,42 @@
 success fraction of 0.5 once again, and calculate
 all the possible quantiles at 0.05 and 0.95.
 
-Begin by including the needed headers:
+Begin by including the needed headers (and some using statements for conciseness):
 
 */
-
 #include <iostream>
-#include <boost/math/distributions/binomial.hpp>
+using std::cout; using std::endl;
+using std::left; using std::fixed; using std::right; using std::scientific;
+#include <iomanip>
+using std::setw;
+using std::setprecision;
 
+#include <boost/math/distributions/binomial.hpp>
 /*`
 
 Next we'll bring the needed declarations into scope, and
 define distribution types for all the available rounding policies:
 
 */
+// Avoid
+// using namespace std; // and
+// using namespace boost::math;
+// to avoid potential ambiguity of names, like binomial.
+// using namespace boost::math::policies; is small risk, but
+// the necessary items are brought into scope thus:
+
+using boost::math::binomial_distribution;
+using boost::math::policies::policy;
+using boost::math::policies::discrete_quantile;
+
+using boost::math::policies::integer_round_outwards;
+using boost::math::policies::integer_round_down;
+using boost::math::policies::integer_round_up;
+using boost::math::policies::integer_round_nearest;
+using boost::math::policies::integer_round_inwards;
+using boost::math::policies::real;
 
-using namespace boost::math::policies;
-using namespace boost::math;
+using boost::math::binomial_distribution; // Not std::binomial_distribution.
 
 typedef binomial_distribution<
             double,
@@ -64,108 +85,99 @@
         binom_real_quantile;
 
 /*`
-
 Now let's set to work calling those quantiles:
-
 */
 
 int main()
 {
- std::cout <<
+ cout <<
       "Testing rounding policies for a 50 sample binomial distribution,\n"
       "with a success fraction of 0.5.\n\n"
       "Lower quantiles are calculated at p = 0.05\n\n"
       "Upper quantiles at p = 0.95.\n\n";
 
- std::cout << std::setw(25) << std::right
- << "Policy"<< std::setw(18) << std::right
- << "Lower Quantile" << std::setw(18) << std::right
- << "Upper Quantile" << std::endl;
+ cout << setw(25) << right
+ << "Policy"<< setw(18) << right
+ << "Lower Quantile" << setw(18) << right
+ << "Upper Quantile" << endl;
    
    // Test integer_round_outwards:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "integer_round_outwards"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_outwards(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_outwards(50, 0.5), 0.95)
- << std::endl;
+ << endl;
    
    // Test integer_round_inwards:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "integer_round_inwards"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_inwards(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_inwards(50, 0.5), 0.95)
- << std::endl;
+ << endl;
    
    // Test integer_round_down:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "integer_round_down"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_down(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_down(50, 0.5), 0.95)
- << std::endl;
+ << endl;
    
    // Test integer_round_up:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "integer_round_up"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_up(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_up(50, 0.5), 0.95)
- << std::endl;
+ << endl;
    
    // Test integer_round_nearest:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "integer_round_nearest"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_nearest(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_round_nearest(50, 0.5), 0.95)
- << std::endl;
+ << endl;
    
    // Test real:
- std::cout << std::setw(25) << std::right
+ cout << setw(25) << right
       << "real"
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_real_quantile(50, 0.5), 0.05)
- << std::setw(18) << std::right
+ << setw(18) << right
       << quantile(binom_real_quantile(50, 0.5), 0.95)
- << std::endl;
-}
+ << endl;
+} // int main()
 
 /*`
 
 Which produces the program output:
 
 [pre
-Testing rounding policies for a 50 sample binomial distribution,
-with a success fraction of 0.5.
-
-Lower quantiles are calculated at p = 0.05
-
-Upper quantiles at p = 0.95.
-
-Testing rounding policies for a 50 sample binomial distribution,
-with a success fraction of 0.5.
-
-Lower quantiles are calculated at p = 0.05
-
-Upper quantiles at p = 0.95.
-
- Policy Lower Quantile Upper Quantile
- integer_round_outwards 18 31
- integer_round_inwards 19 30
- integer_round_down 18 30
- integer_round_up 19 31
- integer_round_nearest 19 30
- real 18.701 30.299
+ policy_eg_10.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_eg_10.exe
+ Testing rounding policies for a 50 sample binomial distribution,
+ with a success fraction of 0.5.
+
+ Lower quantiles are calculated at p = 0.05
+
+ Upper quantiles at p = 0.95.
+
+ Policy Lower Quantile Upper Quantile
+ integer_round_outwards 18 31
+ integer_round_inwards 19 30
+ integer_round_down 18 30
+ integer_round_up 19 31
+ integer_round_nearest 19 30
+ real 18.701 30.299
 ]
 
 */
 
-//] ends quickbook import
-
+//] //[policy_eg_10] ends quickbook import.

Modified: trunk/libs/math/example/policy_eg_9.cpp
==============================================================================
--- trunk/libs/math/example/policy_eg_9.cpp (original)
+++ trunk/libs/math/example/policy_eg_9.cpp 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
@@ -1,4 +1,5 @@
 // Copyright John Maddock 2007.
+// Copyright Paul A. Bristow 2010
 // Use, modification and distribution are subject to the
 // Boost Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -7,6 +8,7 @@
 // and comments, don't change any of the special comment mark-ups!
 
 #include <iostream>
+using std::cout; using std::endl;
 
 //[policy_eg_9
 
@@ -49,14 +51,11 @@
 
 */
 
-#include <iostream>
 #include <boost/math/special_functions.hpp>
 
 /*`
-
 Next we'll implement the error handlers for each type of error,
 starting with domain errors:
-
 */
 
 namespace boost{ namespace math{ namespace policies{
@@ -213,13 +212,12 @@
 
 
 /*`
-
 Now we'll need to define a suitable policy that will call these handlers,
 and define some forwarding functions that make use of the policy:
-
 */
 
-namespace{
+namespace
+{
 
 using namespace boost::math::policies;
 
@@ -237,8 +235,7 @@
 } // close unnamed namespace
 
 /*`
-
-We now have a set of forwarding functions defined in an unnamed namespace
+We now have a set of forwarding functions, defined in an unnamed namespace,
 that all look something like this:
 
 ``
@@ -253,28 +250,27 @@
 So that when we call `tgamma(z)` we really end up calling
 `boost::math::tgamma(z, user_error_policy())`, and any
 errors will get directed to our own error handlers:
-
 */
 
 
 int main()
 {
    // Raise a domain error:
- std::cout << "Result of erf_inv(-10) is: "
- << erf_inv(-10) << std::endl << std::endl;
+ cout << "Result of erf_inv(-10) is: "
+ << erf_inv(-10) << std::endl << endl;
    // Raise a pole error:
- std::cout << "Result of tgamma(-10) is: "
- << tgamma(-10) << std::endl << std::endl;
+ cout << "Result of tgamma(-10) is: "
+ << tgamma(-10) << std::endl << endl;
    // Raise an overflow error:
- std::cout << "Result of tgamma(3000) is: "
- << tgamma(3000) << std::endl << std::endl;
+ cout << "Result of tgamma(3000) is: "
+ << tgamma(3000) << std::endl << endl;
    // Raise an underflow error:
- std::cout << "Result of tgamma(-190.5) is: "
- << tgamma(-190.5) << std::endl << std::endl;
+ cout << "Result of tgamma(-190.5) is: "
+ << tgamma(-190.5) << std::endl << endl;
    // Unfortunately we can't predicably raise a denormalised
    // result, nor can we raise an evaluation error in this example
    // since these should never really occur!
-}
+} // int main()
 
 /*`
 
@@ -309,8 +305,6 @@
 `tgamma(-190.5)` is implemented in terms of `tgamma(190.5)` - which overflows -
 the reflection formula for `tgamma` then notices that it's dividing by
 infinity and underflows.
-
 */
 
-//]
-
+//] //[/policy_eg_9]

Modified: trunk/libs/math/example/policy_ref_snip1.cpp
==============================================================================
--- trunk/libs/math/example/policy_ref_snip1.cpp (original)
+++ trunk/libs/math/example/policy_ref_snip1.cpp 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
@@ -1,4 +1,5 @@
 // Copyright John Maddock 2007.
+// Copyright Paul A. Bristow 2010.
 // Use, modification and distribution are subject to the
 // Boost Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -6,39 +7,69 @@
 // Note that this file contains quickbook mark-up as well as code
 // and comments, don't change any of the special comment mark-ups!
 
-double some_value = 0;
-
 //[policy_ref_snip1
 
 #include <boost/math/special_functions/gamma.hpp>
+using boost::math::tgamma;
 
-using namespace boost::math::policies;
-using namespace boost::math;
+//using namespace boost::math::policies; may also be convenient.
+using boost::math::policies::policy;
+using boost::math::policies::evaluation_error;
+using boost::math::policies::domain_error;
+using boost::math::policies::overflow_error;
+using boost::math::policies::domain_error;
+using boost::math::policies::pole_error;
+using boost::math::policies::errno_on_error;
 
 // Define a policy:
 typedef policy<
- domain_error<errno_on_error>,
- pole_error<errno_on_error>,
- overflow_error<errno_on_error>,
- policies::evaluation_error<errno_on_error>
- > my_policy;
-
-// call the function:
-double t1 = tgamma(some_value, my_policy());
-
-// Alternatively we could use make_policy and define everything at the call site:
-double t2 = tgamma(some_value, make_policy(
- domain_error<errno_on_error>(),
- pole_error<errno_on_error>(),
- overflow_error<errno_on_error>(),
- policies::evaluation_error<errno_on_error>()
- ));
-
+ domain_error<errno_on_error>,
+ pole_error<errno_on_error>,
+ overflow_error<errno_on_error>,
+ evaluation_error<errno_on_error>
+> my_policy;
+
+double my_value = 0.; //
+
+// Call the function applying my_policy:
+double t1 = tgamma(my_value, my_policy());
+
+// Alternatively (and equivalently) we could use helpful function
+// make_policy and define everything at the call site:
+double t2 = tgamma(my_value,
+ make_policy(
+ domain_error<errno_on_error>(),
+ pole_error<errno_on_error>(),
+ overflow_error<errno_on_error>(),
+ evaluation_error<errno_on_error>() )
+ );
 //]
 
 #include <iostream>
+using std::cout; using std::endl;
 
 int main()
 {
- std::cout << t1 << " " << t2 << std::endl;
+ cout << "my_value = " << my_value << endl;
+ try
+ { // First with default policy - throw an exception.
+ cout << "tgamma(my_value) = " << tgamma(my_value) << endl;
+ }
+ catch(const std::exception& e)
+ {
+ cout <<"\n""Message from thrown exception was:\n " << e.what() << endl;
+ }
+
+ cout << "tgamma(my_value, my_policy() = " << t1 << endl;
+ cout << "tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = " << t2 << endl;
 }
+
+/*
+Output:
+ my_value = 0
+
+ Message from thrown exception was:
+ Error in function boost::math::tgamma<long double>(long double): Evaluation of tgamma at a negative integer 0.
+ tgamma(my_value, my_policy() = 1.#QNAN
+ tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = 1.#QNAN
+*/

Modified: trunk/libs/math/example/policy_ref_snip9.cpp
==============================================================================
--- trunk/libs/math/example/policy_ref_snip9.cpp (original)
+++ trunk/libs/math/example/policy_ref_snip9.cpp 2010-08-31 09:55:29 EDT (Tue, 31 Aug 2010)
@@ -1,4 +1,6 @@
 // Copyright John Maddock 2007.
+// Copyright Paul A. Bristow 2010
+
 // Use, modification and distribution are subject to the
 // Boost Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -7,21 +9,26 @@
 // and comments, don't change any of the special comment mark-ups!
 
 //[policy_ref_snip9
+#include <iostream>
+using std::cout; using std::endl;
 
 #include <boost/math/special_functions/gamma.hpp>
+using boost::math::tgamma;
+using boost::math::policies::policy;
+using boost::math::policies::digits10;
 
-using namespace boost::math;
-using namespace boost::math::policies;
-
-typedef policy<digits10<5> > pol;
-
-double t = tgamma(12, pol());
-
+typedef policy<digits10<5> > my_pol_5; // Define a new, non-default, policy
+// to calculate tgamma to accuracy of approximately 5 decimal digits.
 //]
 
-#include <iostream>
-
 int main()
 {
- std::cout << t << std::endl;
+ double t = tgamma(12, my_pol_5()); // Apply the 5 decimal digits accuracy policy to use of tgamma.
+ cout << "tgamma(12, my_pol_5() = " << t << endl;
 }
+
+/*
+
+Output:
+ tgamma(12, my_pol_5() = 3.99168e+007
+*/


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk