當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。