The feclearexcept in C++
In this article, we will discuss the feclearexcept in C++ with its syntax, parameters, and example.
What is feclearexcept?
The feclearexcept function is a low-level function declared in the
Syntax and Parameters:
It has the following syntax:
#includeint feclearexcept(int excepts);
#include
int feclearexcept(int excepts);:
- int excepts: This integer argument represents a bitmask created by bitwise OR-ing the appropriate values from the following macros defined in fenv.h.
- FE_DIVBYZERO: Division by zero
- FE_INEXACT: Inexact result (result cannot be represented exactly)
- FE_INVALID: Invalid operation (e.g., square root of a negative number)
- FE_OVERFLOW: Overflow (result too large)
- FE_UNDERFLOW: Underflow (result too small)
- FE_ALL_EXCEPT: Clear all supported exceptions
Return value:
- 0 indicates successful clearing of all specified exceptions (or if the excepts is zero).
- A non-zero value signifies an error or that not all specified exceptions were cleared.
Purpose:
The feclearexcept function clarifies the floating-point exception flags specified in the excepts argument. These flags get set when a floating-point exception occurs during a previous operation. Clearing these flags allows your program to move on to processing subsequent floating-point calculations without being influenced by the previous exception.
Key Points to Remember:
- It is a low-level function and should be cautiously employed due to its potential to affect the global floating-point environment.
- The success of clearing exceptions depends on factors like hardware and compiler support. Always check the return value to confirm complete clearing.
- It doesn't clear the actual error condition; it only clears the flags indicating its occurrence.
- In C++, floating-point exceptions are distinct from C++ exceptions and cannot be caught using try-catch blocks.
Example:
Let us take an example to illustrate the feclearexcept in C++.
#include#include #include int main() { // Example exception: division by zero float result = 1.0f / 0.0f; // This triggers a division by zero exception // Check if the exception flag is set if (fetestexcept(FE_DIVBYZERO)) { std::cerr << "Division by zero occurred!" << std::endl; // Try to clear the exception flag int clear_result = feclearexcept(FE_DIVBYZERO); if (clear_result == 0) { std::cout << "Exception flag cleared successfully." << std::endl; } else { std::cerr << "Failed to clear exception flag." << std::endl; } } else { std::cout << "No exception occurred." << std::endl; } return EXIT_SUCCESS; }
Output:
![The feclearexcept in C++/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Explanation:</strong></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>In this example, <strong><em>fetestexcept</em></strong> checks if the division by zero exception flag is set. If it is, <strong><em>feclearexcept</em></strong> tries to clear the flag. The final output will show whether the clearing was successful.</p>
<!-- /wp:paragraph -->
<!-- wp:html -->
<div class=](https://static.tutorialandexample.com/cpp/the-feclearexcept-in-cpp1.png)