在线文档教程
C++
数字 | Numerics

math_errhandling

数学[医]埃里诺,数学[医]埃雷克塞普,数学[医]错误处理

Defined in header
#define MATH_ERRNO 1(since C++11)
#define MATH_ERREXCEPT 2(since C++11)
#define math_errhandling /*implementation defined*/(since C++11)

宏常数math_errhandling展开为类型的表达式。int或者等于MATH_ERRNO,或等于MATH_ERREXCEPT,或等于按位或%28MATH_ERRNO | MATH_ERREXCEPT29%。

价值math_errhandling指示由浮点运算符和功能*

ConstantExplanation
MATH_ERREXCEPTindicates that floating-point exceptions are used: at least FE_DIVBYZERO, FE_INVALID, and FE_OVERFLOW are defined in <cfenv>.
MATH_ERRNOindicates that floating-point operations use the variable errno to report errors.

如果实现支持ieee浮点算法%28IEC 60559%29,math_errhandling & MATH_ERREXCEPT必须是非零的。

下列浮点错误条件已被识别:

ConditionExplanationerrnofloating-point exceptionExample
Domain errorthe argument is outside the range in which the operation is mathematically defined (the description of each function lists the required domain errors)EDOMFE_INVALIDstd::acos(2)
Pole errorthe mathematical result of the function is exactly infinite or undefinedERANGEFE_DIVBYZEROstd::log(0.0), 1.0/0.0
Range error due to overflowthe mathematical result is finite, but becomes infinite after rounding, or becomes the largest representable finite value after rounding downERANGEFE_OVERFLOWstd::pow(DBL_MAX,2)
Range error due to underflowthe result is non-zero, but becomes zero after rounding, or becomes subnormal with a loss of precisionERANGE or unchanged (implementation-defined)FE_UNDERFLOW or nothing (implementation-defined)DBL_MIN/2
Inexact resultthe result has to be rounded to fit in the destination typeunchangedFE_INEXACT or nothing (unspecified)std::sqrt(2), 1.0/10.0

注记

是否FE_INEXACT是由数学库函数引发的,一般未指定,但可以在函数%28的说明中显式指定。std::rintVSstd::nearbyint29%。

在C++11之前,没有指定浮点异常,EDOM任何域错误都是必需的,ERANGE为溢流和执行所需---对资金流动的定义。

二次

#include <iostream> #include <cfenv> #include <cmath> #include <cerrno> #include <cstring> #pragma STDC FENV_ACCESS ON int main() { std::cout << "MATH_ERRNO is " << (math_errhandling & MATH_ERRNO ? "set" : "not set") << '\n' << "MATH_ERREXCEPT is " << (math_errhandling & MATH_ERREXCEPT ? "set" : "not set") << '\n'; std::feclearexcept(FE_ALL_EXCEPT errno = 0; std::cout << "log(0) = " << std::log(0) << '\n'; if(errno == ERANGE) std::cout << "errno = ERANGE (" << std::strerror(errno) << ")\n"; if(std::fetestexcept(FE_DIVBYZERO)) std::cout << "FE_DIVBYZERO (pole error) reported\n"; }

二次

可能的产出:

二次

MATH_ERRNO is set MATH_ERREXCEPT is set log(0) = -inf errno = ERANGE (Numerical result out of range) FE_DIVBYZERO (pole error) reported

二次

另见

FE_ALL_EXCEPTFE_DIVBYZEROFE_INEXACTFE_INVALIDFE_OVERFLOWFE_UNDERFLOW (C++11)floating-point exceptions (macro constant)
errnomacro which expands to POSIX-compatible thread-local error number variable(macro variable)

C数学文档[医]错误处理

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/数值/数学/数学[医]错误处理