名稱空間
變體
操作

feclearexcept

來自 cppreference.com
< c‎ | 數值‎ | fenv
定義於標頭檔案 <fenv.h>
int feclearexcept( int excepts );
(C99 起)

嘗試清除在位掩碼引數 excepts 中列出的浮點異常,excepts浮點異常宏的按位或。

目錄

[編輯] 引數

excepts - 列出要清除的異常標誌的位掩碼

[編輯] 返回值

0 如果所有指示的異常都已成功清除,或者 excepts 為零。錯誤時返回非零值。

[編輯] 示例

#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;
}

輸出

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

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.6.2.1 feclearexcept 函式 (p: 209)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.6.2.1 feclearexcept 函式 (p: 190)

[編輯] 參閱

確定哪些指定的浮點狀態標誌已設定
(函式) [編輯]
C++ 文件,關於 feclearexcept