# cfenv

***

**1. Get Environment Variables**

```cpp
#include <cfenv>
#include <cstdio>

int main() {
  std::printf("Current floating-point environment:\n");

  // Get control word
  const int cw = _control87(0, 0);
  std::printf("Control word: %x\n", cw);

  // Get rounding mode
  const int rm = (cw >> 10) & 0x3;
  switch (rm) {
    case 0: std::printf("Rounding mode: Round to nearest\n"); break;
    case 1: std::printf("Rounding mode: Round towards zero\n"); break;
    case 2: std::printf("Rounding mode: Round towards positive infinity\n"); break;
    case 3: std::printf("Rounding mode: Round towards negative infinity\n"); break;
  }

  // Get exception flags
  const int ef = (cw >> 13) & 0xf;
  if (ef & _EM_INVALID) std::printf("Invalid operation exception flag is set\n");
  if (ef & _EM_DENORMAL) std::printf("Denormalized number exception flag is set\n");
  if (ef & _EM_ZERODIVIDE) std::printf("Zero divide exception flag is set\n");
  if (ef & _EM_OVERFLOW) std::printf("Overflow exception flag is set\n");
  if (ef & _EM_UNDERFLOW) std::printf("Underflow exception flag is set\n");
  if (ef & _EM_INEXACT) std::printf("Inexact result exception flag is set\n");

  return 0;
}
```

**2. Set Control Word**

```cpp
#include <cfenv>

int main() {
  // Set control word to round towards zero
  const int cw = _control87(_RC_NEAR, _MCW_RC);

  std::printf("New control word: %x\n", cw);
  return 0;
}
```

**3. Get Exception Status**

```cpp
#include <cfenv>

int main() {
  // Get exception status
  const int es = _status87();

  std::printf("Exception status: %x\n", es);
  return 0;
}
```

**4. Clear Exception Flags**

```cpp
#include <cfenv>

int main() {
  // Clear exception flags
  _clear87();

  std::printf("Exception flags cleared\n");
  return 0;
}
```

**5. Raise Invalid Operation Exception**

```cpp
#include <cfenv>

int main() {
  // Raise invalid operation exception
  _fpreset();

  std::printf("Invalid operation exception raised\n");
  return 0;
}
```

**6. Handle Invalid Operation Exception**

```cpp
#include <cfenv>
#include <exception>

int main() {
  try {
    // Perform an operation that may raise an invalid operation exception
    ...

  } catch (const std::fenv\<InvalidOperationException>) {
    // Handle invalid operation exception
  }

  return 0;
}
```

**7. Get Floating-Point Precision**

```cpp
#include <cfenv>

int main() {
  // Get floating-point precision
  const int prec = __fpclassify(0.0);

  switch (prec) {
    case _FPCLASS_SNAN: std::printf("Floating-point precision: Signalling NaN\n"); break;
    case _FPCLASS_QNAN: std::printf("Floating-point precision: Quiet NaN\n"); break;
    case _FPCLASS_NINF: std::printf("Floating-point precision: Negative infinity\n"); break;
    case _FPCLASS_PINF: std::printf("Floating-point precision: Positive infinity\n"); break;
    case _FPCLASS_NZERO: std::printf("Floating-point precision: Negative zero\n"); break;
    case _FPCLASS_PZERO: std::printf("Floating-point precision: Positive zero\n"); break;
    case _FPCLASS_NDENORM: std::printf("Floating-point precision: Negative denormalized\n"); break;
    case _FPCLASS_PDENORM: std::printf("Floating-point precision: Positive denormalized\n"); break;
    case _FPCLASS_0: std::printf("Floating-point precision: 0\n"); break;
    case _FPCLASS_SUBNORMAL: std::printf("Floating-point precision: Subnormal\n"); break;
    case _FPCLASS_NORMAL: std::printf("Floating-point precision: Normal\n"); break;
  }

  return 0;
}
```

**8. Get Floating-Point Class**

```cpp
#include <cfenv>

int main() {
  // Get floating-point class
  const int cls = fpclassify(0.0);

  switch (cls) {
    case FP_SNAN: std::printf("Floating-point class: Signalling NaN\n"); break;
    case FP_QNAN: std::printf("Floating-point class: Quiet NaN\n"); break;
    case FP_INFINITE: std::printf("Floating-point class: Infinity\n"); break;
    case FP_ZERO: std::printf("Floating-point class: Zero\n"); break;
    case FP_SUBNORMAL: std::printf("Floating-point class: Subnormal\n"); break;
    case FP_NORMAL: std::printf("Floating-point class: Normal\n"); break;
  }

  return 0;
}
```

**9. Test Floating-Point Predicate**

```cpp
#include <cfenv>

int main() {
  // Test floating-point predicate
  const bool is_nan = std::isnan(0.0);

  std::printf("Is NaN: %d\n", is_nan);
  return 0;
}
```

**10. Convert Floating-Point Value to Integer**

```cpp
#include <cfenv>

int main() {
  // Convert floating-point value to integer
  const int i = std::fpclassify(0.0);

  std::printf("Integer value: %d\n", i);
  return 0;
}
```

**11. Convert Integer to Floating-Point Value**

```cpp
#include <cfenv>

int main() {
  // Convert integer to floating-point value
  const float f = std::fpclassify(0);

  std::printf("Floating-point value: %f\n", f);
  return 0;
}
```

**12. Set Default Floating-Point Environment**

```cpp
#include <cfenv>

int main() {
  // Set default floating-point environment
  _control87(0, _MCW_DEFAULT);

  std::printf("Default floating-point environment set\n");
  return 0;
}
```

**13. Get Floating-Point Status Word**

```cpp
#include <cfenv>

int main() {
  // Get floating-point status word
  const int sw = _get_statusword();

  std::printf("Floating-point status word: %x\n", sw);
  return 0;
}
```

**14. Set Floating-Point Status Word**

```cpp
#include <cfenv>

int main() {
  // Set floating-point status word
  _set_statusword(0);

  std::printf("Floating-point status word set\n");
  return 0;
}
```

**15. Get Floating-Point Control Word**

```cpp
#include <cfenv>

int main() {
  // Get floating-point control word
  const int cw = _get_controlword();

  std::printf("Floating-point control word: %x\n", cw);
  return 0;
}
```

**16. Set Floating-Point Control Word**

```cpp
#include <cfenv>

int main() {
  // Set floating-point control word
  _set_controlword(0);

  std::printf("Floating-point control word set\n");
  return 0;
}
```

**17. Get Floating-Point Tag Word**

```cpp
#include <cfenv>

int main() {
  // Get floating-point tag word
  const int

```
