⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 error_handling_example.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
字号:
// example_error_handling.cpp// Copyright Paul A. Bristow 2007.// Copyright John Maddock 2007.// 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)// Note that this file contains quickbook markup as well as code// and comments, don't change any of the special comment markups!//[error_handling_example/*`The following example demonstrates the effect ofsetting the macro BOOST_MATH_DOMAIN_ERROR_POLICYwhen an invalid argument is encountered.  For thepurposes of this example, we'll pass a negativedegrees of freedom parameter to the student's tdistribution.Since we know that this is a single file program we couldjust add:   #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_errorto the top of the source file to change the default policyto one that simply returns a NaN when a domain error occurs.Alternatively we could use:   #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_errorTo ensure the `::errno` is set when a domain error occursas well as returning a NaN.This is safe provided the program consists of a singletranslation unit /and/ we place the define /before/ any#includes.  Note that should we add the define after the includesthen it will have no effect!  A warning such as:[pre warning C4005: 'BOOST_MATH_OVERFLOW_ERROR_POLICY' : macro redefinition]is a certain sign that it will /not/ have the desired effect.We'll begin our sample program with the needed includes:*/// Boost#include <boost/math/distributions/students_t.hpp>   using boost::math::students_t;  // Probability of students_t(df, t).// std#include <iostream>   using std::cout;   using std::endl;#include <stdexcept>   using std::exception;/*`Next we'll define the program's main() to call the student's tdistribution with an invalid degrees of freedom parameter, the programis set up to handle either an exception or a NaN:*/int main(){   cout << "Example error handling using Student's t function. " << endl;   cout << "BOOST_MATH_DOMAIN_ERROR_POLICY is set to: "      << BOOST_STRINGIZE(BOOST_MATH_DOMAIN_ERROR_POLICY) << endl;   double degrees_of_freedom = -1; // A bad argument!   double t = 10;   try   {      errno = 0;      students_t dist(degrees_of_freedom); // exception is thrown here if enabled      double p = cdf(dist, t);      // test for error reported by other means:      if((boost::math::isnan)(p))      {         cout << "cdf returned a NaN!" << endl;         cout << "errno is set to: " << errno << endl;      }      else         cout << "Probability of Student's t is " << p << endl;   }   catch(const std::exception& e)   {      std::cout <<         "\n""Message from thrown exception was:\n   " << e.what() << std::endl;   }   return 0;} // int main()/*`Here's what the program output looks like with a default build(one that does throw exceptions):[preExample error handling using Student's t function.BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_errorMessage from thrown exception was:   Error in function boost::math::students_t_distribution<double>::students_t_distribution:   Degrees of freedom argument is -1, but must be > 0 !]Alternatively let's build with:   #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_errorNow the program output is:[preExample error handling using Student's t function.BOOST_MATH_DOMAIN_ERROR_POLICY is set to: ignore_errorcdf returned a NaN!errno is set to: 0]And finally let's build with:   #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_errorWhich gives the output:[preExample error handling using Student's t function.BOOST_MATH_DOMAIN_ERROR_POLICY is set to: errno_on_errorcdf returned a NaN!errno is set to: 33]*///] [error_handling_eg end quickbook markup]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -