1. Checking for Support of Floating-Point Exceptions (FE_ALL_EXCEPT)
#include <fenv.h>intmain(){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");}return0;}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#include <fenv.h>
int main() {
feclearexcept(FE_BUSY);
printf("Clearing the floating-point unit busy flag.\n");
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}