📜  C / C++中的fegetenv()函数

📅  最后修改于: 2021-05-26 02:26:37             🧑  作者: Mango

在C / C的fegetenv()函数++在头文件cfenv.h并试图存储在对象中的浮点环境的当前状态指出通过envp指定。浮点环境是一组状态标志和控制模式,包括浮点异常和舍入方向模式。

句法:

int fegetenv( fenv_t* envp )

参数:该函数接受一个强制性参数envp ,该参数指定一个对象,该对象存储浮点环境的状态。

返回值:该函数返回两个值,如下所示:

  • 成功时,它返回零。
  • 失败时,它返回非零。

下面的程序说明了上述函数:
程序1:

// C++ program to illustrate
// fegetenv() function
  
#include 
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 
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的C基础课程》。