# fenv

***

**1. Checking for Support of Floating-Point Exceptions (FE\_ALL\_EXCEPT)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  if (fegetexceptflag(&env, FE_ALL_EXCEPT) & FE_ALL_EXCEPT) {
    printf("All floating-point exceptions are supported.\n");
  } else {
    printf("Some or all floating-point exceptions are not supported.\n");
  }
  return 0;
}
```

**2. Setting Floating-Point Exceptions (FE\_ALL\_EXCEPT)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  fesetenv(&env);
  if (fegetexceptflag(&env, FE_ALL_EXCEPT) & FE_ALL_EXCEPT) {
    printf("All floating-point exceptions are enabled.\n");
  }
  return 0;
}
```

**3. Clearing Floating-Point Exceptions (FE\_ALL\_EXCEPT)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  feclearexcept(FE_ALL_EXCEPT);
  if (fegetexceptflag(&env, FE_ALL_EXCEPT) == 0) {
    printf("All floating-point exceptions are cleared.\n");
  }
  return 0;
}
```

**4. Getting the Exception Status (FE\_ALL\_EXCEPT)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int exceptions = fegetexceptflag(&env, FE_ALL_EXCEPT);
  if (exceptions & FE_INVALID) {
    printf("Invalid operation.\n");
  }
  return 0;
}
```

**5. Raising an Invalid Operation Exception (FE\_INVALID)**

```cpp
#include <fenv.h>
int main() {
  feclearexcept(FE_ALL_EXCEPT);
  float x = 0.0;
  float y = 0.0;
  float z = x / y;  // Raising an invalid operation exception (division by zero)
  if (fetestexcept(FE_INVALID)) {
    printf("Invalid operation exception raised.\n");
  }
  return 0;
}
```

**6. Ignoring Floating-Point Exceptions (FE\_NOMASK)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  fesetround(FE_TONEAREST);
  env.__status &= ~FE_NOMASK;
  fesetenv(&env);
  float x = 0.1;
  float y = 0.2;
  float z = x - y;  // Floating-point exception is ignored
  return 0;
}
```

**7. Trapping Floating-Point Exceptions (FE\_NOTIFY)**

```cpp
#include <fenv.h>
#include <signal.h>
void handler(int sig) {
  printf("Floating-point exception occurred.\n");
}
int main() {
  signal(SIGFPE, handler);
  fenv_t env;
  fegetenv(&env);
  env.__status |= FE_NOTIFY;
  fesetenv(&env);
  float x = 0.0;
  float y = 0.0;
  float z = x / y;  // Floating-point exception is trapped
  return 0;
}
```

**8. Setting the Floating-Point Rounding Mode (FE\_TONEAREST)**

```cpp
#include <fenv.h>
int main() {
  fesetround(FE_TONEAREST);
  float x = 0.1;
  float y = 0.2;
  float z = x + y;  // Result rounded to nearest even
  return 0;
}
```

**9. Getting the Floating-Point Rounding Mode (FE\_TONEAREST)**

```cpp
#include <fenv.h>
int main() {
  int rounding_mode = fesetround(FE_TONEAREST);
  if (rounding_mode == FE_TONEAREST) {
    printf("Rounding mode set to nearest even.\n");
  }
  return 0;
}
```

**10. Checking the Current Processor's Floating-Point State (FE\_DFL\_ENV)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  if (env.__control == FE_DFL_ENV) {
    printf("Current processor's floating-point state is default.\n");
  }
  return 0;
}
```

**11. Setting the Processor's Floating-Point State (FE\_DFL\_ENV)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__control = FE_DFL_ENV;
  fesetenv(&env);
  printf("Processor's floating-point state set to default.\n");
  return 0;
}
```

**12. Checking for Floating-Point Unit (FE\_HARDWARE)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_HARDWARE)) {
    printf("Floating-point unit is present.\n");
  } else {
    printf("Floating-point unit is not present.\n");
  }
  return 0;
}
```

**13. Checking for In-Range Support (FE\_INEXACT)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_INEXACT)) {
    printf("In-range support is available.\n");
  } else {
    printf("In-range support is not available.\n");
  }
  return 0;
}
```

**14. Checking for Underflow Support (FE\_UNDERFLOW)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_UNDERFLOW)) {
    printf("Underflow support is available.\n");
  } else {
    printf("Underflow support is not available.\n");
  }
  return 0;
}
```

