# 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;
}
}
xxxxxxxxxx
#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
処理系
- Clang: 3.0 ✅
- GCC: 4.3 ✅
- ICC: ??
- Visual C++: ??