std::fetestexcept

来自cppreference.com
< cpp‎ | numeric‎ | fenv

定义于头文件 <cfenv>
int fetestexcept( int excepts );
(C++11 起)
确定当前设置的指定的子集的浮点异常。参数excepts是一个位的浮点例外宏.
原文:
Determines which of the specified subset of the floating point exceptions are currently set. The argument excepts is a bitwise OR of the 浮点例外宏.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

目录

[编辑] 参数

excepts -
位掩码列出的异常标志进行测试
原文:
bitmask listing the exception flags to test
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 返回值

位或浮点异常的宏都包含在excepts,对应当前设置的浮点异常.
原文:
Bitwise OR of the floating-point exception macros that are both included in excepts and correspond to floating-point exceptions currently set.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 示例

[编辑]
#include <iostream>
#include <cfenv>
#include <cmath>
 
#pragma STDC FENV_ACCESS ON
 
volatile double zero = 0.0; // volatile not needed where FENV_ACCESS is supported
volatile double one = 1.0;  // volatile not needed where FENV_ACCESS is supported
 
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

[编辑] 另请参阅

清除指定的浮点状态标志
原文:
clears the specified floating-point status flags
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

(函数) [编辑]