**15. Checking for Overflow Support (FE\_OVERFLOW)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_OVERFLOW)) {
    printf("Overflow support is available.\n");
  } else {
    printf("Overflow support is not available.\n");
  }
  return 0;
}
```

**16. Checking for Divide-by-Zero Support (FE\_DIVBYZERO)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_DIVBYZERO)) {
    printf("Divide-by-zero support is available.\n");
  } else {
    printf("Divide-by-zero support is not available.\n");
  }
  return 0;
}
```

**17. Getting the Floating-Point Status Flags**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int flags = env.__status;
  printf("Current floating-point status flags: 0x%x\n", flags);
  return 0;
}
```

**18. Setting the Floating-Point Status Flags**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__status |= FE_INVALID;
  fesetenv(&env);
  printf("Setting the invalid operation flag in the floating-point status flags.\n");
  return 0;
}
```

**19. Resetting the Floating-Point Status Flags**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__status &= ~FE_INVALID;
  fesetenv(&env);
  printf("Resetting the invalid operation flag in the floating-point status flags.\n");
  return 0;
}
```

**20. Getting the Floating-Point Control Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int control_mode = env.__control;
  printf("Current floating-point control mode: 0x%x\n", control_mode);
  return 0;
}
```

**21. Setting the Floating-Point Control Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__control |= FE_DFL_ENV;
  fesetenv(&env);
  printf("Setting the floating-point control mode to default.\n");
  return 0;
}
```

**22. Resetting the Floating-Point Control Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__control &= ~FE_DFL_ENV;
  fesetenv(&env);
  printf("Resetting the floating-point control mode to default.\n");
  return 0;
}
```

**23. Getting the Floating-Point Precision Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int precision = env.__precision;
  printf("Current floating-point precision mode: %d\n", precision);
  return 0;
}
```

**24. Setting the Floating-Point Precision Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__precision = FE_DOUBLE;
  fesetenv(&env);
  printf("Setting the floating-point precision mode to double.\n");
  return 0;
}
```

**25. Resetting the Floating-Point Precision Mode**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__precision = FE_SINGLE;
  fesetenv(&env);
  printf("Resetting the floating-point precision mode to single.\n");
  return 0;
}
```

**26. Rounding to a Specified Precision**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__precision = FE_EXTENDED;
  fesetenv(&env);
  double x = 1.234567890123456789;
  double y = fesetprecision(env, FE_DOUBLE);
  printf("Rounded value of x to double precision: %.16f\n", y);
  return 0;
}
```

**27. Getting the Floating-Point Rounding Direction**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int rounding_direction = env.__rounding;
  printf("Current floating-point rounding direction: %d\n", rounding_direction);
  return 0;
}
```

**28. Setting the Floating-Point Rounding Direction**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__rounding = FE_TONEAREST;
  fesetenv(&env);
  printf("Setting the floating-point rounding direction to nearest even.\n");
  return 0;
}
```

**29. Resetting the Floating-Point Rounding Direction**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__rounding = FE_UPWARD;
  fesetenv(&env);
  printf("Resetting the floating-point rounding direction to upward.\n");
  return 0;
}
```

**30. Getting the Floating-Point MXCSR Register**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  unsigned int mxcsr = env.__mxcsr;
  printf("Current value of the floating-point MXCSR register: 0x%x\n", mxcsr);
  return 0;
}
```

**31. Setting the Floating-Point MXCSR Register**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__mxcsr |= MXCSR_MASKED_INVALID_OP;
  fesetenv(&env);
  printf("Setting the masked invalid operation bit in the floating-point MXCSR register.\n");
  return 0;
}
```

**32. Resetting the Floating-Point MXCSR Register**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__mxcsr &= ~MXCSR_MASKED_INVALID_OP;
  fesetenv(&env);
  printf("Resetting the masked invalid operation bit in the floating-point MXCSR register.\n");
  return 0;
}
```

**33. Checking if the Floating-Point Unit is Busy (FE\_BUSY)**

```cpp
#include <fenv.h>
int main() {
  if (fegetexceptflag(FE_BUSY)) {
    printf("Floating-point unit is busy.\n");
  } else {
    printf("Floating-point unit is not busy.\n");
  }
  return 0;
}
```

**34. Clearing the Floating-Point Unit Busy Flag (FE\_BUSY)**

```cpp
#include <fenv.h>
int main() {
  feclearexcept(FE_BUSY);
  printf("Clearing the floating-point unit busy flag.\n");
  return 0;
}
```

**35. Getting the Floating-Point Status of an FPU Register (FE\_STATUS)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int status = fegetstatus(env.__status);
  printf("Floating-point status of the status register: %d\n", status);
  return 0;
}
```

**36. Setting the Floating-Point Status of an FPU Register (FE\_STATUS)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__status |= FE_INVALID;
  fesetstatus(env.__status, env.__status);
  printf("Setting the invalid operation bit in the floating-point status register.\n");
  return 0;
}
```

