// Compile: cc -std=c23 test_annex_F_iec60559.c -lm
#include <math.h>
#include <fenv.h>
#include <float.h>
#include <assert.h>

int main(void){
#if defined(__STDC_IEC_60559_BFP__)
  // Raise FE_DIVBYZERO and check it is set after 1.0/0.0 when traps are off.
  #pragma STDC FENV_ACCESS ON
  feclearexcept(FE_ALL_EXCEPT);
  volatile double z = 0.0;
  volatile double y = 1.0/z;
  (void)y;
  assert(fetestexcept(FE_DIVBYZERO));
  // Check rounding mode affects nearbyint as per Annex F.
  if (fesetround(FE_DOWNWARD)==0){
    double r = nearbyint(0.5);
    assert(r == 0.0);
    fesetround(FE_TONEAREST);
  }
#else
  (void)feclearexcept; (void)fetestexcept;
#endif
  return 0;
}
