0

Suppose I have a C program that manifests a floating-point overflow, such as in the example below.

int main(){
  double x,y;

  x=1e+300;
  y=x*x;

  printf ("x = %.5g; y = %.5g, MAX = %.5g\n", x, y, DBL_MAX);
}

My goal is to catch the exception at run-time. I have thousands of C programs, so I will need to do it in an automated way. I wonder if there exists a C command line option for my purpose?

What is expected is:

  • gcc myprog.c --the_option_wanted ==> This command is expected to compile myprog.c without any problem

  • ./a.out ==> This command runs myprog.c, and the floating-point exception is expected to become a warning or error when running it.

Lance Richardson
  • 4,610
  • 23
  • 30
zell
  • 9,830
  • 10
  • 62
  • 115
  • 1
    The best you can get is to setup a handler for SIGFPE and modify the [floating point environment](http://en.cppreference.com/w/c/numeric/fenv) to raise it. It's not an exception however. C doesn't include an exception model. – StoryTeller - Unslander Monica Mar 12 '17 at 14:46
  • @StoryTeller Thanks. How can one set the handler? Would you elaborate? I have thousands of C programs for which I want to make exceptions explicit, so I will need to do it in an automated way. – zell Mar 12 '17 at 14:51
  • The reference site is a good place to start. And you'll have a hard time automating it. You'll need to inject code at the start of every main function. Or not, you'll have to scour the GCC docs to see if you can setup a default environment. – StoryTeller - Unslander Monica Mar 12 '17 at 14:53
  • C does not support exceptions. If you mean signals: they are also not mandatory. For overflow, you possibly get `+/-INF`. – too honest for this site Mar 12 '17 at 15:25
  • Seems like a duplicate of SO question ["Catch Floating Point Exceptions using a compiler option with C"](http://stackoverflow.com/questions/18644168/catch-floating-point-exceptions-using-a-compiler-option-with-c). In the accepted answer, Kerrek SB explains: "Floating point exceptions are controlled by library code in C99, not by compiler flags". This seems consistent with other online information like at [man7 for fetestexcept](http://man7.org/linux/man-pages/man3/fetestexcept.3.html). – Louis Langholtz Mar 12 '17 at 17:07
  • The compiler may make your life hell by ignoring the effects of ordinary floating-point operations on the exception bits. I am not aware of a mainstream compiler that respects C99's `#pragma STDC FENV_ACCESS ON`. Your requirements may also make your life hell; do you want programs to bomb out if they do an inexact operation? – tmyklebu Mar 12 '17 at 18:21

0 Answers0