/* folded_flags.c -- the same operations as float_flags_c.c, with constant
   operands. Whether a flag is raised now depends on whether the compiler did
   the arithmetic while compiling or left it for the program.
   Build: cc -std=c17 -Wall -Wextra -pedantic folded_flags.c -lm -o folded_flags */
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>

static void show(const char *what)
{
    int raised = fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_INEXACT);
    printf("%-13s raised:", what);
    if (raised & FE_INVALID)   printf(" FE_INVALID");
    if (raised & FE_DIVBYZERO) printf(" FE_DIVBYZERO");
    if (raised & FE_OVERFLOW)  printf(" FE_OVERFLOW");
    if (raised & FE_INEXACT)   printf(" FE_INEXACT");
    if (!raised)               printf(" (none)");
    printf("\n");
}

int main(void)
{
    double r;
    feclearexcept(FE_ALL_EXCEPT); r = sqrt(-42.0);   show("sqrt(-42.0)");
    feclearexcept(FE_ALL_EXCEPT); r = 1.0 / 0.0;     show("1.0 / 0.0");
    feclearexcept(FE_ALL_EXCEPT); r = DBL_MAX * 2.0; show("DBL_MAX * 2.0");
    feclearexcept(FE_ALL_EXCEPT); r = 0.1 + 0.2;     show("0.1 + 0.2");
    (void)r;
    return 0;
}
