在頭文件cfenv.h中指定了C /C++中的fegetenv()函數,並嘗試將浮點環境的當前狀態存儲在envp指向的對象中。浮點環境是一組狀態標誌和控製模式,包括浮點異常和舍入方向模式。
用法:
int fegetenv( fenv_t* envp )
參數:該函數接受一個強製性參數envp,該參數指定一個對象,該對象存儲浮點環境的狀態。
返回值:該函數返回兩個值,如下所示:
- 成功時,它返回零。
- 失敗時,它返回非零。
以下示例程序旨在說明上述函數:
程序1:
// C++ program to illustrate
// fegetenv() function
#include <bits/stdc++.h>
using namespace std;
// rounding direction mode
void rounding_mode()
{
cout << "Rounding mode is ->";
switch (fegetround()) {
case FE_TONEAREST:
// Round to nearest
cout << "FE_TONEAREST" << endl;
break;
case FE_DOWNWARD:
// Round downward
cout << "FE_DOWNWARD" << endl;
break;
case FE_UPWARD:
// Round upward
cout << "FE_UPWARD" << endl;
break;
case FE_TOWARDZERO:
// Round toward zero
cout << "FE_TOWARDZERO" << endl;
break;
default:
cout << "unknown" << endl;
};
}
int main(void)
{
fenv_t envp;
// initial environment
cout << "Initial environment:" << endl;
// print the exception raised initially
cout << "Exception raised -> \n";
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
cout << "FE_DIVBYZERO " << endl;
if (fetestexcept(FE_INEXACT))
cout << "FE_INEXACT " << endl;
if (fetestexcept(FE_INVALID))
cout << "FE_INVALID " << endl;
if (fetestexcept(FE_OVERFLOW))
cout << "FE_OVERFLOW " << endl;
if (fetestexcept(FE_UNDERFLOW))
cout << "FE_UNDERFLOW " << endl;
}
else
cout << "None" << endl;
// print the rounding direction mode
rounding_mode();
// Current environment
fegetenv(&envp);
feraiseexcept(FE_INVALID);
// Set rounding direction mode
fesetround(FE_DOWNWARD);
// after environment is change
cout << endl
<< "Final environment:" << endl;
// print the exception raised
cout << "Exception raised -> \n";
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
cout << "FE_DIVBYZERO " << endl;
if (fetestexcept(FE_INEXACT))
cout << "FE_INEXACT " << endl;
if (fetestexcept(FE_INVALID))
cout << "FE_INVALID " << endl;
if (fetestexcept(FE_OVERFLOW))
cout << "FE_OVERFLOW " << endl;
if (fetestexcept(FE_UNDERFLOW))
cout << "FE_UNDERFLOW " << endl;
}
else
cout << "None" << endl;
// print the rounding direction mode
rounding_mode();
return 0;
}
輸出:
Initial environment: Exception raised -> None Rounding mode is ->FE_TONEAREST Final environment: Exception raised -> FE_INVALID Rounding mode is ->FE_DOWNWARD
程序2:
// C++ program to illustrate
// fegetenv() function
#include <bits/stdc++.h>
using namespace std;
// rounding direction mode
void rounding_mode()
{
cout << "Rounding mode is ->";
switch (fegetround()) {
case FE_TONEAREST:
// Round to nearest
cout << "FE_TONEAREST" << endl;
break;
case FE_DOWNWARD:
// Round downward
cout << "FE_DOWNWARD" << endl;
break;
case FE_UPWARD:
// Round upward
cout << "FE_UPWARD" << endl;
break;
case FE_TOWARDZERO:
// Round toward zero
cout << "FE_TOWARDZERO" << endl;
break;
default:
cout << "unknown" << endl;
};
}
int main(void)
{
fenv_t envp;
// initial environment
cout << "Initial environment:" << endl;
// print the exception raised initially
cout << "Exception raised -> \n";
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
cout << "FE_DIVBYZERO " << endl;
if (fetestexcept(FE_INEXACT))
cout << "FE_INEXACT " << endl;
if (fetestexcept(FE_INVALID))
cout << "FE_INVALID " << endl;
if (fetestexcept(FE_OVERFLOW))
cout << "FE_OVERFLOW " << endl;
if (fetestexcept(FE_UNDERFLOW))
cout << "FE_UNDERFLOW " << endl;
}
else
cout << "None" << endl;
// print the rounding direction mode
rounding_mode();
// Current environment
fegetenv(&envp);
feraiseexcept(FE_ALL_EXCEPT);
// Set rounding direction mode
fesetround(FE_DOWNWARD);
// after environment is change
cout << endl
<< "Final environment:" << endl;
// print the exception raised
cout << "Exception raised -> \n";
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
cout << "FE_DIVBYZERO " << endl;
if (fetestexcept(FE_INEXACT))
cout << "FE_INEXACT " << endl;
if (fetestexcept(FE_INVALID))
cout << "FE_INVALID " << endl;
if (fetestexcept(FE_OVERFLOW))
cout << "FE_OVERFLOW " << endl;
if (fetestexcept(FE_UNDERFLOW))
cout << "FE_UNDERFLOW " << endl;
}
else
cout << "None" << endl;
// print the rounding direction mode
rounding_mode();
return 0;
}
輸出:
Initial environment: Exception raised -> None Rounding mode is ->FE_TONEAREST Final environment: Exception raised -> FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW Rounding mode is ->FE_DOWNWARD
相關用法
- C++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C語言 tolower()用法及代碼示例
- C++ wmemmove()用法及代碼示例
- C語言 strlwr()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ wcstok()用法及代碼示例
- C++ wcsstr()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 fegetenv() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。