FE_DFL_ENV

Defined in header <cfenv>
1
#define FE_DFL_ENV  /*implementation defined*/
(since C++11)

The macro constant FE_DFL_ENV expands to an expression of type const std::fenv_t*, which points to a full copy of the default floating-point environment, that is, the environment as loaded at program startup.

Additional macros that begin with FE_ followed by uppercase letters, and have the type const std::fenv_t*, may be supported by an implementation.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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();
}

Output:

1
2
3
4
5
6
7
8
9
10
On startup:
rounding to nearest
  
Before restoration:
underflow is raised
overflow is raised
rounding up
  
After reset to default:
rounding to nearest

See also

saves or restores the current floating point environment
(function)
(C++11)
restores the floating-point environment and raises the previously raise exceptions
(function)
doc_CPP
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.