/* trap_invalid.c -- asking for a real trap. feenableexcept is a GNU extension,
   not standard C: it unmasks an exception, so the next invalid operation
   stops the program with SIGFPE instead of returning NaN.
   Build: cc -std=c17 -Wall -Wextra -pedantic trap_invalid.c -lm -o trap_invalid */
#define _GNU_SOURCE
#include <fenv.h>
#include <math.h>
#include <stdio.h>

int main(void)
{
    volatile double minus_42 = -42.0;
    feenableexcept(FE_INVALID);
    printf("FE_INVALID now traps; calling sqrt(-42.0)\n");
    fflush(stdout);
    double r = sqrt(minus_42);
    printf("still running: isnan %d\n", isnan(r) != 0);
    return 0;
}
