fetestexcept
From cppreference.com
| ヘッダ <fenv.h> で定義 |
||
| int fetestexcept( int excepts ); |
(C99以降) | |
指定された浮動小数点例外のサブセットのうち、現在設定されているものを判断します。引数exceptsは、浮動小数点例外マクロのビットごとのORです。
目次 |
[編集] パラメータ
| excepts | - | テストする例外フラグを指定するビットマスク |
[編集] 戻り値
exceptsに含まれており、かつ現在設定されている浮動小数点例外に対応する浮動小数点例外マクロのビットごとのOR。
[編集] 例
このコードを実行
#include <stdio.h> #include <math.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"); } int main(void) { /* Show default set of exception flags. */ show_fe_exceptions(); /* Perform some computations which raise exceptions. */ printf("1.0/0.0 = %f\n", 1.0/0.0); /* FE_DIVBYZERO */ printf("1.0/10.0 = %f\n", 1.0/10.0); /* FE_INEXACT */ printf("sqrt(-1) = %f\n", sqrt(-1)); /* FE_INVALID */ printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0); /* FE_INEXACT FE_OVERFLOW */ printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n", nextafter(DBL_MIN/pow(2.0,52),0.0)); /* FE_INEXACT FE_UNDERFLOW */ show_fe_exceptions(); return 0; }
出力
current exceptions raised: none 1.0/0.0 = inf 1.0/10.0 = 0.100000 sqrt(-1) = -nan DBL_MAX*2.0 = inf nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0 current exceptions raised: FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW