📜  C++ feupdateenv()

📅  最后修改于: 2020-09-25 07:18:21             🧑  作者: Mango

feupdateenv() 函数首先保存当前引发的浮点异常,然后从给定的fenv_t对象还原浮点环境,然后引发先前保存的异常。

feupdateenv() 函数在头文件中定义。

feupdateenv()原型

int feupdateenv( fenv_t* envp );

feupdateenv() 函数将类型为fenv_t的指针作为其参数,该指针保存先前通过使用feholdexcept或fegetenv设置的浮点环境,并将该浮点环境与当前环境一起还原。

feupdateenv()参数

feupdateenv()返回值

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

#include 
#include 
#include 
#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()
{
    fenv_t envp;

    /* raise certain exceptions */
    feraiseexcept(FE_INVALID|FE_DIVBYZERO);
    print_exceptions();
    
    /* saves and clears current exceptions */
    feupdateenv(&envp);
    print_exceptions();
    
    /* restores saved exceptions */
    feupdateenv(&envp);
    print_exceptions();
    
    return 0;
}

运行该程序时,输出为:

Raised exceptions: FE_DIVBYZERO FE_INVALID
Raised exceptions: None
Raised exceptions: FE_DIVBYZERO FE_INVALID