Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      feclearexcept

      From cppreference.com
      <c‎ |numeric‎ |fenv
       
       
       
       
      Defined in header<fenv.h>
      int feclearexcept(int excepts);
      (since C99)

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

      Contents

      [edit]Parameters

      excepts - bitmask listing the exception flags to clear

      [edit]Return value

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

      [edit]Example

      Run this code
      #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){constint range_problem=FE_OVERFLOW|FE_UNDERFLOW;  feclearexcept(range_problem);// try a fast algorithmdouble result=sqrt(a* a+ b* b);if(!fetestexcept(range_problem))// no overflow or underflowreturn result;// return the fast result// do a more complicated calculation to avoid overflow or underflowint a_exponent,b_exponent;frexp(a,&a_exponent);frexp(b,&b_exponent); if(a_exponent- b_exponent>DBL_MAX_EXP)returnfabs(a)+fabs(b);// we can ignore the smaller value// scale so that fabs(a) is near 1double 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 scalingreturnscalbn(result, a_exponent);} int main(void){// Normal case takes the fast routeprintf("hypot(%f, %f) = %f\n",3.0,4.0, hypot_demo(3.0,4.0));// Extreme case takes the slow but more accurate routeprintf("hypot(%e, %e) = %e\n",DBL_MAX/2.0,DBL_MAX/2.0,                                 hypot_demo(DBL_MAX/2.0,DBL_MAX/2.0)); return0;}

      Output:

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

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 7.6.2.1 The feclearexcept function (p: 209)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.6.2.1 The feclearexcept function (p: 190)

      [edit]See also

      determines which of the specified floating-point status flags are set
      (function)[edit]
      C++ documentation forfeclearexcept
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/fenv/feclearexcept&oldid=133793"

      [8]ページ先頭

      ©2009-2025 Movatter.jp