C++ Reference: Standard C++ Library reference: C Library: cfenv: fetestexcept

C++官方参考链接:https://cplusplus.com/reference/cfenv/fetestexcept/

函数 
<cfenv>
fetestexcept
int fetestexcept (int excepts);

测试浮点异常
返回当前设置的异常,包括由excepts指定的异常。
返回的值是当前在浮点环境中设置的excepts子集的按位或表示形式。如果当前没有设置excepts中的任何异常,则为0。 
调用此函数的程序应确保为调用启用pragma FENV_ACCESS

形参
excepts
位掩码值:实现支持的任意数量的浮点异常值的组合(带有按位或:|):

macro value(宏值)description(描述)
FE_DIVBYZEROPole error: division by zero, or some other asymptotically infinite result (from finite arguments).(极点错误:除以零,或其他渐进无限大的结果(来自有限的实参)。)
FE_INEXACTInexact: the result is not exact.(不精确:结果不精确。)
FE_INVALIDDomain error: At least one of the arguments is a value for which the function is not defined.(定义域错误:至少有一个实参是函数没有定义的值。)
FE_OVERFLOWOverflow range error: The result is too large in magnitude to be represented as a value of the return type.(上溢范围错误:结果的绝对大小太大,不能用返回类型的值表示。)
FE_UNDERFLOWUnderflow range error: The result is too small in magnitude to be represented as a value of the return type.(下溢范围错误:结果的绝对大小太小,不能用返回类型的值表示。)
FE_ALL_EXCEPTAll exceptions (selects all of the exceptions supported by the implementation).(所有异常(选择实现支持的所有异常)。)

某些库实现可能支持额外的浮点异常值(其对应的宏也以FE_开头)。
C99
库可以在<fenv.h>中只定义它们支持的以上宏值(其他的可能没有定义)。
C++ 11
至少上述所有宏值都在<cfenv>中定义(即使实现不支持)。

返回值 
如果excepts中的异常都没有设置,则为0。
否则,为当前设置的异常(expects中的异常)。 

用例
/* fetestexcept example */
#include <stdio.h>      /* puts */
#include <fenv.h>       /* feraiseexcept, fetestexcept, FE_* */
#pragma STDC FENV_ACCESS on

double fn (double x) {
  /* some function for which zero is a domain and range error */
  if (x==0.0) feraiseexcept(FE_INVALID|FE_OVERFLOW);
  return x;
}

int main ()
{
  int fe;

  feclearexcept (FE_ALL_EXCEPT);
  fn (0.0);

  /* testing for single exception: */
  if (fetestexcept(FE_OVERFLOW)) puts ("FE_OVERFLOW is set");

  /* testing multiple exceptions: */
  fe = fetestexcept (FE_ALL_EXCEPT);

  puts ("The following exceptions are set:");
  if (fe & FE_DIVBYZERO) puts ("FE_DIVBYZERO");
  if (fe & FE_INEXACT)   puts ("FE_INEXACT");
  if (fe & FE_INVALID)   puts ("FE_INVALID");
  if (fe & FE_OVERFLOW)  puts ("FE_OVERFLOW");
  if (fe & FE_UNDERFLOW) puts ("FE_UNDERFLOW");

  return 0;
}
可能的输出:

数据竞争
每个线程都维护一个具有自己状态的独立浮点环境。生成一个新线程会复制当前状态。【这适用于C11和C++11实现】 

异常
无抛出保证:此函数从不抛出异常。
注意,C浮点异常不是C++异常,因此不会被try/catch块捕获。
使用pragma FENV_ACCESS off调用此函数会导致未定义的行为。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值