policy.qbk 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. [mathpart policy Policies: Controlling Precision, Error Handling etc]
  2. [section:pol_overview Policy Overview]
  3. [policy_overview]
  4. [endsect] [/section:pol_overview Policy Overview]
  5. [include policy_tutorial.qbk]
  6. [section:pol_ref Policy Reference]
  7. [section:error_handling_policies Error Handling Policies]
  8. There are two orthogonal aspects to error handling:
  9. * What to do (if anything) with the error.
  10. * What kind of error is being raised.
  11. [h4 Available Actions When an Error is Raised]
  12. What to do with the error is encapsulated by an enumerated type:
  13. namespace boost { namespace math { namespace policies {
  14. enum error_policy_type
  15. {
  16. throw_on_error = 0, // throw an exception.
  17. errno_on_error = 1, // set ::errno & return 0, NaN, infinity or best guess.
  18. ignore_error = 2, // return 0, NaN, infinity or best guess.
  19. user_error = 3 // call a user-defined error handler.
  20. };
  21. }}} // namespaces
  22. The various enumerated values have the following meanings:
  23. [h5 throw_on_error]
  24. Will throw one of the following exceptions, depending upon the
  25. type of the error:
  26. [table
  27. [[Error Type][Exception]]
  28. [[Domain Error][std::domain_error]]
  29. [[Pole Error][std::domain_error]]
  30. [[Overflow Error][std::overflow_error]]
  31. [[Underflow Error][std::underflow_error]]
  32. [[Denorm Error][std::underflow_error]]
  33. [[Evaluation Error][boost::math::evaluation_error]]
  34. [[Indeterminate Result Error][std::domain_error]]
  35. ]
  36. [h5 errno_on_error]
  37. Will set global __errno `::errno` to one of the following values depending
  38. upon the error type (often EDOM = 33 and ERANGE = 34),
  39. and then return the same value as if the error
  40. had been ignored:
  41. [table
  42. [[Error Type][errno value]]
  43. [[Domain Error][EDOM]]
  44. [[Pole Error][EDOM]]
  45. [[Overflow Error][ERANGE]]
  46. [[Underflow Error][ERANGE]]
  47. [[Denorm Error][ERANGE]]
  48. [[Evaluation Error][EDOM]]
  49. [[Indeterminate Result Error][EDOM]]
  50. ]
  51. [h5 ignore_error]
  52. Will return one of the values below depending on the error type (`::errno` is NOT changed)::
  53. [table
  54. [[Error Type][Returned Value]]
  55. [[Domain Error][std::numeric_limits<T>::quiet_NaN()]]
  56. [[Pole Error][std::numeric_limits<T>::quiet_NaN()]]
  57. [[Overflow Error][std::numeric_limits<T>::infinity()]]
  58. [[Underflow Error][0]]
  59. [[Denorm Error][The denormalised value.]]
  60. [[Evaluation Error][The best guess (perhaps NaN) as to the result: which
  61. may be significantly in error.]]
  62. [[Indeterminate Result Error][Depends on the function where the error occurred]]
  63. ]
  64. [h5 user_error]
  65. Will call a user defined error handler: these are forward declared
  66. in boost/math/policies/error_handling.hpp, but the actual definitions
  67. must be provided by the user:
  68. namespace boost{ namespace math{ namespace policies{
  69. template <class T>
  70. T user_domain_error(const char* function, const char* message, const T& val);
  71. template <class T>
  72. T user_pole_error(const char* function, const char* message, const T& val);
  73. template <class T>
  74. T user_overflow_error(const char* function, const char* message, const T& val);
  75. template <class T>
  76. T user_underflow_error(const char* function, const char* message, const T& val);
  77. template <class T>
  78. T user_denorm_error(const char* function, const char* message, const T& val);
  79. template <class T>
  80. T user_rounding_error(const char* function, const char* message, const T& val);
  81. template <class T>
  82. T user_evaluation_error(const char* function, const char* message, const T& val);
  83. template <class T>
  84. T user_indeterminate_result_error(const char* function, const char* message, const T& val);
  85. }}} // namespaces
  86. Note that the strings ['function] and ['message] may contain "%1%" format specifiers
  87. designed to be used in conjunction with Boost.Format.
  88. If these strings are to be presented to the program's end-user then
  89. the "%1%" format specifier
  90. should be replaced with the name of type T in the ['function] string, and
  91. if there is a %1% specifier in the ['message] string then it
  92. should be replaced with the value of ['val].
  93. There is more information on user-defined error handlers in
  94. the [link math_toolkit.pol_tutorial.user_def_err_pol
  95. tutorial here].
  96. [h4 Kinds of Error Raised]
  97. There are six kinds of error reported by this library,
  98. which are summarised in the following table:
  99. [table
  100. [[Error Type]
  101. [Policy Class]
  102. [Description]]
  103. [[Domain Error]
  104. [boost::math::policies::domain_error<['action]>]
  105. [Raised when more or more arguments are outside the
  106. defined range of the function.
  107. Defaults to `boost::math::policies::domain_error<throw_on_error>`
  108. When the action is set to ['throw_on_error]
  109. then throws `std::domain_error`]]
  110. [[Pole Error]
  111. [boost::math::policies::pole_error<['action]>]
  112. [Raised when more or more arguments would cause the function
  113. to be evaluated at a pole.
  114. Defaults to `boost::math::policies::pole_error<throw_on_error>`
  115. When the action is ['throw_on_error] then
  116. throw a `std::domain_error`]]
  117. [[Overflow Error]
  118. [boost::math::policies::overflow_error<['action]>]
  119. [Raised when the result of the function is outside
  120. the representable range of the floating point type used.
  121. Defaults to `boost::math::policies::overflow_error<throw_on_error>`.
  122. When the action is ['throw_on_error] then throws a `std::overflow_error`.]]
  123. [[Underflow Error]
  124. [boost::math::policies::underflow_error<['action]>]
  125. [Raised when the result of the function is too small
  126. to be represented in the floating point type used.
  127. Defaults to `boost::math::policies::underflow_error<ignore_error>`
  128. When the specified action is ['throw_on_error] then
  129. throws a `std::underflow_error`]]
  130. [[Denorm Error]
  131. [boost::math::policies::denorm_error<['action]>]
  132. [Raised when the result of the function is a
  133. denormalised value.
  134. Defaults to `boost::math::policies::denorm_error<ignore_error>`
  135. When the action is ['throw_on_error] then throws a `std::underflow_error`]]
  136. [[Rounding Error]
  137. [boost::math::policies::rounding_error<['action]>]
  138. [Raised When one of the rounding functions __round, __trunc or __modf is
  139. called with an argument that has no integer representation, or
  140. is too large to be represented in the result type
  141. Defaults to `boost::math::policies::rounding_error<throw_on_error>`
  142. When the action is ['throw_on_error] then throws `boost::math::rounding_error`]]
  143. [[Evaluation Error]
  144. [boost::math::policies::evaluation_error<['action]>]
  145. [Raised when the result of the function is well defined and
  146. finite, but we were unable to compute it. Typically
  147. this occurs when an iterative method fails to converge.
  148. Of course ideally this error should never be raised: feel free
  149. to report it as a bug if it is!
  150. Defaults to `boost::math::policies::evaluation_error<throw_on_error>`
  151. When the action is ['throw_on_error] then throws `boost::math::evaluation_error`]]
  152. [[Indeterminate Result Error]
  153. [boost::math::policies::indeterminate_result_error<['action]>]
  154. [Raised when the result of a function is not defined for the values that
  155. were passed to it.
  156. Defaults to `boost::math::policies::indeterminate_result_error<ignore_error>`
  157. When the action is ['throw_on_error] then throws `std::domain_error`]]
  158. ]
  159. [h4 Examples]
  160. Suppose we want a call to `tgamma` to behave in a C-compatible way and set global
  161. `::errno` rather than throw an exception, we can achieve this at the call site
  162. using:
  163. [import ../../example/policy_ref_snip1.cpp]
  164. [policy_ref_snip1]
  165. Suppose we want a statistical distribution to return infinities,
  166. rather than throw exceptions, then we can use:
  167. [import ../../example/policy_ref_snip2.cpp]
  168. [policy_ref_snip2]
  169. [endsect] [/section:error_handling_policies Error Handling Policies]
  170. [section:internal_promotion Internal Floating-point Promotion Policies]
  171. Normally when evaluating a function at say `float` precision, maximal
  172. accuracy is assured by conducting the calculation at `double` precision
  173. internally, and then rounding the result. There are two policies that
  174. control whether internal promotion to a higher precision floating-point type takes place, or not:
  175. [table
  176. [[Policy][Meaning]]
  177. [[`boost::math::policies::promote_float<B>`]
  178. [Indicates whether `float` arguments should be promoted to `double`
  179. precision internally: defaults to `boost::math::policies::promote_float<true>`]]
  180. [[`boost::math::policies::promote_double<B>`]
  181. [Indicates whether `double` arguments should be promoted to `long double`
  182. precision internally: defaults to `boost::math::policies::promote_double<true>`]]
  183. ]
  184. [h4 Examples]
  185. Suppose we want `tgamma` to be evaluated without internal promotion to
  186. `long double`, then we could use:
  187. [import ../../example/policy_ref_snip3.cpp]
  188. [policy_ref_snip3]
  189. Alternatively, suppose we want a distribution to perform calculations
  190. without promoting `float` to `double`, then we could use:
  191. [import ../../example/policy_ref_snip4.cpp]
  192. [policy_ref_snip4]
  193. [endsect] [/section:internal_promotion Internal Promotion Policies]
  194. [section:assert_undefined Mathematically Undefined Function Policies]
  195. There are some functions that are generic
  196. (they are present for all the statistical distributions supported)
  197. but which may be mathematically undefined for certain distributions, but defined for others.
  198. For example, the Cauchy distribution does not have a meaningful mean,
  199. so what should
  200. mean(cauchy<>());
  201. return, and should such an expression even compile at all?
  202. The default behaviour is for all such functions to not compile at all
  203. - in fact they will raise a
  204. [@http://www.boost.org/libs/static_assert/index.html static assertion]
  205. - but by changing the policy
  206. we can have them return the result of a domain error instead
  207. (which may well throw an exception, depending on the error handling policy).
  208. This behaviour is controlled by the `assert_undefined<>` policy:
  209. namespace boost{ namespace math{ namespace policies {
  210. template <bool b>
  211. class assert_undefined;
  212. }}} //namespaces
  213. For example:
  214. #include <boost/math/distributions/cauchy.hpp>
  215. using namespace boost::math::policies;
  216. using namespace boost::math;
  217. // This will not compile, cauchy has no mean!
  218. double m1 = mean(cauchy());
  219. // This will compile, but raises a domain error!
  220. double m2 = mean(cauchy_distribution<double, policy<assert_undefined<false> > >());
  221. `policy<assert_undefined<false>` behaviour can also be obtained by defining the macro
  222. #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
  223. at the head of the file - see __policy_macros.
  224. [endsect] [/section:assert_undefined Mathematically Undefined Function Policies]
  225. [section:discrete_quant_ref Discrete Quantile Policies]
  226. If a statistical distribution is ['discrete] then the random variable
  227. can only have integer values - this leaves us with a problem when calculating
  228. quantiles - we can either ignore the discreteness of the distribution and return
  229. a real value, or we can round to an integer. As it happens, computing integer
  230. values can be substantially faster than calculating a real value, so there are
  231. definite advantages to returning an integer, but we do then need to decide
  232. how best to round the result. The `discrete_quantile` policy defines how
  233. discrete quantiles work, and how integer results are rounded:
  234. enum discrete_quantile_policy_type
  235. {
  236. real,
  237. integer_round_outwards, // default
  238. integer_round_inwards,
  239. integer_round_down,
  240. integer_round_up,
  241. integer_round_nearest
  242. };
  243. template <discrete_quantile_policy_type>
  244. struct discrete_quantile;
  245. The values that `discrete_quantile` can take have the following meanings:
  246. [h5 real]
  247. Ignores the discreteness of the distribution, and returns a real-valued
  248. result. For example:
  249. [import ../../example/policy_ref_snip5.cpp]
  250. [policy_ref_snip5]
  251. Results in `x = 27.3898` and `y = 68.1584`.
  252. [h5 integer_round_outwards]
  253. This is the default policy: an integer value is returned so that:
  254. * Lower quantiles (where the probability is less than 0.5) are rounded
  255. down.
  256. * Upper quantiles (where the probability is greater than 0.5) are rounded up.
  257. This is normally the safest rounding policy, since it ensures that both
  258. one and two sided intervals are guaranteed to have ['at least]
  259. the requested coverage. For example:
  260. [import ../../example/policy_ref_snip6.cpp]
  261. [policy_ref_snip6]
  262. Results in `x = 27` (rounded down from 27.3898) and `y = 69` (rounded up from 68.1584).
  263. The variables x and y are now defined so that:
  264. cdf(negative_binomial(20), x) <= 0.05
  265. cdf(negative_binomial(20), y) >= 0.95
  266. In other words we guarantee ['at least 90% coverage in the central region overall],
  267. and also ['no more than 5% coverage in each tail].
  268. [h5 integer_round_inwards]
  269. This is the opposite of ['integer_round_outwards]: an integer value is returned so that:
  270. * Lower quantiles (where the probability is less than 0.5) are rounded
  271. ['up].
  272. * Upper quantiles (where the probability is greater than 0.5) are rounded ['down].
  273. For example:
  274. [import ../../example/policy_ref_snip7.cpp]
  275. [policy_ref_snip7]
  276. Results in `x = 28` (rounded up from 27.3898) and `y = 68` (rounded down from 68.1584).
  277. The variables x and y are now defined so that:
  278. cdf(negative_binomial(20), x) >= 0.05
  279. cdf(negative_binomial(20), y) <= 0.95
  280. In other words we guarantee ['at no more than 90% coverage in the central region overall],
  281. and also ['at least 5% coverage in each tail].
  282. [h5 integer_round_down]
  283. Always rounds down to an integer value, no matter whether it's an upper
  284. or a lower quantile.
  285. [h5 integer_round_up]
  286. Always rounds up to an integer value, no matter whether it's an upper
  287. or a lower quantile.
  288. [h5 integer_round_nearest]
  289. Always rounds to the nearest integer value, no matter whether it's an upper
  290. or a lower quantile. This will produce the requested coverage
  291. ['in the average case], but for any specific example may results in
  292. either significantly more or less coverage than the requested amount.
  293. For example:
  294. For example:
  295. [import ../../example/policy_ref_snip8.cpp]
  296. [policy_ref_snip8]
  297. Results in `x = 27` (rounded from 27.3898) and `y = 68` (rounded from 68.1584).
  298. [endsect] [/section:discrete_quant_ref Discrete Quantile Policies]
  299. [section:precision_pol Precision Policies]
  300. There are two equivalent policies that effect the ['working precision]
  301. used to calculate results, these policies both default to 0 - meaning
  302. calculate to the maximum precision available in the type being used
  303. - but can be set to other values to cause lower levels of precision
  304. to be used. One might want to trade precision for evaluation speed.
  305. namespace boost{ namespace math{ namespace policies{
  306. template <int N>
  307. digits10;
  308. template <int N>
  309. digits2;
  310. }}} // namespaces
  311. As you would expect, ['digits10] specifies the number of decimal digits
  312. to use, and ['digits2] the number of binary digits. Internally, whichever
  313. is used, the precision is always converted to ['binary digits].
  314. These policies are specified at compile-time, because many of the special
  315. functions use compile-time-dispatch to select which approximation to use
  316. based on the precision requested and the numeric type being used.
  317. For example we could calculate `tgamma` to approximately 5 decimal digits using:
  318. [import ../../example/policy_ref_snip9.cpp]
  319. [policy_ref_snip9]
  320. Or again using helper function `make_policy`:
  321. [import ../../example/policy_ref_snip10.cpp]
  322. [policy_ref_snip10]
  323. And for a quantile of a distribution to approximately 25-bit precision:
  324. [import ../../example/policy_ref_snip11.cpp]
  325. [policy_ref_snip11]
  326. [endsect] [/section:precision_pol Precision Policies]
  327. [section:iteration_pol Iteration Limits Policies]
  328. There are two policies that effect the iterative algorithms
  329. used to implement the special functions in this library:
  330. template <unsigned long limit = BOOST_MATH_MAX_SERIES_ITERATION_POLICY>
  331. class max_series_iterations;
  332. template <unsigned long limit = BOOST_MATH_MAX_ROOT_ITERATION_POLICY>
  333. class max_root_iterations;
  334. The class `max_series_iterations` determines the maximum number of
  335. iterations permitted in a series evaluation, before the special
  336. function gives up and returns the result of __evaluation_error.
  337. The class `max_root_iterations` determines the maximum number
  338. of iterations permitted in a root-finding algorithm before the special
  339. function gives up and returns the result of __evaluation_error.
  340. [endsect] [/section:iteration_pol Iteration Limits Policies]
  341. [section:policy_defaults Using Macros to Change the Policy Defaults]
  342. You can use the various macros below to change any (or all) of the policies.
  343. You can make a local change by placing a macro definition *before*
  344. a function or distribution #include.
  345. [caution There is a danger of One-Definition-Rule violations if you
  346. add ad-hoc macros to more than one source files: these must be set the same in *every
  347. translation unit*.]
  348. [caution If you place it after the #include it will have no effect,
  349. (and it will affect only any other following #includes).
  350. This is probably not what you intend!]
  351. If you want to alter the defaults for any or all of
  352. the policies for *all* functions and distributions, installation-wide,
  353. then you can do so by defining various macros in
  354. [@../../../../boost/math/tools/user.hpp boost/math/tools/user.hpp].
  355. [h5 BOOST_MATH_DOMAIN_ERROR_POLICY]
  356. Defines what happens when a domain error occurs, if not defined then
  357. defaults to `throw_on_error`, but can be set to any of the enumerated
  358. actions for error handing: `throw_on_error`, `errno_on_error`,
  359. `ignore_error` or `user_error`.
  360. [h5 BOOST_MATH_POLE_ERROR_POLICY]
  361. Defines what happens when a pole error occurs, if not defined then
  362. defaults to `throw_on_error`, but can be set to any of the enumerated
  363. actions for error handing: `throw_on_error`, `errno_on_error`,
  364. `ignore_error` or `user_error`.
  365. [h5 BOOST_MATH_OVERFLOW_ERROR_POLICY]
  366. Defines what happens when an overflow error occurs, if not defined then
  367. defaults to `throw_on_error`, but can be set to any of the enumerated
  368. actions for error handing: `throw_on_error`, `errno_on_error`,
  369. `ignore_error` or `user_error`.
  370. [h5 BOOST_MATH_ROUNDING_ERROR_POLICY]
  371. Defines what happens when a rounding error occurs, if not defined then
  372. defaults to `throw_on_error`, but can be set to any of the enumerated
  373. actions for error handing: `throw_on_error`, `errno_on_error`,
  374. `ignore_error` or `user_error`.
  375. [h5 BOOST_MATH_EVALUATION_ERROR_POLICY]
  376. Defines what happens when an internal evaluation error occurs, if not defined then
  377. defaults to `throw_on_error`, but can be set to any of the enumerated
  378. actions for error handing: `throw_on_error`, `errno_on_error`,
  379. `ignore_error` or `user_error`.
  380. [h5 BOOST_MATH_UNDERFLOW_ERROR_POLICY]
  381. Defines what happens when an overflow error occurs, if not defined then
  382. defaults to `ignore_error`, but can be set to any of the enumerated
  383. actions for error handing: `throw_on_error`, `errno_on_error`,
  384. `ignore_error` or `user_error`.
  385. [h5 BOOST_MATH_DENORM_ERROR_POLICY]
  386. Defines what happens when a denormalisation error occurs, if not defined then
  387. defaults to `ignore_error`, but can be set to any of the enumerated
  388. actions for error handing: `throw_on_error`, `errno_on_error`,
  389. `ignore_error` or `user_error`.
  390. [h5 BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY]
  391. Defines what happens when the result is indeterminate, but where there
  392. is none the less a convention for the result. If not defined then
  393. defaults to `ignore_error`, but can be set to any of the enumerated
  394. actions for error handing: `throw_on_error`, `errno_on_error`,
  395. `ignore_error` or `user_error`.
  396. [h5 BOOST_MATH_DIGITS10_POLICY]
  397. Defines how many decimal digits to use in internal computations:
  398. defaults to `0` - meaning use all available digits - but can be set
  399. to some other decimal value. Since setting this is likely to have
  400. a substantial impact on accuracy, it's not generally recommended
  401. that you change this from the default.
  402. [h5 BOOST_MATH_PROMOTE_FLOAT_POLICY]
  403. Determines whether `float` types get promoted to `double`
  404. internally to ensure maximum precision in the result, defaults
  405. to `true`, but can be set to `false` to turn promotion of
  406. `float`'s off.
  407. [h5 BOOST_MATH_PROMOTE_DOUBLE_POLICY]
  408. Determines whether `double` types get promoted to `long double`
  409. internally to ensure maximum precision in the result, defaults
  410. to `true`, but can be set to `false` to turn promotion of
  411. `double`'s off.
  412. [h5 BOOST_MATH_DISCRETE_QUANTILE_POLICY]
  413. Determines how discrete quantiles return their results: either
  414. as an integer, or as a real value, can be set to one of the
  415. enumerated values: `real`, `integer_round_outwards`, `integer_round_inwards`,
  416. `integer_round_down`, `integer_round_up`, `integer_round_nearest`. Defaults to
  417. `integer_round_outwards`.
  418. [h5 BOOST_MATH_ASSERT_UNDEFINED_POLICY]
  419. Determines whether functions that are mathematically undefined
  420. for a specific distribution compile or raise a static (i.e. compile-time)
  421. assertion. Defaults to `true`: meaning that any mathematically
  422. undefined function will not compile. When set to `false` then the function
  423. will compile but return the result of a domain error: this can be useful
  424. for some generic code, that needs to work with all distributions and determine
  425. at runtime whether or not a particular property is well defined.
  426. [h5 BOOST_MATH_MAX_SERIES_ITERATION_POLICY]
  427. Determines how many series iterations a special function is permitted
  428. to perform before it gives up and returns an __evaluation_error:
  429. Defaults to 1000000.
  430. [h5 BOOST_MATH_MAX_ROOT_ITERATION_POLICY]
  431. Determines how many root-finding iterations a special function is permitted
  432. to perform before it gives up and returns an __evaluation_error:
  433. Defaults to 200.
  434. [h5 Example]
  435. Suppose we want overflow errors to set `::errno` and return an infinity,
  436. discrete quantiles to return a real-valued result (rather than round to
  437. integer), and for mathematically undefined functions to compile, but return
  438. a domain error. Then we could add the following to boost/math/tools/user.hpp:
  439. #define BOOST_MATH_OVERFLOW_ERROR_POLICY errno_on_error
  440. #define BOOST_MATH_DISCRETE_QUANTILE_POLICY real
  441. #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
  442. or we could place these definitions *before*
  443. #include <boost/math/distributions/normal.hpp>
  444. using boost::math::normal_distribution;
  445. in a source .cpp file.
  446. [endsect] [/section:policy_defaults Changing the Policy Defaults]
  447. [section:namespace_pol Setting Polices at Namespace Scope]
  448. Sometimes what you really want to do is bring all the special functions,
  449. or all the distributions into a specific namespace-scope, along with
  450. a specific policy to use with them. There are two macros defined to
  451. assist with that:
  452. BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS(Policy)
  453. and:
  454. BOOST_MATH_DECLARE_DISTRIBUTIONS(Type, Policy)
  455. You can use either of these macros after including any special function
  456. or distribution header. For example:
  457. [import ../../example/policy_ref_snip12.cpp]
  458. [policy_ref_snip12]
  459. In this example, using BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS results in
  460. a set of thin inline forwarding functions being defined:
  461. template <class T>
  462. inline T tgamma(T a){ return ::boost::math::tgamma(a, mypolicy()); }
  463. template <class T>
  464. inline T lgamma(T a) ( return ::boost::math::lgamma(a, mypolicy()); }
  465. and so on. Note that while a forwarding function is defined for all the special
  466. functions, however, unless you include the specific header for the special
  467. function you use (or boost/math/special_functions.hpp to include everything),
  468. you will get linker errors from functions that are forward declared, but not
  469. defined.
  470. We can do the same thing with the distributions, but this time we need to
  471. specify the floating-point type to use:
  472. [import ../../example/policy_ref_snip13.cpp]
  473. [policy_ref_snip13]
  474. In this example the result of BOOST_MATH_DECLARE_DISTRIBUTIONS is to
  475. declare a typedef for each distribution like this:
  476. typedef boost::math::cauchy_distribution<double, my_policy> cauchy;
  477. tyepdef boost::math::gamma_distribution<double, my_policy> gamma;
  478. and so on. The name given to each typedef is the name of the distribution
  479. with the "_distribution" suffix removed.
  480. [endsect] [/section Changing the Policy Defaults]
  481. [section:pol_ref_ref Policy Class Reference]
  482. There's very little to say here, the `policy` class is just a rag-bag
  483. compile-time container for a collection of policies:
  484. ```#include <boost/math/policies/policy.hpp>```
  485. namespace boost{
  486. namespace math{
  487. namespace policies
  488. template <class A1 = default_policy,
  489. class A2 = default_policy,
  490. class A3 = default_policy,
  491. class A4 = default_policy,
  492. class A5 = default_policy,
  493. class A6 = default_policy,
  494. class A7 = default_policy,
  495. class A8 = default_policy,
  496. class A9 = default_policy,
  497. class A10 = default_policy,
  498. class A11 = default_policy,
  499. class A12 = default_policy,
  500. class A13 = default_policy>
  501. struct policy
  502. {
  503. public:
  504. typedef ``['computed-from-template-arguments]`` domain_error_type;
  505. typedef ``['computed-from-template-arguments]`` pole_error_type;
  506. typedef ``['computed-from-template-arguments]`` overflow_error_type;
  507. typedef ``['computed-from-template-arguments]`` underflow_error_type;
  508. typedef ``['computed-from-template-arguments]`` denorm_error_type;
  509. typedef ``['computed-from-template-arguments]`` rounding_error_type;
  510. typedef ``['computed-from-template-arguments]`` evaluation_error_type;
  511. typedef ``['computed-from-template-arguments]`` indeterminate_result_error_type;
  512. typedef ``['computed-from-template-arguments]`` precision_type;
  513. typedef ``['computed-from-template-arguments]`` promote_float_type;
  514. typedef ``['computed-from-template-arguments]`` promote_double_type;
  515. typedef ``['computed-from-template-arguments]`` discrete_quantile_type;
  516. typedef ``['computed-from-template-arguments]`` assert_undefined_type;
  517. };
  518. template <...argument list...>
  519. typename normalise<policy<>, A1>::type make_policy(...argument list..);
  520. template <class Policy,
  521. class A1 = default_policy,
  522. class A2 = default_policy,
  523. class A3 = default_policy,
  524. class A4 = default_policy,
  525. class A5 = default_policy,
  526. class A6 = default_policy,
  527. class A7 = default_policy,
  528. class A8 = default_policy,
  529. class A9 = default_policy,
  530. class A10 = default_policy,
  531. class A11 = default_policy,
  532. class A12 = default_policy,
  533. class A13 = default_policy>
  534. struct normalise
  535. {
  536. typedef ``computed-from-template-arguments`` type;
  537. };
  538. The member typedefs of class `policy` are intended for internal use
  539. but are documented briefly here for the sake of completeness.
  540. policy<...>::domain_error_type
  541. Specifies how domain errors are handled, will be an instance of
  542. `boost::math::policies::domain_error<>` with the template argument to
  543. `domain_error` one of the `error_policy_type` enumerated values.
  544. policy<...>::pole_error_type
  545. Specifies how pole-errors are handled, will be an instance of
  546. `boost::math::policies::pole_error<>` with the template argument to
  547. `pole_error` one of the `error_policy_type` enumerated values.
  548. policy<...>::overflow_error_type
  549. Specifies how overflow errors are handled, will be an instance of
  550. `boost::math::policies::overflow_error<>` with the template argument to
  551. `overflow_error` one of the `error_policy_type` enumerated values.
  552. policy<...>::underflow_error_type
  553. Specifies how underflow errors are handled, will be an instance of
  554. `boost::math::policies::underflow_error<>` with the template argument to
  555. `underflow_error` one of the `error_policy_type` enumerated values.
  556. policy<...>::denorm_error_type
  557. Specifies how denorm errors are handled, will be an instance of
  558. `boost::math::policies::denorm_error<>` with the template argument to
  559. `denorm_error` one of the `error_policy_type` enumerated values.
  560. policy<...>::rounding_error_type
  561. Specifies how rounding errors are handled, will be an instance of
  562. `boost::math::policies::rounding_error<>` with the template argument to
  563. `rounding_error` one of the `error_policy_type` enumerated values.
  564. policy<...>::evaluation_error_type
  565. Specifies how evaluation errors are handled, will be an instance of
  566. `boost::math::policies::evaluation_error<>` with the template argument to
  567. `evaluation_error` one of the `error_policy_type` enumerated values.
  568. policy<...>::indeterminate_error_type
  569. Specifies how indeterminate result errors are handled, will be an instance of
  570. `boost::math::policies::indeterminate_result_error<>` with the template argument
  571. to `indeterminate_result_error` one of the `error_policy_type` enumerated
  572. values.
  573. policy<...>::precision_type
  574. Specifies the internal precision to use in binary digits (uses zero
  575. to represent whatever the default precision is). Will be an instance
  576. of `boost::math::policies::digits2<N>` which in turn inherits from
  577. `boost::mpl::int_<N>`.
  578. policy<...>::promote_float_type
  579. Specifies whether or not to promote `float` arguments to `double` precision
  580. internally. Will be an instance of `boost::math::policies::promote_float<B>`
  581. which in turn inherits from `boost::mpl::bool_<B>`.
  582. policy<...>::promote_double_type
  583. Specifies whether or not to promote `double` arguments to `long double` precision
  584. internally. Will be an instance of `boost::math::policies::promote_float<B>`
  585. which in turn inherits from `boost::mpl::bool_<B>`.
  586. policy<...>::discrete_quantile_type
  587. Specifies how discrete quantiles are evaluated, will be an instance
  588. of `boost::math::policies::discrete_quantile<>` instantiated with one of
  589. the `discrete_quantile_policy_type` enumerated type.
  590. policy<...>::assert_undefined_type
  591. Specifies whether mathematically-undefined properties are
  592. asserted as compile-time errors, or treated as runtime errors
  593. instead. Will be an instance of `boost::math::policies::assert_undefined<B>`
  594. which in turn inherits from `boost::math::mpl::bool_<B>`.
  595. template <...argument list...>
  596. typename normalise<policy<>, A1>::type make_policy(...argument list..);
  597. `make_policy` is a helper function that converts a list of policies into
  598. a normalised `policy` class.
  599. template <class Policy,
  600. class A1 = default_policy,
  601. class A2 = default_policy,
  602. class A3 = default_policy,
  603. class A4 = default_policy,
  604. class A5 = default_policy,
  605. class A6 = default_policy,
  606. class A7 = default_policy,
  607. class A8 = default_policy,
  608. class A9 = default_policy,
  609. class A10 = default_policy,
  610. class A11 = default_policy,
  611. class A12 = default_policy,
  612. class A13 = default_policy>
  613. struct normalise
  614. {
  615. typedef ``computed-from-template-arguments`` type;
  616. };
  617. The `normalise` class template converts one instantiation of the
  618. `policy` class into a normalised form. This is used internally
  619. to reduce code bloat: so that instantiating a special function
  620. on `policy<A,B>` or `policy<B,A>` actually both generate the same
  621. code internally.
  622. Further more, `normalise` can be used to combine
  623. a policy with one or more policies: for example many of the
  624. special functions will use this to set policies which they don't
  625. make use of to their default values, before forwarding to the actual
  626. implementation. In this way code bloat is reduced, since the
  627. actual implementation depends only on the policy types that they
  628. actually use.
  629. [endsect] [/section:pol_ref_ref Policy Class Reference]
  630. [endsect] [/section:pol_ref Policy Reference]
  631. [endmathpart] [/section:policy Policies]
  632. [/ policy.qbk
  633. Copyright 2007, 2010 John Maddock and Paul A. Bristow.
  634. Distributed under the Boost Software License, Version 1.0.
  635. (See accompanying file LICENSE_1_0.txt or copy at
  636. http://www.boost.org/LICENSE_1_0.txt).
  637. ]