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%2Ffeclearexcept.html | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html (limited to 'devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html') diff --git a/devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html b/devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html new file mode 100644 index 00000000..04dbc3ee --- /dev/null +++ b/devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html @@ -0,0 +1,63 @@ +

feclearexcept

Defined in header <fenv.h>
int feclearexcept( int excepts );
+
(since C99)

Attempts to clear the floating-point exceptions that are listed in the bitmask argument excepts, which is a bitwise OR of the floating-point exception macros.

+

Parameters

+ +
excepts - bitmask listing the exception flags to clear

Return value

​0​ if all indicated exceptions were successfully cleared or if excepts is zero. Returns a non-zero value on error.

+

Example

#include <fenv.h>
+#include <stdio.h>
+#include <math.h>
+#include <float.h>
+ 
+/*
+ * A possible implementation of hypot which makes use of many advanced
+ * floating-point features.
+ */
+double hypot_demo(double a, double b) {
+  const int range_problem = FE_OVERFLOW | FE_UNDERFLOW;
+  feclearexcept(range_problem);
+  // try a fast algorithm
+  double result = sqrt(a * a + b * b);
+  if (!fetestexcept(range_problem))  // no overflow or underflow
+    return result;                   // return the fast result
+  // do a more complicated calculation to avoid overflow or underflow
+  int a_exponent,b_exponent;
+  frexp(a, &a_exponent);
+  frexp(b, &b_exponent);
+ 
+  if (a_exponent - b_exponent > DBL_MAX_EXP)
+    return fabs(a) + fabs(b);        // we can ignore the smaller value
+  // scale so that fabs(a) is near 1
+  double a_scaled = scalbn(a, -a_exponent);
+  double b_scaled = scalbn(b, -a_exponent);
+  // overflow and underflow is now impossible 
+  result = sqrt(a_scaled * a_scaled + b_scaled * b_scaled);
+  // undo scaling
+  return scalbn(result, a_exponent);
+}
+ 
+int main(void)
+{
+  // Normal case takes the fast route
+  printf("hypot(%f, %f) = %f\n", 3.0, 4.0, hypot_demo(3.0, 4.0));
+  // Extreme case takes the slow but more accurate route
+  printf("hypot(%e, %e) = %e\n", DBL_MAX / 2.0, 
+                                DBL_MAX / 2.0, 
+                                hypot_demo(DBL_MAX / 2.0, DBL_MAX / 2.0));
+ 
+  return 0;
+}

Output:

+
hypot(3.000000, 4.000000) = 5.000000
+hypot(8.988466e+307, 8.988466e+307) = 1.271161e+308

References

See also

+ +
+
(C99)
determines which of the specified floating-point status flags are set
(function)
C++ documentation for feclearexcept
+

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

+
-- cgit v1.2.3