Thread: How to raise different floating point exceptions in C?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    3

    Question How to raise different floating point exceptions in C?

    I am interested in raising floating point exceptions like Division by zero, Overflow, Underflow, etc.

    I am using functions in fenv.h and signal handling. Whenever, any floating point exception occurs,

    The problem seems like when inside the function catch_fpe ,the flags set by feraiseexcept somehow gets cleared and no condition is true.

    Code:
     
    #define _GNU_SOURCE
    #include <fenv.h>
    #include <stdio.h>
    #include <signal.h>
    #include <stdlib.h>
    void catch_fpe(int sig)
    {
      //#pragma STDC FENV_ACCESS ON 
      printf("i am in catch_fpe\n");
      if (fetestexcept(FE_OVERFLOW))
        printf("OVERFLOW\n");
      else if (fetestexcept(FE_UNDERFLOW))
        printf("UNDERFLOW\n");
      else if (fetestexcept(FE_DIVBYZERO))
        printf("DIVBYZERO\n");
      else if (fetestexcept(FE_INVALID))
        printf("INVALID OPERATION\n");
      else if (fetestexcept(FE_INEXACT))
        printf("INEXACT RESULT\n");
      exit(0);
    }
    
    int main()
    {
      feclearexcept(FE_ALL_EXCEPT);
      feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
      signal(SIGFPE, catch_fpe);
      feraiseexcept(FE_DIVBYZERO);
    }
    Last edited by Salem; 06-27-2019 at 06:36 AM. Reason: Removed list, numbers are added automatically by code tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating point exceptions
    By Scarecrowm in forum C Programming
    Replies: 2
    Last Post: 10-18-2007, 08:57 AM
  2. Floating point Hex.
    By cstubbs50 in forum C Programming
    Replies: 4
    Last Post: 11-16-2005, 04:29 PM
  3. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  4. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 08:32 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread