Fetestexept Funtion In C

It has the following syntax:

Example

int fetestexept(int exepts);

Parameters:

  • Exceptions: This bitmask indicates which floating-point exception flags to check. Specific constants have been designated for individual exceptions:

FE_DIVBYZERO: Division by zero

FE_INEXAT: Inexat result

FE_INVALID: Invalid operation

FE_OVERFLOW: Overflow

FE_UNDERFLOW: Underflow

Return value:

The function provides a bitmask indicating which of the recognized exceptions are currently active. Moreover, when a specific flag is enabled, the corresponding bit in the result will also be activated.

Key Points:

  • Fetestexept helps programs to identify speifi floating-point errors so they an be onditionally addressed after a alulation.
  • Floating-point exeption handling is required for appliations that rely heavily on numbers and where auray and error-heking are essential.
  • The funtionality boosts the auray and adaptability of mistake management by enabling the seletive examination of exeption flags.
  • Example 1:

Let's consider an example to demonstrate the fetsestexept function in Python.

Example

#inlude <stdio.h>

#inlude <fenv.h>

#inlude <math.h>



int main() {

    // lear all floating-point exeptions

    felearexept(FE_ALL_EXEPT);

    

    // Perform operations that ould raise exeptions

    double result = 1.0 / 0.0;  // Raises FE_DIVBYZERO

    

    // hek if the division by zero exeption was raised

    if (fetestexept(FE_DIVBYZERO)) {

        printf("Division by zero exeption raised.\n");

    }



    return 0;

}

Output:

Output

Division by zero exeption raised.

Example 2:

Let's consider another instance to demonstrate the fetsestexept method in Python.

Example

#include <stdio.h>

#include <fenv.h>

#include <math.h>



#pragma STDC FENV_ACCESS ON  // Ensure floating-point environment access



int main() {

    // Clear all floating-point exceptions

    feclearexcept(FE_ALL_EXCEPT);

    

    // Perform operations that may raise exceptions

    double x = 0.0;

    double y = 1.0 / x;           // Division by zero

    double z = pow(1e300, 2);      // Overflow

    

    // Check for multiple exceptions

    int raised_exceptions = fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);



    // Handle each exception case

    if (raised_exceptions & FE_DIVBYZERO) {

        printf("Division by zero exception raised.\n");

    }

    if (raised_exceptions & FE_OVERFLOW) {

        printf("Overflow exception raised.\n");

    }

    if (raised_exceptions & FE_UNDERFLOW) {

        printf("Underflow exception raised.\n");

    }



    return 0;

}

Output:

Output

Division by zero exception raised.

Explanation:

  • This code initially resolves the floating-point exceptions.
  • After that, it computes a massive power that causes overflow (pow(1e300, 2)) and divides by zero (1.0 / x).
  • Fetestexcept is used to run the tests for FEDIVBYZERO, FEOVERFLOW, and FE_UNDERFLOW.
  • The software sends a message stating if the associated exception occurred after examining each individual exception flag.
  • Example 3:

Let's consider another scenario to demonstrate the fastestexcept function in the C programming language.

Example

#include <stdio.h>

#include <fenv.h>

#include <math.h>



#pragma STDC FENV_ACCESS ON  // Ensure floating-point environment access



int main() {

    // Clear all floating-point exceptions

    feclearexcept(FE_ALL_EXCEPT);

    

    // Perform a series of operations that may trigger exceptions

    double a = 0.0;

    double b = -1.0;

    double result1 = sqrt(b);           // Invalid operation (sqrt of negative number)

    double result2 = 1.0 / a;            // Division by zero

    double result3 = pow(10, 308) * 10;  // Overflow

    double result4 = 1e-308 / 1e308;     // Underflow



    // Test for each floating-point exception

    int raised_exceptions = fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID | FE_INEXACT);



    // Check and print the results for each detected exception

    if (raised_exceptions & FE_INVALID) {

        printf("Invalid operation exception raised (e.g., sqrt of negative number).\n");

    }

    if (raised_exceptions & FE_DIVBYZERO) {

        printf("Division by zero exception raised.\n");

    }

    if (raised_exceptions & FE_OVERFLOW) {

        printf("Overflow exception raised.\n");

    }

    if (raised_exceptions & FE_UNDERFLOW) {

        printf("Underflow exception raised.\n");

    }

    if (raised_exceptions & FE_INEXACT) {

        printf("Inexact result exception raised (rounding occurred).\n");

    }



    return 0;

}

Output:

Output

Invalid operation exception raised (e.g., sqrt of negative number).

Division by zero exception raised.

Overflow exception raised.

Inexact result exception raised (rounding occurred).

Reset any previously configured floating-point exceptions by employing feclearexcept(FEALLEXCEPT);.

Methods:

  • An invalid operation (FE_INVALID) is produced when sqrt(b) attempts to take the square root of a negative value.
  • 0 / a: Division by zero, which results in FE_DIVBYZERO.
  • Pow(10, 308) * 10: Overflow (FE_OVERFLOW) occurs when the range for double is exceeded.
  • 1e-308 / 1e308: A little result that might result in underflow (FE_UNDERFLOW).
  • All significant floating-point exceptions are checked by fetestexcept.
  • Any particular exceptions raised by the operations are then identified and printed out by the code.
  • Conclusion:

In C programming, having a grasp of floating-point exceptions is crucial for developing precise and dependable numerical applications. Following calculations, the fetestexcept function from the fenv.h library offers a means to identify specific floating-point exceptions like division by zero, overflow, underflow, invalid operations, and imprecise outcomes. This function empowers developers to pinpoint the occurrence of any of these exceptions and respond accordingly, whether by documenting issues, adjusting computations, or triggering new processes. By adopting this method, even within intricate numerical computations, developers ensure that their programs uphold accuracy and resilience when encountering unexpected scenarios in floating-point arithmetic. Ultimately, fetestexcept empowers programmers to effectively handle floating-point errors, facilitating the creation of robust and fault-tolerant applications.

Input Required

This code uses input(). Please provide values below: