Files
hustoj/web/cppreference-zh/chmhelp/cpp-numeric-math-math_errhandling.html
2025-08-01 12:57:54 +08:00

183 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MATH_ERRNO, MATH_ERREXCEPT, math_errhandling</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_numeric_math_math_errhandling skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading">MATH_ERRNO, MATH_ERREXCEPT, math_errhandling</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-numeric.html">numeric</a>&lrm; | <a href="cpp-numeric-math.html">math</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dsc-header">
<td>
<div>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code><a href="cpp-header-cmath.html">&lt;cmath&gt;</a></code></div>
</td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="co2">#define MATH_ERRNO &nbsp; &nbsp; &nbsp; &nbsp;1</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="co2">#define MATH_ERREXCEPT &nbsp; &nbsp;2</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="co2">#define math_errhandling &nbsp;/*implementation defined*/</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#23439;&#24120;&#37327; <code>math_errhandling</code> &#23637;&#24320;&#25104; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">int</span></span></span> &#31867;&#22411;&#30340;&#34920;&#36798;&#24335;&#65292;&#35201;&#20040;&#31561;&#20110; <code>MATH_ERRNO</code> &#65292;&#35201;&#20040;&#31561;&#20110; <code>MATH_ERREXCEPT</code> &#65292;&#35201;&#20040;&#31561;&#20110;&#20854;&#36880;&#20301;&#25110;&#65288; <span class="t-c"><span class="mw-geshi cpp source-cpp">MATH_ERRNO <span class="sy3">|</span> MATH_ERREXCEPT</span></span> &#65289;&#12290;</p>
<p><code>math_errhandling</code> &#30340;&#20540;&#25351;&#31034;&#28014;&#28857;&#36816;&#31639;&#31526;&#21644;<a href="cpp-numeric-math.html">&#20989;&#25968;</a>&#25152;&#36827;&#34892;&#30340;&#38169;&#35823;&#22788;&#29702;&#65306;</p>
<table class="t-dsc-begin">
<tr class="t-dsc-hitem">
<td>&#24120;&#37327;</td>
<td>&#35299;&#37322;</td>
</tr>
<tr class="t-dsc">
<td><code>MATH_ERREXCEPT</code></td>
<td>&#25351;&#31034;&#20351;&#29992;&#28014;&#28857;&#24322;&#24120;&#65306; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">&lt;</span>cfenv<span class="sy1">&gt;</span></span></span> &#20013;&#33267;&#23569;&#23450;&#20041;&#20102; <span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_DIVBYZERO</a></span> &#12289; <span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_INVALID</a></span> &#21450; <span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_OVERFLOW</a></span> &#12290;</td>
</tr>
<tr class="t-dsc">
<td><code>MATH_ERRNO</code></td>
<td>&#25351;&#26126;&#28014;&#28857;&#36816;&#31639;&#20351;&#29992;&#21464;&#37327; <span class="t-lc"><a href="cpp-error-errno.html">errno</a></span> &#25253;&#21578;&#38169;&#35823;&#12290;</td>
</tr>
</table>
<p>&#33509;&#23454;&#29616;&#25903;&#25345; IEEE &#28014;&#28857;&#31639;&#26415;&#65288; IEC 60559 &#65289;&#65292;&#21017;&#35201;&#27714; <span class="t-c"><span class="mw-geshi cpp source-cpp">math_errhandling <span class="sy3">&amp;</span> MATH_ERREXCEPT</span></span> &#38750;&#38646;&#12290;</p>
<p>&#35782;&#21035;&#19979;&#21015;&#28014;&#28857;&#38169;&#35823;&#26465;&#20214;&#65306;</p>
<table class="wikitable">
<tr>
<th>&#26465;&#20214;</th>
<th>&#35299;&#37322;</th>
<th>errno</th>
<th>&#28014;&#28857;&#24322;&#24120;</th>
<th>&#31034;&#20363;</th>
</tr>
<tr>
<td>&#23450;&#20041;&#22495;&#38169;&#35823;</td>
<td>&#21442;&#25968;&#22312;&#35813;&#36816;&#31639;&#30340;&#25968;&#23398;&#19978;&#30340;&#23450;&#20041;&#22495;&#20043;&#22806;&#65288;<a href="cpp-numeric-math.html">&#27599;&#20010;&#20989;&#25968;</a>&#30340;&#25551;&#36848;&#21015;&#20986;&#20102;&#35201;&#27714;&#30340;&#23450;&#20041;&#22495;&#38169;&#35823;&#65289;</td>
<td><span class="t-lc"><a href="cpp-error-errno_macros.html">EDOM</a></span></td>
<td><span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_INVALID</a></span></td>
<td><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-numeric-math-acos.html"><span class="kw1337">std::<span class="me2">acos</span></span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span></span></span></td>
</tr>
<tr>
<td>&#26497;&#28857;&#38169;&#35823;</td>
<td>&#20989;&#25968;&#30340;&#25968;&#23398;&#32467;&#26524;&#24688;&#26159;&#26080;&#38480;&#22823;&#25110;&#26410;&#23450;&#20041;</td>
<td><span class="t-lc"><a href="cpp-error-errno_macros.html">ERANGE</a></span></td>
<td><span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_DIVBYZERO</a></span></td>
<td><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-numeric-math-log.html"><span class="kw1324">std::<span class="me2">log</span></span></a><span class="br0">(</span><span class="nu16">0.0</span><span class="br0">)</span></span></span> &#12289; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="nu16">1.0</span><span class="sy2">/</span><span class="nu16">0.0</span></span></span></td>
</tr>
<tr>
<td>&#19978;&#28322;&#25152;&#33268;&#30340;&#20540;&#22495;&#38169;&#35823;</td>
<td>&#25968;&#23398;&#32467;&#26524;&#26377;&#38480;&#65292;&#20294;&#33293;&#20837;&#21518;&#21464;&#20026;&#26080;&#38480;&#65292;&#25110;&#22312;&#21521;&#19979;&#33293;&#20837;&#21518;&#21464;&#25104;&#26368;&#22823;&#21487;&#34920;&#31034;&#26377;&#38480;&#20540;</td>
<td><span class="t-lc"><a href="cpp-error-errno_macros.html">ERANGE</a></span></td>
<td><span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_OVERFLOW</a></span></td>
<td><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-numeric-math-pow.html"><span class="kw1332">std::<span class="me2">pow</span></span></a><span class="br0">(</span><a href="cpp-types-climits.html"><span class="kw418">DBL_MAX</span></a>,<span class="nu0">2</span><span class="br0">)</span></span></span></td>
</tr>
<tr>
<td>&#19979;&#28322;&#25152;&#33268;&#30340;&#22495;&#38169;&#35823;</td>
<td>&#32467;&#26524;&#38750;&#38646;&#65292;&#20294;&#22240;&#20026;&#33293;&#20837;&#21464;&#20026;&#38646;&#65292;&#25110;&#21464;&#25104;&#38750;&#27491;&#35268;&#24182;&#26377;&#31934;&#24230;&#25439;&#22833;</td>
<td><span class="t-lc"><a href="cpp-error-errno_macros.html">ERANGE</a></span> &#25110;&#19981;&#25913;&#21464;&#65288;&#23454;&#29616;&#23450;&#20041;&#65289;</td>
<td><span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_UNDERFLOW</a></span> &#25110;&#26080;&#65288;&#23454;&#29616;&#23450;&#20041;&#65289;</td>
<td><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-types-climits.html"><span class="kw415">DBL_TRUE_MIN</span></a><span class="sy2">/</span><span class="nu0">2</span></span></span></td>
</tr>
<tr>
<td>&#32467;&#26524;&#19981;&#20934;&#30830;</td>
<td>&#32467;&#26524;&#24517;&#39035;&#34987;&#33293;&#20837;&#21040;&#30446;&#26631;&#31867;&#22411;</td>
<td>&#19981;&#25913;&#21464;</td>
<td><span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_INEXACT</a></span> &#25110;&#26080;&#65288;&#26410;&#25351;&#23450;&#65289;</td>
<td><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-numeric-math-sqrt.html"><span class="kw1329">std::<span class="me2">sqrt</span></span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span></span></span>&#12289; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="nu16">1.0</span><span class="sy2">/</span><span class="nu16">10.0</span></span></span></td>
</tr>
</table>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">&#27880;&#24847;</span></h3>
<p>&#36890;&#24120;&#65292; <span class="t-lc"><a href="cpp-numeric-fenv-FE_exceptions.html">FE_INEXACT</a></span> &#26159;&#21542;&#20026;&#25968;&#23398;&#24211;&#20989;&#25968;&#25152;&#24341;&#21457;&#26159;&#26410;&#25351;&#23450;&#30340;&#65292;&#20294;&#36825;&#21487;&#20197;&#26174;&#24335;&#25351;&#23450;&#20110;&#20989;&#25968;&#30340;&#25551;&#36848;&#65288;&#20363;&#22914; <span class="t-lc"><a href="cpp-numeric-math-rint.html">std::rint</a></span> vs <span class="t-lc"><a href="cpp-numeric-math-nearbyint.html">std::nearbyint</a></span> &#65289;&#12290;</p>
<p>C++11 &#21069;&#65292;&#28014;&#28857;&#24322;&#24120;&#26159;&#26410;&#25351;&#23450;&#30340;&#65292;&#35201;&#27714;&#23545;&#20110;&#20219;&#20309;&#23450;&#20041;&#22495;&#38169;&#35823;&#26377; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-error-errno_macros.html"><span class="kw810">EDOM</span></a></span></span> &#65292;&#35201;&#27714;&#23545;&#19978;&#28322;&#21644;&#23454;&#29616;&#23450;&#20041;&#30340;&#19979;&#28322;&#26377; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-error-errno_macros.html"><span class="kw862">ERANGE</span></a></span></span> &#12290;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<div class="t-example">
<div class="t-example-live-link"></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;cfenv&gt;</span>
<span class="co2">#include &lt;cmath&gt;</span>
<span class="co2">#include &lt;cerrno&gt;</span>
<span class="co2">#include &lt;cstring&gt;</span>
<span class="co2">#pragma STDC FENV_ACCESS ON</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-io-cout.html"><span class="kw1755">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;MATH_ERRNO is &quot;</span>
<span class="sy1">&lt;&lt;</span> <span class="br0">(</span>math_errhandling <span class="sy3">&amp;</span> MATH_ERRNO <span class="sy4">?</span> <span class="st0">&quot;set&quot;</span> <span class="sy4">:</span> <span class="st0">&quot;not set&quot;</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span>
<span class="sy1">&lt;&lt;</span> <span class="st0">&quot;MATH_ERREXCEPT is &quot;</span>
<span class="sy1">&lt;&lt;</span> <span class="br0">(</span>math_errhandling <span class="sy3">&amp;</span> MATH_ERREXCEPT <span class="sy4">?</span> <span class="st0">&quot;set&quot;</span> <span class="sy4">:</span> <span class="st0">&quot;not set&quot;</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<a href="cpp-numeric-fenv-feclearexcept.html"><span class="kw1447">std::<span class="me2">feclearexcept</span></span></a><span class="br0">(</span><a href="cpp-numeric-fenv-FE_exceptions.html"><span class="kw1458">FE_ALL_EXCEPT</span></a><span class="br0">)</span><span class="sy4">;</span>
<span class="kw2">errno</span> <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
<a href="cpp-io-cout.html"><span class="kw1755">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;log(0) = &quot;</span> <span class="sy1">&lt;&lt;</span> <a href="cpp-numeric-math-log.html"><span class="kw1324">std::<span class="me2">log</span></span></a><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<span class="kw1">if</span><span class="br0">(</span><span class="kw2">errno</span> <span class="sy1">==</span> <a href="cpp-error-errno_macros.html"><span class="kw862">ERANGE</span></a><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1755">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;errno = ERANGE (&quot;</span> <span class="sy1">&lt;&lt;</span> <a href="cpp-string-byte-strerror.html"><span class="kw1161">std::<span class="me2">strerror</span></span></a><span class="br0">(</span><span class="kw2">errno</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;)<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="kw1">if</span><span class="br0">(</span><a href="cpp-numeric-fenv-fetestexcept.html"><span class="kw1448">std::<span class="me2">fetestexcept</span></span></a><span class="br0">(</span><a href="cpp-numeric-fenv-FE_exceptions.html"><span class="kw1459">FE_DIVBYZERO</span></a><span class="br0">)</span><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1755">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;FE_DIVBYZERO (pole error) reported<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#21487;&#33021;&#30340;&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">MATH_ERRNO is set
MATH_ERREXCEPT is set
log(0) = -inf
errno = ERANGE (Numerical result out of range)
FE_DIVBYZERO (pole error) reported</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-numeric-fenv-FE_exceptions.html"><span class="t-lines"><span>FE_ALL_EXCEPT</span><span>FE_DIVBYZERO</span><span>FE_INEXACT</span><span>FE_INVALID</span><span>FE_OVERFLOW</span><span>FE_UNDERFLOW</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div>
</td>
<td>&#28014;&#28857;&#24322;&#24120;<br />
<span class="t-mark">(&#23439;&#24120;&#37327;)</span></td>
</tr>
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-error-errno.html"><span class="t-lines"><span>errno</span></span></a></div>
</div>
</td>
<td>&#23637;&#24320;&#25104; POSIX &#20860;&#23481;&#30340;&#32447;&#31243;&#23616;&#22495;&#38169;&#35823;&#21495;&#21464;&#37327;&#30340;&#23439;<br />
<span class="t-mark">(&#23439;&#21464;&#37327;)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>