首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

math_errhandling

Defined in header <cmath>

?

?

#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指示由浮点运算符和功能*

Constant

Explanation

MATH_ERREXCEPT

indicates that floating-point exceptions are used: at least FE_DIVBYZERO, FE_INVALID, and FE_OVERFLOW are defined in <cfenv>.

MATH_ERRNO

indicates that floating-point operations use the variable errno to report errors.

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

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

Condition

Explanation

errno

floating-point exception

Example

Domain error

the argument is outside the range in which the operation is mathematically defined (the description of each function lists the required domain errors)

EDOM

FE_INVALID

std::acos(2)

Pole error

the mathematical result of the function is exactly infinite or undefined

ERANGE

FE_DIVBYZERO

std::log(0.0), 1.0/0.0

Range error due to overflow

the mathematical result is finite, but becomes infinite after rounding, or becomes the largest representable finite value after rounding down

ERANGE

FE_OVERFLOW

std::pow(DBL_MAX,2)

Range error due to underflow

the result is non-zero, but becomes zero after rounding, or becomes subnormal with a loss of precision

ERANGE or unchanged (implementation-defined)

FE_UNDERFLOW or nothing (implementation-defined)

DBL_MIN/2

Inexact result

the result has to be rounded to fit in the destination type

unchanged

FE_INEXACT or nothing (unspecified)

std::sqrt(2), 1.0/10.0

注记

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

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

二次

代码语言:javascript
复制
#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";
}

二次

可能的产出:

二次

代码语言:javascript
复制
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)

errno

macro which expands to POSIX-compatible thread-local error number variable(macro variable)

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

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com