Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is feraiseexcept() in C?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

feraiseexcept() raises the floating-point exceptions specified in the except argument. It is defined in the fenv.h header.

Prototype

int feraiseexcept(int except);

Parameters

  • except: A bitmask of all the floating-point exceptions that need to be raised. The bitmask is a bitwise OR of the specified floating-point exception macros.

Return value

  • feraiseexcept() returns 00 if all of the exceptions are successfully raised, or if the argument is 00. Otherwise, it returns a non-zero value.

Example

#include<stdio.h>
#include <fenv.h>
void raise_exceptions(){
feraiseexcept(FE_OVERFLOW);
feraiseexcept(FE_INEXACT);
feraiseexcept(FE_DIVBYZERO);
}
void show_exceptions(){
printf("Currently raised exceptions are: ");
if (fetestexcept(FE_DIVBYZERO)) printf("FE_DIVBYZERO ");
if (fetestexcept(FE_INVALID)) printf("FE_INVALID ");
if (fetestexcept(FE_INEXACT)) printf("FE_INEXACT ");
if (fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW ");
if (fetestexcept(FE_OVERFLOW)) printf("FE_OVERFLOW ");
if (!fetestexcept(FE_ALL_EXCEPT)) printf("NONE ");
printf("\n");
}
int main() {
raise_exceptions();
show_exceptions();
return 0;
}

The above code demonstrates how we can use feraiseexcept() to raise the desired exceptions.

We make calls to feraiseexcept() in the raise_exceptions() function, after which we can test and display the set exceptions using the show_exceptions() function.

The show_exceptions() function uses fetestexcept() to test for the given exception and returns a non-zero value if it is set. If the given exception is not set, it returns 00.

RELATED TAGS

c

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring