feholdexcept
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <fenv.h> で定義
|
||
int feholdexcept( fenv_t* envp ); |
(C99以上) | |
まず、現在の浮動小数点環境を envp の指すオブジェクトに保存し (fegetenv と同様)、すべての浮動小数点数ステータスフラグをクリアし、非停止モードを設定します。 つまり、 feupdateenv または fesetenv によって浮動小数点環境を復元するまで、今後の浮動小数点例外が実行に割り込まなく (トラップしなく) なります。
この関数は発生するかもしれない浮動小数点例外を呼び出し元から隠さなければならないサブルーチンの先頭で使用することができます。 いくつかの例外だけを抑制し、他は報告しなければならない場合は、通常、欲しくない例外をクリアした後、 feupdateenv を呼ぶことによって終了します。
引数
| envp | - | 浮動小数点環境を格納する fenv_t 型のオブジェクトを指すポインタ
|
戻り値
成功した場合は 0、そうでなければ非ゼロ。
例
Run this code
#include <stdio.h>
#include <fenv.h>
#include <float.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");
}
double x2 (double x) /* times two */
{
fenv_t curr_excepts;
/* Save and clear current f-p environment. */
feholdexcept(&curr_excepts);
/* Raise inexact and overflow exceptions. */
printf("In x2(): x = %f\n", x=x*2.0);
show_fe_exceptions();
feclearexcept(FE_INEXACT); /* hide inexact exception from caller */
/* Merge caller's exceptions (FE_INVALID) */
/* with remaining x2's exceptions (FE_OVERFLOW). */
feupdateenv(&curr_excepts);
return x;
}
int main(void)
{
feclearexcept(FE_ALL_EXCEPT);
feraiseexcept(FE_INVALID); /* some computation with invalid argument */
show_fe_exceptions();
printf("x2(DBL_MAX) = %f\n", x2(DBL_MAX));
show_fe_exceptions();
return 0;
}
出力:
current exceptions raised: FE_INVALID
In x2(): x = inf
current exceptions raised: FE_INEXACT FE_OVERFLOW
x2(DBL_MAX) = inf
current exceptions raised: FE_INVALID FE_OVERFLOW
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.6.4.2 The feholdexcept function (p: 213-214)
- C99 standard (ISO/IEC 9899:1999):
- 7.6.4.2 The feholdexcept function (p: 194-195)
関連項目
(C99) |
浮動小数点環境を復元し、以前に発生した例外を発生させます (関数) |
(C99) |
浮動小数点環境を保存または復元します (関数) |
(C99) |
デフォルトの浮動小数点環境 (マクロ定数) |
feholdexcept の C++リファレンス
| |