std::fetestexcept

< cpp‎ | numeric‎ | fenv
定义于头文件 <cfenv>
int fetestexcept( int excepts );
(C++11 起)

确定当前设定了哪个浮点异常的指定子集。参数 excepts浮点异常宏的逐位或。

参数

excepts - 列出要测试的异常标志的位掩码

返回值

包含于 excepts 而且对应于当前设置的浮点异常中的浮点异常宏的逐位或。

示例

#include <iostream>
#include <cfenv>
#include <cmath>
 
#pragma STDC FENV_ACCESS ON
 
volatile double zero = 0.0; // 支持 FENV_ACCESS 处不需要 volatile
volatile double one = 1.0;  // 支持 FENV_ACCESS 处不需要 volatile
 
int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout <<  "1.0/0.0 = " << 1.0 / zero << '\n';
    if(std::fetestexcept(FE_DIVBYZERO)) {
        std::cout << "division by zero reported\n";
    } else {
        std::cout << "divsion by zero not reported\n";
    }
 
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "1.0/10 = " << one/10 << '\n';
    if(std::fetestexcept(FE_INEXACT)) {
        std::cout << "inexact result reported\n";
    } else {
        std::cout << "inexact result not reported\n";
    }
 
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "sqrt(-1) = " << std::sqrt(-1) << '\n';
    if(std::fetestexcept(FE_INVALID)) {
        std::cout << "invalid result reported\n";
    } else {
        std::cout << "invalid result not reported\n";
    }
}

输出:

1.0/0.0 = inf
division by zero reported
1.0/10 = 0.1
inexact result reported
sqrt(-1) = -nan
invalid result reported

参阅

清除指定的浮点状态标志
(函数)