📜  C++ feraiseexcept()

📅  最后修改于: 2020-09-25 07:14:32             🧑  作者: Mango

C++中的feraiseexcept() 函数尝试引发该参数指定的所有浮点异常。

feraiseexcept() 函数在头文件中定义。

feraiseexcept()原型

int feraiseexcept(int excepts);

参数中列出了要引发的浮点异常。

另外,您应该启用FENV_ACCESS ,这将使您的程序可以访问浮点环境以测试引发的异常。

feraiseexcept()参数

Bitmask accepted macros
Macro Type Description
FE_DIVBYZERO Pole error Division by zero
FE_INEXACT Inexact Not exact results such as (1.0/3.0)
FE_INVALID Domain error At least one arguments used is a value for which the function is not defined
FE_OVERFLOW Overflow range error Result is too large in magnitude to be represented by the return type
FE_UNDERFLOW Underflow range error Result is too small in magnitude to be represented by the return type
FE_ALL_EXCEPT All exceptions All exceptions supported by the implementation

feraiseexcept()返回值

示例:feraiseexcept() 函数的工作方式

#include 
#include 
#pragma STDC FENV_ACCESS ON
using namespace std;

int main()
{
    int retVal;
    feclearexcept(FE_ALL_EXCEPT);

    retVal = feraiseexcept(FE_OVERFLOW | FE_INVALID);
    if (retVal == 0)
        cout << "Successfully raised FE_OVERFLOW and FE_INVALID" << endl;
    else
        cout << "Raising FE_OVERFLOW and FE_INVALID failed" << endl;

    return 0;
}

运行该程序时,输出为:

Successfully raised FE_OVERFLOW and FE_INVALID