error_handling.qbk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. [section:error_handling Error Handling]
  2. [def __format [@../../../format/index.html Boost.Format]]
  3. [heading Quick Reference]
  4. Handling of errors by this library is split into two orthogonal parts:
  5. * What kind of error has been raised?
  6. * What should be done when the error is raised?
  7. [warning The default error actions are to throw an exception with an informative error message.
  8. [role red If you do not try to catch the exception, you will not see the message!]]
  9. The kinds of errors that can be raised are:
  10. [variablelist
  11. [[Domain Error][Occurs when one or more arguments to a function
  12. are out of range.]]
  13. [[Pole Error][Occurs when the particular arguments cause the function to be
  14. evaluated at a pole with no well defined residual value. For example if
  15. __tgamma is evaluated at exactly -2, the function approaches different limiting
  16. values depending upon whether you approach from just above or just below
  17. -2. Hence the function has no well defined value at this point and a
  18. Pole Error will be raised.]]
  19. [[Overflow Error][Occurs when the result is either infinite, or too large
  20. to represent in the numeric type being returned by the function.]]
  21. [[Underflow Error][Occurs when the result is not zero, but is too small
  22. to be represented by any other value in the type being returned by
  23. the function.]]
  24. [[Denormalisation Error][Occurs when the returned result would be a denormalised value.]]
  25. [[Rounding Error][Occurs when the argument to one of the rounding functions __trunc,
  26. __round and __modf can not be represented as an integer type, is
  27. outside the range of the result type.]]
  28. [[Evaluation Error][Occurs if no method of evaluation is known,
  29. or when an internal error occurred that prevented the
  30. result from being evaluated: this should never occur, but if it does, then
  31. it's likely to be due to an iterative method not converging fast enough.]]
  32. [[Indeterminate Result Error][Occurs when the result of a function is not
  33. defined for the values that were passed to it.]]
  34. ]
  35. The action undertaken by each error condition is determined by the current
  36. __Policy in effect. This can be changed program-wide by setting some
  37. configuration macros, or at namespace scope, or at the call site (by
  38. specifying a specific policy in the function call).
  39. The available actions are:
  40. [variablelist
  41. [[throw_on_error][Throws the exception most appropriate to the error condition.]]
  42. [[errno_on_error][Sets ::errno to an appropriate value, and then returns the most
  43. appropriate result]]
  44. [[ignore_error][Ignores the error and simply the returns the most appropriate result.]]
  45. [[user_error][Calls a
  46. [link math_toolkit.pol_tutorial.user_def_err_pol user-supplied error handler].]]
  47. ]
  48. The following tables show all the permutations of errors and actions,
  49. with the *default action for each error shown in bold*:
  50. [table Possible Actions for Domain Errors
  51. [[Action] [Behaviour]]
  52. [[throw_on_error][[*Throws `std::domain_error`]]]
  53. [[errno_on_error][Sets `::errno` to `EDOM` and returns `std::numeric_limits<T>::quiet_NaN()`]]
  54. [[ignore_error][Returns `std::numeric_limits<T>::quiet_NaN()`]]
  55. [[user_error][Returns the result of `boost::math::policies::user_domain_error`:
  56. [link math_toolkit.pol_tutorial.user_def_err_pol
  57. this function must be defined by the user].]]
  58. ]
  59. [table Possible Actions for Pole Errors
  60. [[Action] [Behaviour]]
  61. [[throw_on_error] [[*Throws `std::domain_error`]]]
  62. [[errno_on_error][Sets `::errno` to `EDOM` and returns `std::numeric_limits<T>::quiet_NaN()`]]
  63. [[ignore_error][Returns `std::numeric_limits<T>::quiet_NaN()`]]
  64. [[user_error][Returns the result of `boost::math::policies::user_pole_error`:
  65. [link math_toolkit.pol_tutorial.user_def_err_pol
  66. this function must be defined by the user].]]
  67. ]
  68. [table Possible Actions for Overflow Errors
  69. [[Action] [Behaviour]]
  70. [[throw_on_error][[*Throws `std::overflow_error`]]]
  71. [[errno_on_error][Sets `::errno` to `ERANGE` and returns `std::numeric_limits<T>::infinity()`]]
  72. [[ignore_error][Returns `std::numeric_limits<T>::infinity()`]]
  73. [[user_error][Returns the result of `boost::math::policies::user_overflow_error`:
  74. [link math_toolkit.pol_tutorial.user_def_err_pol
  75. this function must be defined by the user].]]
  76. ]
  77. [table Possible Actions for Underflow Errors
  78. [[Action] [Behaviour]]
  79. [[throw_on_error][Throws `std::underflow_error`]]
  80. [[errno_on_error][Sets `::errno` to `ERANGE` and returns 0.]]
  81. [[ignore_error][[*Returns 0]]]
  82. [[user_error][Returns the result of `boost::math::policies::user_underflow_error`:
  83. [link math_toolkit.pol_tutorial.user_def_err_pol
  84. this function must be defined by the user].]]
  85. ]
  86. [table Possible Actions for Denorm Errors
  87. [[Action] [Behaviour]]
  88. [[throw_on_error][Throws `std::underflow_error`]]
  89. [[errno_on_error][Sets `::errno` to `ERANGE` and returns the denormalised value.]]
  90. [[ignore_error][[*Returns the denormalised value.]]]
  91. [[user_error][Returns the result of `boost::math::policies::user_denorm_error`:
  92. [link math_toolkit.pol_tutorial.user_def_err_pol
  93. this function must be defined by the user].]]
  94. ]
  95. [table Possible Actions for Rounding Errors
  96. [[Action] [Behaviour]]
  97. [[throw_on_error][Throws `boost::math::rounding_error`]]
  98. [[errno_on_error][Sets `::errno` to `ERANGE` and returns the largest representable value of the target integer type
  99. (or the most negative value if the argument to the function was less than zero).]]
  100. [[ignore_error][[*Returns the largest representable value of the target integer type
  101. (or the most negative value if the argument to the function was less than zero).]]]
  102. [[user_error][Returns the result of `boost::math::policies::user_rounding_error`:
  103. [link math_toolkit.pol_tutorial.user_def_err_pol
  104. this function must be defined by the user].]]
  105. ]
  106. [table Possible Actions for Internal Evaluation Errors
  107. [[Action] [Behaviour]]
  108. [[throw_on_error][[*Throws `boost::math::evaluation_error`]]]
  109. [[errno_on_error][Sets `::errno` to `EDOM` and returns the closest approximation found.]]
  110. [[ignore_error][Returns the closest approximation found.]]
  111. [[user_error][Returns the result of `boost::math::policies::user_evaluation_error`:
  112. [link math_toolkit.pol_tutorial.user_def_err_pol
  113. this function must be defined by the user].]]
  114. ]
  115. [table Possible Actions for Indeterminate Result Errors
  116. [[Action] [Behaviour]]
  117. [[throw_on_error][Throws `std::domain_error`]]
  118. [[errno_on_error][Sets `::errno` to `EDOM` and returns the same value as `ignore_error`.]]
  119. [[ignore_error][[*Returns a default result that depends on the function where the error occurred.]]]
  120. [[user_error][Returns the result of `boost::math::policies::user_indeterminate_result_error`:
  121. [link math_toolkit.pol_tutorial.user_def_err_pol
  122. this function must be defined by the user].]]
  123. ]
  124. All these error conditions are in namespace boost::math::policies,
  125. made available, for example, a by namespace declaration
  126. using `namespace boost::math::policies;` or individual using declarations
  127. `using boost::math::policies::overflow_error;`.
  128. [heading Rationale]
  129. The flexibility of the current implementation should be reasonably obvious: the
  130. default behaviours were chosen based on feedback during the formal review of
  131. this library. It was felt that:
  132. * Genuine errors should be flagged with exceptions
  133. rather than following C-compatible behaviour and setting `::errno`.
  134. * Numeric underflow and denormalised results were not considered to be
  135. fatal errors in most cases, so it was felt that these should be ignored.
  136. * If there is more than one error,
  137. only the first detected will be reported in the throw message.
  138. [heading Finding More Information]
  139. There are some pre-processor macro defines that can be used to
  140. [link math_toolkit.pol_ref.policy_defaults
  141. change the policy defaults]. See also the [link policy
  142. policy section].
  143. An example is at the Policy tutorial in
  144. [link math_toolkit.pol_tutorial.changing_policy_defaults
  145. Changing the Policy Defaults].
  146. Full source code of this typical example of passing a 'bad' argument
  147. (negative degrees of freedom) to Student's t distribution
  148. is [link math_toolkit.stat_tut.weg.error_eg in the error handling example].
  149. The various kind of errors are described in more detail below.
  150. [heading:domain_error Domain Errors]
  151. When a special function is passed an argument that is outside the range
  152. of values for which that function is defined, then the function returns
  153. the result of:
  154. boost::math::policies::raise_domain_error<T>(FunctionName, Message, Val, __Policy);
  155. Where
  156. `T` is the floating-point type passed to the function, `FunctionName` is the
  157. name of the function, `Message` is an error message describing the problem,
  158. Val is the value that was out of range, and __Policy is the current policy
  159. in use for the function that was called.
  160. The default policy behaviour of this function is to throw a
  161. std::domain_error C++ exception. But if the __Policy is to ignore
  162. the error, or set global `::errno`, then a NaN will be returned.
  163. This behaviour is chosen to assist compatibility with the behaviour of
  164. ['ISO/IEC 9899:1999 Programming languages - C]
  165. and with the
  166. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf Draft Technical Report on C++ Library Extensions, 2005-06-24, section 5.2.1, paragraph 6]:
  167. [:['\"Each of the functions declared above shall return a NaN (Not a Number)
  168. if any argument value is a NaN, but it shall not report a domain error.
  169. Otherwise, each of the functions declared above shall report a domain error
  170. for just those argument values for which:[br]
  171. the function description's Returns clause explicitly specifies a domain, and those arguments fall outside the specified domain; or [br]
  172. the corresponding mathematical function value has a non-zero imaginary component; or [br]
  173. the corresponding mathematical function is not mathematically defined. [br]
  174. Note 2: A mathematical function is mathematically defined
  175. for a given set of argument values if it is explicitly defined
  176. for that set of argument values or
  177. if its limiting value exists and does not depend on the direction of approach.\"]]
  178. Note that in order to support information-rich error messages when throwing
  179. exceptions, `Message` must contain
  180. a __format recognised format specifier: the argument `Val` is inserted into
  181. the error message according to the specifier used.
  182. For example if `Message` contains a "%1%" then it is replaced by the value of
  183. `Val` to the full precision of T, where as "%.3g" would contain the value of
  184. `Val` to 3 digits. See the __format documentation for more details.
  185. [heading:pole_error Evaluation at a pole]
  186. When a special function is passed an argument that is at a pole
  187. without a well defined residual value, then the function returns
  188. the result of:
  189. boost::math::policies::raise_pole_error<T>(FunctionName, Message, Val, __Policy);
  190. Where
  191. `T` is the floating point type passed to the function, `FunctionName` is the
  192. name of the function, `Message` is an error message describing the problem,
  193. `Val` is the value of the argument that is at a pole, and __Policy is the
  194. current policy in use for the function that was called.
  195. The default behaviour of this function is to throw a std::domain_error exception.
  196. But __error_policy can be used to change this, for example to `ignore_error`
  197. and return NaN.
  198. Note that in order to support information-rich error messages when throwing
  199. exceptions, `Message` must contain
  200. a __format recognised format specifier: the argument `val` is inserted into
  201. the error message according to the specifier used.
  202. For example if `Message` contains a "%1%" then it is replaced by the value of
  203. `val` to the full precision of T, where as "%.3g" would contain the value of
  204. `val` to 3 digits. See the __format documentation for more details.
  205. [heading:overflow_error Numeric Overflow]
  206. When the result of a special function is too large to fit in the argument
  207. floating-point type, then the function returns the result of:
  208. boost::math::policies::raise_overflow_error<T>(FunctionName, Message, __Policy);
  209. Where
  210. `T` is the floating-point type passed to the function, `FunctionName` is the
  211. name of the function, `Message` is an error message describing the problem,
  212. and __Policy is the current policy
  213. in use for the function that was called.
  214. The default policy for this function is that `std::overflow_error`
  215. C++ exception is thrown. But if, for example, an `ignore_error` policy
  216. is used, then returns `std::numeric_limits<T>::infinity()`.
  217. In this situation if the type `T` doesn't support infinities,
  218. the maximum value for the type is returned.
  219. [heading:underflow_error Numeric Underflow]
  220. If the result of a special function is known to be non-zero, but the
  221. calculated result underflows to zero, then the function returns the result of:
  222. boost::math::policies::raise_underflow_error<T>(FunctionName, Message, __Policy);
  223. Where
  224. `T` is the floating point type passed to the function, `FunctionName` is the
  225. name of the function, `Message` is an error message describing the problem,
  226. and __Policy is the current policy
  227. in use for the called function.
  228. The default version of this function returns zero.
  229. But with another policy, like `throw_on_error`,
  230. throws an `std::underflow_error` C++ exception.
  231. [heading:denorm_error Denormalisation Errors]
  232. If the result of a special function is a denormalised value /z/ then the function
  233. returns the result of:
  234. boost::math::policies::raise_denorm_error<T>(z, FunctionName, Message, __Policy);
  235. Where
  236. `T` is the floating point type passed to the function, `FunctionName` is the
  237. name of the function, `Message` is an error message describing the problem,
  238. and __Policy is the current policy
  239. in use for the called function.
  240. The default version of this function returns /z/.
  241. But with another policy, like `throw_on_error`
  242. throws an `std::underflow_error` C++ exception.
  243. [heading:evaluation_error Evaluation Errors]
  244. When a special function calculates a result that is known to be erroneous,
  245. or where the result is incalculable then it calls:
  246. boost::math::policies::raise_evaluation_error<T>(FunctionName, Message, Val, __Policy);
  247. Where
  248. `T` is the floating point type passed to the function, `FunctionName` is the
  249. name of the function, `Message` is an error message describing the problem,
  250. `Val` is the erroneous value,
  251. and __Policy is the current policy
  252. in use for the called function.
  253. The default behaviour of this function is to throw a `boost::math::evaluation_error`.
  254. Note that in order to support information rich error messages when throwing
  255. exceptions, `Message` must contain
  256. a __format recognised format specifier: the argument `val` is inserted into
  257. the error message according to the specifier used.
  258. For example if `Message` contains a "%1%" then it is replaced by the value of
  259. `val` to the full precision of T, where as "%.3g" would contain the value of
  260. `val` to 3 digits. See the __format documentation for more details.
  261. [heading:indeterminate_result_error Indeterminate Result Errors]
  262. When the result of a special function is indeterminate for the value that was
  263. passed to it, then the function returns the result of:
  264. boost::math::policies::raise_overflow_error<T>(FunctionName, Message, Val, Default, __Policy);
  265. Where
  266. `T` is the floating-point type passed to the function, `FunctionName` is the
  267. name of the function, `Message` is an error message describing the problem,
  268. Val is the value for which the result is indeterminate, Default is an
  269. alternative default result that must be returned for `ignore_error` and
  270. `errno_on_erro` policies, and __Policy is the current policy in use for the
  271. function that was called.
  272. The default policy for this function is `ignore_error`: note that this error
  273. type is reserved for situations where the result is mathematically
  274. undefined or indeterminate, but there is none the less a convention for what
  275. the result should be: for example the C99 standard specifies that the result
  276. of 0[super 0] is 1, even though the result is actually mathematically indeterminate.
  277. [heading:rounding_error Rounding Errors]
  278. When one of the rounding functions __round, __trunc or __modf is
  279. called with an argument that has no integer representation, or
  280. is too large to be represented in the result type then the
  281. value returned is the result of a call to:
  282. boost::math::policies::raise_rounding_error<T>(FunctionName, Message, Val, __Policy);
  283. Where
  284. `T` is the floating point type passed to the function, `FunctionName` is the
  285. name of the function, `Message` is an error message describing the problem,
  286. `Val` is the erroneous argument,
  287. and __Policy is the current policy in use for the called function.
  288. The default behaviour of this function is to throw a `boost::math::rounding_error`.
  289. Note that in order to support information rich error messages when throwing
  290. exceptions, `Message` must contain
  291. a __format recognised format specifier: the argument `val` is inserted into
  292. the error message according to the specifier used.
  293. For example if `Message` contains a "%1%" then it is replaced by the value of
  294. `val` to the full precision of T, where as "%.3g" would contain the value of
  295. `val` to 3 digits. See the __format documentation for more details.
  296. [heading:checked_narrowing_cast Errors from typecasts]
  297. Many special functions evaluate their results at a higher precision
  298. than their arguments in order to ensure full machine precision in
  299. the result: for example, a function passed a float argument may evaluate
  300. its result using double precision internally. Many of the errors listed
  301. above may therefore occur not during evaluation, but when converting
  302. the result to the narrower result type. The function:
  303. template <class T, class __Policy, class U>
  304. T checked_narrowing_cast(U const& val, const char* function);
  305. Is used to perform these conversions, and will call the error handlers
  306. listed above on [link math_toolkit.error_handling.overflow_error overflow],
  307. [link math_toolkit.error_handling.underflow_error underflow] or [link math_toolkit.error_handling.denorm_error denormalisation].
  308. [endsect] [/section:error_handling Error Handling]
  309. [/
  310. Copyright 2006 - 2012 John Maddock and Paul A. Bristow.
  311. Distributed under the Boost Software License, Version 1.0.
  312. (See accompanying file LICENSE_1_0.txt or copy at
  313. http://www.boost.org/LICENSE_1_0.txt).
  314. ]