From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/numeric%2Ffenv%2Ffeenv.html | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 devdocs/c/numeric%2Ffenv%2Ffeenv.html (limited to 'devdocs/c/numeric%2Ffenv%2Ffeenv.html') diff --git a/devdocs/c/numeric%2Ffenv%2Ffeenv.html b/devdocs/c/numeric%2Ffenv%2Ffeenv.html new file mode 100644 index 00000000..55c9bb86 --- /dev/null +++ b/devdocs/c/numeric%2Ffenv%2Ffeenv.html @@ -0,0 +1,116 @@ +

fegetenv, fesetenv

Defined in header <fenv.h>
int fegetenv( fenv_t* envp );
+
(1) (since C99)
int fesetenv( const fenv_t* envp );
+
(2) (since C99)

1) Attempts to store the status of the floating-point environment in the object pointed to by envp.

+

2) Attempts to establish the floating-point environment from the object pointed to by envp. The value of that object must be previously obtained by a call to feholdexcept or fegetenv or be a floating-point macro constant. If any of the floating-point status flags are set in envp, they become set in the environment (and are then testable with fetestexcept), but the corresponding floating-point exceptions are not raised (execution continues uninterrupted)

+

Parameters

+ +
envp - pointer to the object of type fenv_t which holds the status of the floating-point environment

Return value

​0​ on success, non-zero otherwise.

+

Example

#include <stdio.h>
+#include <math.h>
+#include <fenv.h>
+ 
+#pragma STDC FENV_ACCESS ON
+ 
+void show_fe_exceptions(void)
+{
+    printf("current exceptions raised: ");
+    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
+    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
+    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
+    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
+    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
+    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
+    printf("\n");
+}
+ 
+void show_fe_rounding_method(void)
+{
+    printf("current rounding method:    ");
+    switch (fegetround()) {
+           case FE_TONEAREST:  printf ("FE_TONEAREST");  break;
+           case FE_DOWNWARD:   printf ("FE_DOWNWARD");   break;
+           case FE_UPWARD:     printf ("FE_UPWARD");     break;
+           case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break;
+           default:            printf ("unknown");
+    };
+    printf("\n");
+}
+ 
+void show_fe_environment(void)
+{
+    show_fe_exceptions();
+    show_fe_rounding_method();
+}    
+ 
+int main(void)
+{
+    fenv_t curr_env;
+    int rtn;
+ 
+    /* Show default environment. */
+    show_fe_environment();
+    printf("\n");
+ 
+    /* Perform some computation under default environment. */
+    printf("+11.5 -> %+4.1f\n", rint(+11.5)); /* midway between two integers */
+    printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* midway between two integers */
+    show_fe_environment();
+    printf("\n");
+ 
+    /* Save current environment. */
+    rtn = fegetenv(&curr_env);
+ 
+    /* Perform some computation with new rounding method. */
+    feclearexcept(FE_ALL_EXCEPT);
+    fesetround(FE_DOWNWARD);
+    printf("1.0/0.0 = %f\n", 1.0/0.0);
+    printf("+11.5 -> %+4.1f\n", rint(+11.5));
+    printf("+12.5 -> %+4.1f\n", rint(+12.5));
+    show_fe_environment();
+    printf("\n");
+ 
+    /* Restore previous environment. */
+    rtn = fesetenv(&curr_env);
+    show_fe_environment();
+ 
+    return 0;
+}

Output:

+
current exceptions raised: none
+current rounding method:   FE_TONEAREST
+ 
++11.5 -> +12.0
++12.5 -> +12.0
+current exceptions raised: FE_INEXACT
+current rounding method:   FE_TONEAREST
+ 
+1.0/0.0 = inf
++11.5 -> +11.0
++12.5 -> +12.0
+current exceptions raised: FE_DIVBYZERO FE_INEXACT
+current rounding method:   FE_DOWNWARD
+ 
+current exceptions raised: FE_INEXACT
+current rounding method:   FE_TONEAREST

References

See also

+ + + +
+
(C99)
saves the environment, clears all status flags and ignores all future errors
(function)
+
(C99)
restores the floating-point environment and raises the previously raise exceptions
(function)
+
(C99)
default floating-point environment
(macro constant)
C++ documentation for fegetenv, fesetenv
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/numeric/fenv/feenv +

+
-- cgit v1.2.3