Namespaces
Variants

fegetexceptflag, fesetexceptflag

From cppreference.net
< c ‎ | numeric ‎ | fenv
ヘッダーで定義 <fenv.h>
int fegetexceptflag ( fexcept_t * flagp, int excepts ) ;
(1) (C99以降)
int fesetexceptflag ( const fexcept_t * flagp, int excepts ) ;
(2) (C99以降)

1) ビットマスク引数 excepts にリストされている浮動小数点例外フラグの完全な内容を取得しようとします。 excepts 浮動小数点例外マクロ のビット単位のORです。

2) excepts にリストされている浮動小数点例外フラグの完全な内容を flagp から浮動小数点環境へコピーしようと試みます。例外を発生させず、フラグのみを変更します。

浮動小数点例外フラグの完全な内容は、必ずしも例外が発生したかクリアされたかを示すブール値ではありません。例えば、ブール値のステータスと例外をトリガーしたコードのアドレスを含む構造体である可能性があります。これらの関数はそのような内容をすべて取得し、実装定義の形式で flagp に取得/保存します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Example、References、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは一切変更していません - 数値や書式設定は完全に保持されています

パラメータ

flagp - フラグが格納または読み出される fexcept_t オブジェクトへのポインタ
excepts - 取得/設定する例外フラグをリストするビットマスク

戻り値

0 成功時は0、それ以外の場合は非ゼロ。

#include <stdio.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");
}
int main(void)
{
    fexcept_t excepts;
    /* 現在の例外フラグのセットアップ */
    feraiseexcept(FE_INVALID);
    show_fe_exceptions();
    /* 現在の例外フラグを保存 */
    fegetexceptflag(&excepts,FE_ALL_EXCEPT);
    /* 一時的に他の2つの例外を発生 */
    feclearexcept(FE_ALL_EXCEPT);
    feraiseexcept(FE_OVERFLOW | FE_INEXACT);
    show_fe_exceptions();
    /* 以前の例外フラグを復元 */
    fesetexceptflag(&excepts,FE_ALL_EXCEPT);
    show_fe_exceptions();
    return 0;
}

出力:

current exceptions raised: FE_INVALID
current exceptions raised: FE_INEXACT FE_OVERFLOW
current exceptions raised: FE_INVALID

参考文献

  • C11規格 (ISO/IEC 9899:2011):
  • 7.6.2.2 fegetexceptflag関数 (p: 210)
  • 7.6.2.4 fesetexceptflag関数 (p: 211)
  • C99規格 (ISO/IEC 9899:1999):
  • 7.6.2.2 fegetexceptflag関数 (p: 191)
  • 7.6.2.4 fesetexceptflag関数 (p: 192)

関連項目

C++ドキュメント for fegetexceptflag , fesetexceptflag