CppDS.com

C++ 98 11 14 17 20 手册

FE_DFL_ENV

来自cppreference.com
< cpp‎ | numeric‎ | fenv
定义于头文件 <cfenv>
#define FE_DFL_ENV  /*implementation defined*/
(C++11 起)

宏常量 FE_DFL_ENV 展开成 const std::fenv_t* 类型表达式,它指向默认浮点环境,即在程序加载时的浮点环境的完整副本。

实现亦可支持附加的以 FE_ 后随大写字母,并拥有 const std::fenv_t* 类型的宏。

示例

#include <iostream>
#include <cfenv>
 
#pragma STDC FENV_ACCESS ON
 
void show_env()
{
    int e = std::fetestexcept(FE_ALL_EXCEPT);
    if(e & FE_DIVBYZERO) std::cout << "division by zero is raised\n";
    if(e & FE_INEXACT)   std::cout << "inexact is raised\n";
    if(e & FE_INVALID)   std::cout << "invalid is raised\n";
    if(e & FE_UNDERFLOW) std::cout << "underflow is raised\n";
    if(e & FE_OVERFLOW)  std::cout << "overflow is raised\n";
    int r = std::fegetround();
    switch(r)
    {  
        case FE_DOWNWARD: std::cout << "rounding down\n"; break;
        case FE_TONEAREST: std::cout << "rounding to nearest\n"; break;
        case FE_TOWARDZERO: std::cout << "rounding to zero\n"; break;
        case FE_UPWARD: std::cout << "rounding up\n"; break;
    }
}
 
int main()
{
    std::cout << "On startup: \n";
    show_env();
 
    std::feraiseexcept(FE_UNDERFLOW | FE_OVERFLOW);
    std::fesetround(FE_UPWARD);
 
    std::cout << "\nBefore restoration: \n";
    show_env();
 
    std::fesetenv(FE_DFL_ENV);
 
    std::cout << "\nAfter reset to default: \n";
    show_env();
}

输出:

On startup: 
rounding to nearest
 
Before restoration: 
underflow is raised
overflow is raised
rounding up
 
After reset to default: 
rounding to nearest

参阅

保存或恢复当前浮点环境
(函数)
恢复浮点环境并引发之前引发的异常
(函数)
关闭