3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Error Handling</title>
5 <link rel="stylesheet" href="../math.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../index.html" title="Math Toolkit 2.11.0">
8 <link rel="up" href="../overview.html" title="Chapter 1. Overview">
9 <link rel="prev" href="result_type.html" title="Calculation of the Type of the Result">
10 <link rel="next" href="compilers_overview.html" title="Compilers">
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="result_type.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="compilers_overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="math_toolkit.error_handling"></a><a class="link" href="error_handling.html" title="Error Handling">Error Handling</a>
28 </h2></div></div></div>
30 <a name="math_toolkit.error_handling.h0"></a>
31 <span class="phrase"><a name="math_toolkit.error_handling.quick_reference"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.quick_reference">Quick
35 Handling of errors by this library is split into two orthogonal parts:
37 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
39 What kind of error has been raised?
42 What should be done when the error is raised?
45 <div class="warning"><table border="0" summary="Warning">
47 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../doc/src/images/warning.png"></td>
48 <th align="left">Warning</th>
50 <tr><td align="left" valign="top"><p>
51 The default error actions are to throw an exception with an informative error
52 message. <span class="red">If you do not try to catch the exception, you
53 will not see the message!</span>
57 The kinds of errors that can be raised are:
59 <div class="variablelist">
60 <p class="title"><b></b></p>
61 <dl class="variablelist">
62 <dt><span class="term">Domain Error</span></dt>
64 Occurs when one or more arguments to a function are out of range.
66 <dt><span class="term">Pole Error</span></dt>
68 Occurs when the particular arguments cause the function to be evaluated
69 at a pole with no well defined residual value. For example if <a class="link" href="sf_gamma/tgamma.html" title="Gamma">tgamma</a>
70 is evaluated at exactly -2, the function approaches different limiting
71 values depending upon whether you approach from just above or just below
72 -2. Hence the function has no well defined value at this point and a
73 Pole Error will be raised.
75 <dt><span class="term">Overflow Error</span></dt>
77 Occurs when the result is either infinite, or too large to represent
78 in the numeric type being returned by the function.
80 <dt><span class="term">Underflow Error</span></dt>
82 Occurs when the result is not zero, but is too small to be represented
83 by any other value in the type being returned by the function.
85 <dt><span class="term">Denormalisation Error</span></dt>
87 Occurs when the returned result would be a denormalised value.
89 <dt><span class="term">Rounding Error</span></dt>
91 Occurs when the argument to one of the rounding functions <a class="link" href="rounding/trunc.html" title="Truncation Functions">trunc</a>,
92 <a class="link" href="rounding/round.html" title="Rounding Functions">round</a> and <a class="link" href="rounding/modf.html" title="Integer and Fractional Part Splitting (modf)">modf</a>
93 can not be represented as an integer type, is outside the range of the
96 <dt><span class="term">Evaluation Error</span></dt>
98 Occurs if no method of evaluation is known, or when an internal error
99 occurred that prevented the result from being evaluated: this should
100 never occur, but if it does, then it's likely to be due to an iterative
101 method not converging fast enough.
103 <dt><span class="term">Indeterminate Result Error</span></dt>
105 Occurs when the result of a function is not defined for the values that
111 The action undertaken by each error condition is determined by the current
112 <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> in effect. This can be changed program-wide
113 by setting some configuration macros, or at namespace scope, or at the call
114 site (by specifying a specific policy in the function call).
117 The available actions are:
119 <div class="variablelist">
120 <p class="title"><b></b></p>
121 <dl class="variablelist">
122 <dt><span class="term">throw_on_error</span></dt>
124 Throws the exception most appropriate to the error condition.
126 <dt><span class="term">errno_on_error</span></dt>
128 Sets ::errno to an appropriate value, and then returns the most appropriate
131 <dt><span class="term">ignore_error</span></dt>
133 Ignores the error and simply the returns the most appropriate result.
135 <dt><span class="term">user_error</span></dt>
137 Calls a <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">user-supplied
143 The following tables show all the permutations of errors and actions, with
144 the <span class="bold"><strong>default action for each error shown in bold</strong></span>:
147 <a name="math_toolkit.error_handling.possible_actions_for_domain_erro"></a><p class="title"><b>Table 1.1. Possible Actions for Domain Errors</b></p>
148 <div class="table-contents"><table class="table" summary="Possible Actions for Domain Errors">
174 <span class="bold"><strong>Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">domain_error</span></code></strong></span>
186 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
187 to <code class="computeroutput"><span class="identifier">EDOM</span></code> and returns
188 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()</span></code>
200 Returns <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()</span></code>
212 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_domain_error</span></code>:
213 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
214 must be defined by the user</a>.
221 <br class="table-break"><div class="table">
222 <a name="math_toolkit.error_handling.possible_actions_for_pole_errors"></a><p class="title"><b>Table 1.2. Possible Actions for Pole Errors</b></p>
223 <div class="table-contents"><table class="table" summary="Possible Actions for Pole Errors">
249 <span class="bold"><strong>Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">domain_error</span></code></strong></span>
261 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
262 to <code class="computeroutput"><span class="identifier">EDOM</span></code> and returns
263 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()</span></code>
275 Returns <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()</span></code>
287 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_pole_error</span></code>:
288 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
289 must be defined by the user</a>.
296 <br class="table-break"><div class="table">
297 <a name="math_toolkit.error_handling.possible_actions_for_overflow_er"></a><p class="title"><b>Table 1.3. Possible Actions for Overflow Errors</b></p>
298 <div class="table-contents"><table class="table" summary="Possible Actions for Overflow Errors">
324 <span class="bold"><strong>Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">overflow_error</span></code></strong></span>
336 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
337 to <code class="computeroutput"><span class="identifier">ERANGE</span></code> and returns
338 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">infinity</span><span class="special">()</span></code>
350 Returns <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">infinity</span><span class="special">()</span></code>
362 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_overflow_error</span></code>:
363 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
364 must be defined by the user</a>.
371 <br class="table-break"><div class="table">
372 <a name="math_toolkit.error_handling.possible_actions_for_underflow_e"></a><p class="title"><b>Table 1.4. Possible Actions for Underflow Errors</b></p>
373 <div class="table-contents"><table class="table" summary="Possible Actions for Underflow Errors">
399 Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underflow_error</span></code>
411 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
412 to <code class="computeroutput"><span class="identifier">ERANGE</span></code> and returns
425 <span class="bold"><strong>Returns 0</strong></span>
437 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_underflow_error</span></code>:
438 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
439 must be defined by the user</a>.
446 <br class="table-break"><div class="table">
447 <a name="math_toolkit.error_handling.possible_actions_for_denorm_erro"></a><p class="title"><b>Table 1.5. Possible Actions for Denorm Errors</b></p>
448 <div class="table-contents"><table class="table" summary="Possible Actions for Denorm Errors">
474 Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underflow_error</span></code>
486 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
487 to <code class="computeroutput"><span class="identifier">ERANGE</span></code> and returns
488 the denormalised value.
500 <span class="bold"><strong>Returns the denormalised value.</strong></span>
512 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_denorm_error</span></code>:
513 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
514 must be defined by the user</a>.
521 <br class="table-break"><div class="table">
522 <a name="math_toolkit.error_handling.possible_actions_for_rounding_er"></a><p class="title"><b>Table 1.6. Possible Actions for Rounding Errors</b></p>
523 <div class="table-contents"><table class="table" summary="Possible Actions for Rounding Errors">
549 Throws <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">rounding_error</span></code>
561 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
562 to <code class="computeroutput"><span class="identifier">ERANGE</span></code> and returns
563 the largest representable value of the target integer type (or the
564 most negative value if the argument to the function was less than
577 <span class="bold"><strong>Returns the largest representable value of
578 the target integer type (or the most negative value if the argument
579 to the function was less than zero).</strong></span>
591 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_rounding_error</span></code>:
592 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
593 must be defined by the user</a>.
600 <br class="table-break"><div class="table">
601 <a name="math_toolkit.error_handling.possible_actions_for_internal_ev"></a><p class="title"><b>Table 1.7. Possible Actions for Internal Evaluation Errors</b></p>
602 <div class="table-contents"><table class="table" summary="Possible Actions for Internal Evaluation Errors">
628 <span class="bold"><strong>Throws <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">evaluation_error</span></code></strong></span>
640 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
641 to <code class="computeroutput"><span class="identifier">EDOM</span></code> and returns
642 the closest approximation found.
654 Returns the closest approximation found.
666 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_evaluation_error</span></code>:
667 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
668 must be defined by the user</a>.
675 <br class="table-break"><div class="table">
676 <a name="math_toolkit.error_handling.possible_actions_for_indetermina"></a><p class="title"><b>Table 1.8. Possible Actions for Indeterminate Result Errors</b></p>
677 <div class="table-contents"><table class="table" summary="Possible Actions for Indeterminate Result Errors">
703 Throws <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">domain_error</span></code>
715 Sets <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>
716 to <code class="computeroutput"><span class="identifier">EDOM</span></code> and returns
717 the same value as <code class="computeroutput"><span class="identifier">ignore_error</span></code>.
729 <span class="bold"><strong>Returns a default result that depends on the
730 function where the error occurred.</strong></span>
742 Returns the result of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">user_indeterminate_result_error</span></code>:
743 <a class="link" href="pol_tutorial/user_def_err_pol.html" title="Calling User Defined Error Handlers">this function
744 must be defined by the user</a>.
751 <br class="table-break"><p>
752 All these error conditions are in namespace boost::math::policies, made available,
753 for example, a by namespace declaration using <code class="computeroutput"><span class="keyword">namespace</span>
754 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span></code> or individual using declarations <code class="computeroutput"><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">policies</span><span class="special">::</span><span class="identifier">overflow_error</span><span class="special">;</span></code>.
757 <a name="math_toolkit.error_handling.h1"></a>
758 <span class="phrase"><a name="math_toolkit.error_handling.rationale"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.rationale">Rationale</a>
761 The flexibility of the current implementation should be reasonably obvious:
762 the default behaviours were chosen based on feedback during the formal review
763 of this library. It was felt that:
765 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
766 <li class="listitem">
767 Genuine errors should be flagged with exceptions rather than following
768 C-compatible behaviour and setting <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>.
770 <li class="listitem">
771 Numeric underflow and denormalised results were not considered to be fatal
772 errors in most cases, so it was felt that these should be ignored.
774 <li class="listitem">
775 If there is more than one error, only the first detected will be reported
776 in the throw message.
780 <a name="math_toolkit.error_handling.h2"></a>
781 <span class="phrase"><a name="math_toolkit.error_handling.finding_more_information"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.finding_more_information">Finding
785 There are some pre-processor macro defines that can be used to <a class="link" href="pol_ref/policy_defaults.html" title="Using Macros to Change the Policy Defaults">change
786 the policy defaults</a>. See also the <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">policy section</a>.
789 An example is at the Policy tutorial in <a class="link" href="pol_tutorial/changing_policy_defaults.html" title="Changing the Policy Defaults">Changing
790 the Policy Defaults</a>.
793 Full source code of this typical example of passing a 'bad' argument (negative
794 degrees of freedom) to Student's t distribution is <a class="link" href="stat_tut/weg/error_eg.html" title="Error Handling Example">in
795 the error handling example</a>.
798 The various kind of errors are described in more detail below.
801 <a name="math_toolkit.error_handling.h3"></a>
802 <span class="phrase"><a name="math_toolkit.error_handling.domain_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.domain_error">Domain
806 When a special function is passed an argument that is outside the range of
807 values for which that function is defined, then the function returns the result
810 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_domain_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <span class="identifier">Val</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
813 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating-point
814 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
815 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
816 is an error message describing the problem, Val is the value that was out of
817 range, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> is the current policy in use
818 for the function that was called.
821 The default policy behaviour of this function is to throw a std::domain_error
822 C++ exception. But if the <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> is to ignore
823 the error, or set global <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code>,
824 then a NaN will be returned.
827 This behaviour is chosen to assist compatibility with the behaviour of <span class="emphasis"><em>ISO/IEC
828 9899:1999 Programming languages - C</em></span> and with the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf" target="_top">Draft
829 Technical Report on C++ Library Extensions, 2005-06-24, section 5.2.1, paragraph
832 <div class="blockquote"><blockquote class="blockquote"><p>
833 <span class="emphasis"><em>"Each of the functions declared above shall return a NaN (Not
834 a Number) if any argument value is a NaN, but it shall not report a domain
835 error. Otherwise, each of the functions declared above shall report a domain
836 error for just those argument values for which:<br> the function description's
837 Returns clause explicitly specifies a domain, and those arguments fall outside
838 the specified domain; or <br> the corresponding mathematical function value
839 has a non-zero imaginary component; or <br> the corresponding mathematical
840 function is not mathematically defined. <br> Note 2: A mathematical function
841 is mathematically defined for a given set of argument values if it is explicitly
842 defined for that set of argument values or if its limiting value exists and
843 does not depend on the direction of approach."</em></span>
844 </p></blockquote></div>
846 Note that in order to support information-rich error messages when throwing
847 exceptions, <code class="computeroutput"><span class="identifier">Message</span></code> must contain
848 a <a href="../../../../format/index.html" target="_top">Boost.Format</a> recognised format
849 specifier: the argument <code class="computeroutput"><span class="identifier">Val</span></code>
850 is inserted into the error message according to the specifier used.
853 For example if <code class="computeroutput"><span class="identifier">Message</span></code> contains
854 a "%1%" then it is replaced by the value of <code class="computeroutput"><span class="identifier">Val</span></code>
855 to the full precision of T, where as "%.3g" would contain the value
856 of <code class="computeroutput"><span class="identifier">Val</span></code> to 3 digits. See the
857 <a href="../../../../format/index.html" target="_top">Boost.Format</a> documentation
861 <a name="math_toolkit.error_handling.h4"></a>
862 <span class="phrase"><a name="math_toolkit.error_handling.pole_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.pole_error">Evaluation
866 When a special function is passed an argument that is at a pole without a well
867 defined residual value, then the function returns the result of:
869 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_pole_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <span class="identifier">Val</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
872 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating point
873 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
874 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
875 is an error message describing the problem, <code class="computeroutput"><span class="identifier">Val</span></code>
876 is the value of the argument that is at a pole, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a>
877 is the current policy in use for the function that was called.
880 The default behaviour of this function is to throw a std::domain_error exception.
881 But <a class="link" href="pol_ref/error_handling_policies.html" title="Error Handling Policies">error handling
882 policies</a> can be used to change this, for example to <code class="computeroutput"><span class="identifier">ignore_error</span></code>
886 Note that in order to support information-rich error messages when throwing
887 exceptions, <code class="computeroutput"><span class="identifier">Message</span></code> must contain
888 a <a href="../../../../format/index.html" target="_top">Boost.Format</a> recognised format
889 specifier: the argument <code class="computeroutput"><span class="identifier">val</span></code>
890 is inserted into the error message according to the specifier used.
893 For example if <code class="computeroutput"><span class="identifier">Message</span></code> contains
894 a "%1%" then it is replaced by the value of <code class="computeroutput"><span class="identifier">val</span></code>
895 to the full precision of T, where as "%.3g" would contain the value
896 of <code class="computeroutput"><span class="identifier">val</span></code> to 3 digits. See the
897 <a href="../../../../format/index.html" target="_top">Boost.Format</a> documentation
901 <a name="math_toolkit.error_handling.h5"></a>
902 <span class="phrase"><a name="math_toolkit.error_handling.overflow_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.overflow_error">Numeric
906 When the result of a special function is too large to fit in the argument floating-point
907 type, then the function returns the result of:
909 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_overflow_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
912 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating-point
913 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
914 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
915 is an error message describing the problem, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a>
916 is the current policy in use for the function that was called.
919 The default policy for this function is that <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">overflow_error</span></code>
920 C++ exception is thrown. But if, for example, an <code class="computeroutput"><span class="identifier">ignore_error</span></code>
921 policy is used, then returns <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">infinity</span><span class="special">()</span></code>.
922 In this situation if the type <code class="computeroutput"><span class="identifier">T</span></code>
923 doesn't support infinities, the maximum value for the type is returned.
926 <a name="math_toolkit.error_handling.h6"></a>
927 <span class="phrase"><a name="math_toolkit.error_handling.underflow_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.underflow_error">Numeric
931 If the result of a special function is known to be non-zero, but the calculated
932 result underflows to zero, then the function returns the result of:
934 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_underflow_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
937 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating point
938 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
939 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
940 is an error message describing the problem, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a>
941 is the current policy in use for the called function.
944 The default version of this function returns zero. But with another policy,
945 like <code class="computeroutput"><span class="identifier">throw_on_error</span></code>, throws
946 an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underflow_error</span></code> C++ exception.
949 <a name="math_toolkit.error_handling.h7"></a>
950 <span class="phrase"><a name="math_toolkit.error_handling.denorm_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.denorm_error">Denormalisation
954 If the result of a special function is a denormalised value <span class="emphasis"><em>z</em></span>
955 then the function returns the result of:
957 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_denorm_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">z</span><span class="special">,</span> <span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
960 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating point
961 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
962 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
963 is an error message describing the problem, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a>
964 is the current policy in use for the called function.
967 The default version of this function returns <span class="emphasis"><em>z</em></span>. But with
968 another policy, like <code class="computeroutput"><span class="identifier">throw_on_error</span></code>
969 throws an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underflow_error</span></code> C++ exception.
972 <a name="math_toolkit.error_handling.h8"></a>
973 <span class="phrase"><a name="math_toolkit.error_handling.evaluation_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.evaluation_error">Evaluation
977 When a special function calculates a result that is known to be erroneous,
978 or where the result is incalculable then it calls:
980 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_evaluation_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <span class="identifier">Val</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
983 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating point
984 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
985 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
986 is an error message describing the problem, <code class="computeroutput"><span class="identifier">Val</span></code>
987 is the erroneous value, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> is the current
988 policy in use for the called function.
991 The default behaviour of this function is to throw a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">evaluation_error</span></code>.
994 Note that in order to support information rich error messages when throwing
995 exceptions, <code class="computeroutput"><span class="identifier">Message</span></code> must contain
996 a <a href="../../../../format/index.html" target="_top">Boost.Format</a> recognised format
997 specifier: the argument <code class="computeroutput"><span class="identifier">val</span></code>
998 is inserted into the error message according to the specifier used.
1001 For example if <code class="computeroutput"><span class="identifier">Message</span></code> contains
1002 a "%1%" then it is replaced by the value of <code class="computeroutput"><span class="identifier">val</span></code>
1003 to the full precision of T, where as "%.3g" would contain the value
1004 of <code class="computeroutput"><span class="identifier">val</span></code> to 3 digits. See the
1005 <a href="../../../../format/index.html" target="_top">Boost.Format</a> documentation
1009 <a name="math_toolkit.error_handling.h9"></a>
1010 <span class="phrase"><a name="math_toolkit.error_handling.indeterminate_result_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.indeterminate_result_error">Indeterminate
1014 When the result of a special function is indeterminate for the value that was
1015 passed to it, then the function returns the result of:
1017 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_overflow_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <span class="identifier">Val</span><span class="special">,</span> <span class="identifier">Default</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
1020 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating-point
1021 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
1022 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
1023 is an error message describing the problem, Val is the value for which the
1024 result is indeterminate, Default is an alternative default result that must
1025 be returned for <code class="computeroutput"><span class="identifier">ignore_error</span></code>
1026 and <code class="computeroutput"><span class="identifier">errno_on_erro</span></code> policies,
1027 and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> is the current policy in use for the
1028 function that was called.
1031 The default policy for this function is <code class="computeroutput"><span class="identifier">ignore_error</span></code>:
1032 note that this error type is reserved for situations where the result is mathematically
1033 undefined or indeterminate, but there is none the less a convention for what
1034 the result should be: for example the C99 standard specifies that the result
1035 of 0<sup>0</sup> is 1, even though the result is actually mathematically indeterminate.
1038 <a name="math_toolkit.error_handling.h10"></a>
1039 <span class="phrase"><a name="math_toolkit.error_handling.rounding_error"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.rounding_error">Rounding
1043 When one of the rounding functions <a class="link" href="rounding/round.html" title="Rounding Functions">round</a>,
1044 <a class="link" href="rounding/trunc.html" title="Truncation Functions">trunc</a> or <a class="link" href="rounding/modf.html" title="Integer and Fractional Part Splitting (modf)">modf</a>
1045 is called with an argument that has no integer representation, or is too large
1046 to be represented in the result type then the value returned is the result
1049 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">raise_rounding_error</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">FunctionName</span><span class="special">,</span> <span class="identifier">Message</span><span class="special">,</span> <span class="identifier">Val</span><span class="special">,</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">);</span>
1052 Where <code class="computeroutput"><span class="identifier">T</span></code> is the floating point
1053 type passed to the function, <code class="computeroutput"><span class="identifier">FunctionName</span></code>
1054 is the name of the function, <code class="computeroutput"><span class="identifier">Message</span></code>
1055 is an error message describing the problem, <code class="computeroutput"><span class="identifier">Val</span></code>
1056 is the erroneous argument, and <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> is the
1057 current policy in use for the called function.
1060 The default behaviour of this function is to throw a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">rounding_error</span></code>.
1063 Note that in order to support information rich error messages when throwing
1064 exceptions, <code class="computeroutput"><span class="identifier">Message</span></code> must contain
1065 a <a href="../../../../format/index.html" target="_top">Boost.Format</a> recognised format
1066 specifier: the argument <code class="computeroutput"><span class="identifier">val</span></code>
1067 is inserted into the error message according to the specifier used.
1070 For example if <code class="computeroutput"><span class="identifier">Message</span></code> contains
1071 a "%1%" then it is replaced by the value of <code class="computeroutput"><span class="identifier">val</span></code>
1072 to the full precision of T, where as "%.3g" would contain the value
1073 of <code class="computeroutput"><span class="identifier">val</span></code> to 3 digits. See the
1074 <a href="../../../../format/index.html" target="_top">Boost.Format</a> documentation
1078 <a name="math_toolkit.error_handling.h11"></a>
1079 <span class="phrase"><a name="math_toolkit.error_handling.checked_narrowing_cast"></a></span><a class="link" href="error_handling.html#math_toolkit.error_handling.checked_narrowing_cast">Errors
1083 Many special functions evaluate their results at a higher precision than their
1084 arguments in order to ensure full machine precision in the result: for example,
1085 a function passed a float argument may evaluate its result using double precision
1086 internally. Many of the errors listed above may therefore occur not during
1087 evaluation, but when converting the result to the narrower result type. The
1090 <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span><span class="special">></span>
1091 <span class="identifier">T</span> <span class="identifier">checked_narrowing_cast</span><span class="special">(</span><span class="identifier">U</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">val</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">function</span><span class="special">);</span>
1094 Is used to perform these conversions, and will call the error handlers listed
1095 above on <a class="link" href="error_handling.html#math_toolkit.error_handling.overflow_error">overflow</a>,
1096 <a class="link" href="error_handling.html#math_toolkit.error_handling.underflow_error">underflow</a>
1097 or <a class="link" href="error_handling.html#math_toolkit.error_handling.denorm_error">denormalisation</a>.
1100 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
1101 <td align="left"></td>
1102 <td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
1103 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
1104 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
1105 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
1106 Daryle Walker and Xiaogang Zhang<p>
1107 Distributed under the Boost Software License, Version 1.0. (See accompanying
1108 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>)
1113 <div class="spirit-nav">
1114 <a accesskey="p" href="result_type.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="compilers_overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>