当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ fegetexceptflag()用法及代码示例

C++ 中的fegetexceptflag() 函数获取参数excepts 指定的浮点异常标志,并将其存储在flagp 指定的点中。

fetgetexceptflag() 函数在<cfenv> 头文件中定义。

fegetexceptflag()原型

int fegetexceptflag(fexcept_t* flagp, int excepts);

参数 excepts 可以是浮点异常宏的按位或。

参数:

  • flagp:指向将存储标志的 fexcept_t 对象的指针。
  • excepts:要获取的异常标志的位掩码列表。
位掩码接受的宏
类型 说明
FE_DIVBYZERO 极差 被零除
FE_INEXACT Inexact 不准确的结果,例如 (1.0/3.0)
FE_INVALID 域错误 使用的至少一个参数是未定义函数的值
FE_OVERFLOW 溢出范围错误 结果量级太大,无法用返回类型表示
FE_UNDERFLOW 下溢范围错误 结果量级太小,无法用返回类型表示
FE_ALL_EXCEPT 所有例外 实现支持的所有异常

返回:

  • fegetexceptflag() 函数成功返回零,否则返回非零。

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

#include <iostream>
#include <cfenv>
#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()
{
	fexcept_t excepts;
	feraiseexcept(FE_DIVBYZERO);
	
	/* save current state*/
	fegetexceptflag(&excepts,FE_ALL_EXCEPT);
	print_exceptions();
	feraiseexcept(FE_INVALID|FE_OVERFLOW);
	print_exceptions();
	
	/* restoring previous exceptions */
	fesetexceptflag(&excepts,FE_ALL_EXCEPT);
	print_exceptions();
	
	return 0;
}

运行程序时,输出将是:

Raised exceptions: FE_DIVBYZERO
Raised exceptions: FE_DIVBYZERO FE_INVALID FE_OVERFLOW
Raised exceptions: FE_DIVBYZERO

相关用法


注:本文由纯净天空筛选整理自 C++ fegetexceptflag()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。