fetestexcept
来自cppreference.com
| 在标头 <fenv.h> 定义
|
||
| |
(C99 起) | |
确定当前设置了哪个指定的浮点数异常子集。参数 excepts 是浮点数异常宏的逐位或。
参数
| excepts | - | 列出待测试异常标志的位掩码 |
返回值
包含于 excepts 而且对应于当前设置的浮点数异常中的浮点数异常宏的逐位或。
示例
运行此代码
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <float.h>
#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
printf("current exceptions raised: ");
if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT");
if(fetestexcept(FE_INVALID)) printf(" FE_INVALID");
if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW");
if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
int main(void)
{
/* 显示默认浮点数标志集合。 */
show_fe_exceptions();
/* 进行一些引发浮点数异常的计算。 */
printf("1.0/0.0 = %f\n", 1.0/0.0); /* FE_DIVBYZERO */
printf("1.0/10.0 = %f\n", 1.0/10.0); /* FE_INEXACT */
printf("sqrt(-1) = %f\n", sqrt(-1)); /* FE_INVALID */
printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0); /* FE_INEXACT FE_OVERFLOW */
printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n",
nextafter(DBL_MIN/pow(2.0,52),0.0)); /* FE_INEXACT FE_UNDERFLOW */
show_fe_exceptions();
return 0;
}
输出:
current exceptions raised: none
1.0/0.0 = inf
1.0/10.0 = 0.100000
sqrt(-1) = -nan
DBL_MAX*2.0 = inf
nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0
current exceptions raised: FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW
引用
- C11 标准(ISO/IEC 9899:2011):
- 7.6.2.5 The fetestexcept function (第 211-212 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.6.2.5 The fetestexcept function (第 192-193 页)
参阅
(C99) |
清除指定的浮点数异常状态标志 (函数) |
fetestexcept 的 C++ 文档
| |