C++ Library - <cfenv>
The <cfenv> header in C++, provides various functions to deal the floating-point environment, along with floating-point rounding modes and handling exceptions.
The floating-point environment maintains a series of status flags and control modes that indicate if any specific floating-point errors have occurred. The Rounding mode refers to how floating-point numbers are rounded during arithmetic operations.
Including <cfenv> Header
To use the cfenv in C++, we need to include the cfenv header file as shown below.
#include <cfenv>
Functions of <cfenv> Header
Below is list of all functions from <cfenv> header.
Rounding Direction Functions
The <cfenv> header provides some rounding modes by using those we can control the rounding direction for floating-point operations.
| S.NO | Functions & Description |
|---|---|
| 1 |
This function returns a value that indicates the rounding direction mode in the current floating point environment. |
| 2 | fesetround(int rdir)
Sets rdir as the current rounding direction mode for the floating point environment. |
Getting the Current Rounding Mode
In the following example we are going to use fegetround() to get the current rounding direction mode of the floating-point environment.
#include <iostream>
#include <cfenv>
int main() {
std::fesetround(FE_DOWNWARD);
int round_mode = std::fegetround();
if (round_mode == FE_DOWNWARD)
std::cout << "Rounding mode: Downward" << std::endl;
else
std::cout << "Other rounding mode" << std::endl;
}
Output
If we run the above code it will generate the following output
Rounding mode: Downward
Floating-point exceptions
In C++ Floating-point exceptions when a floating-point operation leads to an error condition. such as division by zero or overflow. some common floating-point exceptions are as follows.
Floating-point exceptions
In C++, floating-point exceptions occur when an arithmetic operation results in an error condition, such as division by zero or overflow. Some common floating-point exception functions are as follows:
| S.NO | Functions & Description |
|---|---|
| 1 |
feclearexcept()
Clears the specified floating-point exceptions. |
| 2 |
feraiseexcept()
Raises the specified floating-point exceptions. |
| 3 |
fegetexceptflag()
Stores the current state of specified floating-point exceptions in the given object. |
| 4 |
fesetexceptflag()
Sets the floating-point exceptions as indicated by the state stored in the object. |
Clearing a Floating-point Exception
In the following example we are going to use feclearexcept() to clear specific floating-point exception.
#include <iostream>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
int main() {
std::feclearexcept(FE_DIVBYZERO);
volatile double result = 1.0 / 0.0;
if (std::fetestexcept(FE_DIVBYZERO))
std::cout << "Division by zero exception detected" << std::endl;
else
std::cout << "No exception" << std::endl;
}
Output
If we run the above code it will generate the following output
Division by zero exception detected
Manipulating Entire Floating Point Environment
C++ provides functions to save, restore, or modify the entire environment at once. The floating-point environment includes both the current rounding mode and exception flags.
| S.NO | Functions & Description |
|---|---|
| 1 | fegetenv()
This function attempts to store the current state of the floating-point environment in the object. |
| 2 | fesetenv()
This function attempts to establish the state of the floating-point environment as represented by the object. |
| 3 | feholdexcept()
This function Saves the current state of the floating-point environment in the object. |
| 4 | feholdexcept()
This function attempts to establish the state of the floating-point environment as represented by the object. |
| 5 | fetestexcept()
This function returns the exceptions currently set, among those specified by excepts. |
Saving and Restoring the Environment
In the following example we are going to use feholdexcept() to save the current floating-point environment both rounding modes and exception flags and clears the exception flags.
#include <iostream>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
int main() {
fenv_t env;
std::feholdexcept(&env);
volatile double result = 1.0 / 0.0;
if (std::fetestexcept(FE_DIVBYZERO))
std::cout << "Division by zero detected!" << std::endl;
std::fesetenv(&env);
}
Output
If we run the above code it will generate the following output
Division by zero detected!