**37. Resetting the Floating-Point Status of an FPU Register (FE\_STATUS)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__status &= ~FE_INVALID;
  fesetstatus(env.__status, env.__status);
  printf("Resetting the invalid operation bit in the floating-point status register.\n");
  return 0;
}
```

**38. Getting the Floating-Point Control Mode of an FPU Register (FE\_CONTROL)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int control_mode = fegetcontrol(env.__control);
  printf("Floating-point control mode of the control register: %d\n", control_mode);
  return 0;
}
```

**39. Setting the Floating-Point Control Mode of an FPU Register (FE\_CONTROL)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__control |= FE_DFL_ENV;
  fesetcontrol(env.__control, env.__control);
  printf("Setting the default environment in the floating-point control register.\n");
  return 0;
}
```

**40. Resetting the Floating-Point Control Mode of an FPU Register (FE\_CONTROL)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__control &= ~FE_DFL_ENV;
  fesetcontrol(env.__control, env.__control);
  printf("Resetting the default environment in the floating-point control register.\n");
  return 0;
}
```

**41. Getting the Floating-Point Precision Mode of an FPU Register (FE\_PRECISION)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int precision = fegetprecision(env.__precision);
  printf("Floating-point precision mode of the precision register: %d\n", precision);
  return 0;
}
```

**42. Setting the Floating-Point Precision Mode of an FPU Register (FE\_PRECISION)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__precision |= FE_DOUBLE;
  fesetprecision(env.__precision, env.__precision);
  printf("Setting the double precision in the floating-point precision register.\n");
  return 0;
}
```

**43. Resetting the Floating-Point Precision Mode of an FPU Register (FE\_PRECISION)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__precision &= ~FE_DOUBLE;
  fesetprecision(env.__precision, env.__precision);
  printf("Resetting the double precision in the floating-point precision register.\n");
  return 0;
}
```

**44. Getting the Floating-Point Rounding Direction of an FPU Register (FE\_ROUNDING)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  int rounding_direction = fegetrounding(env.__rounding);
  printf("Floating-point rounding direction of the rounding register: %d\n", rounding_direction);
  return 0;
}
```

**45. Setting the Floating-Point Rounding Direction of an FPU Register (FE\_ROUNDING)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__rounding |= FE_TONEAREST;
  fesetrounding(env.__rounding, env.__rounding);
  printf("Setting the nearest even rounding direction in the floating-point rounding register.\n");
  return 0;
}
```

**46. Resetting the Floating-Point Rounding Direction of an FPU Register (FE\_ROUNDING)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__rounding &= ~FE_TONEAREST;
  fesetrounding(env.__rounding, env.__rounding);
  printf("Resetting the nearest even rounding direction in the floating-point rounding register.\n");
  return 0;
}
```

**47. Getting the Floating-Point MXCSR Register of an FPU Register (FE\_MXCSR)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  unsigned int mxcsr = fegetmxcsr(env.__mxcsr);
  printf("Floating-point MXCSR register of the MXCSR register: 0x%x\n", mxcsr);
  return 0;
}
```

**48. Setting the Floating-Point MXCSR Register of an FPU Register (FE\_MXCSR)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__mxcsr |= MXCSR_MASKED_INVALID_OP;
  fesetmxcsr(env.__mxcsr, env.__mxcsr);
  printf("Setting the masked invalid operation bit in the floating-point MXCSR register.\n");
  return 0;
}
```

**49. Resetting the Floating-Point MXCSR Register of an FPU Register (FE\_MXCSR)**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  fegetenv(&env);
  env.__mxcsr &= ~MXCSR_MASKED_INVALID_OP;
  fesetmxcsr(env.__mxcsr, env.__mxcsr);
  printf("Resetting the masked invalid operation bit in the floating-point MXCSR register.\n");
  return 0;
}
```

**50. Combining and Using Multiple Floating-Point Environment Functions**

```cpp
#include <fenv.h>
int main() {
  fenv_t env;
  // Get the current floating-point environment
  fegetenv(&env);
  // Set the floating-point rounding direction to nearest even
  fesetround(FE_TONEAREST);
  // Set the floating-point exception status flags to all masked
  env.__status |= FE_ALL_EXCEPT;
  fesetstatus(env.__status, env.__status);
  // Set the floating-point control mode to default
  fesetcontrol(env.__control, FE_DFL_ENV);
  // Enable the floating-point exception trap
  env.__status |= FE_NOTIFY;
  fesetenv(&env);
  printf("Floating-point environment configuration complete.\n");
  return 0;
}
```
