最終更新日時(UTC):
が更新

履歴 編集

macro
<cmath>

MATH_ERREXCEPT(C++11)

# define MATH_ERREXCEPT 2

概要

MATH_ERREXCEPTは、<cmath>内で浮動小数点例外が発生したかを表す整数定数マクロである。

この定数とmath_errhandlingでビットANDをとった結果がゼロでないか比較することにより、浮動小数点例外が発生したか否かを判定できる。

#include <iostream>
#include <cmath>
#include <cfenv>

int main()
{
  std::feclearexcept(FE_ALL_EXCEPT);
  std::log(0.0);

  if (math_errhandling & MATH_ERREXCEPT) {
    int excepts = std::fetestexcept(FE_ALL_EXCEPT);
    if (excepts & FE_INVALID) {
      std::cout << "FE_INVALID" << std::endl;
    }
    if (excepts & FE_DIVBYZERO) {
      std::cout << "FE_DIVBYZERO" << std::endl;
    }
    if (excepts & FE_OVERFLOW) {
      std::cout << "FE_OVERFLOW" << std::endl;
    }
    if (excepts & FE_UNDERFLOW) {
      std::cout << "FE_UNDERFLOW" << std::endl;
    }
    if (excepts & FE_INEXACT) {
      std::cout << "FE_INEXACT" << std::endl;
    }
  }
  else {
    std::cout << "no exception" << std::endl;
  }
}

出力例

FE_DIVBYZERO

バージョン

言語

  • C++11

処理系