📜  C++ fetestexcept()

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

C++中的fetestexcept() 函数确定当前设置了浮点异常的指定子集中的哪个子集。

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

fetestexcept()原型

int fetestexcept( int excepts );

fetestexcept() 函数测试当前是否设置了excepts指定的浮点异常。参数excepts是浮点异常宏的按位或。

fetestexcept()参数

fetestexcept()返回值

示例:fetestexcept() 函数如何工作?

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

void print_exceptions()
{
    cout << "Raised exceptions: ";
    if(fetestexcept(FE_ALL_EXCEPT))
    {
        if(fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO ";
        if(fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT ";
        if(fetestexcept(FE_INVALID))
            cout << "FE_INVALID ";
        if(fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW ";
        if(fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW ";
    }
    else
        cout << "None";

    cout << endl;
}

int main(void)
{
    print_exceptions();
    
    feraiseexcept(FE_INVALID|FE_DIVBYZERO);
    print_exceptions();

    feclearexcept(FE_ALL_EXCEPT);
    print_exceptions();
    
    return 0;
}

运行该程序时,输出为:

Raised exceptions: None
Raised exceptions: FE_DIVBYZERO FE_INVALID
Raised exceptions: None