
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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!