Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.22 KB

File metadata and controls

75 lines (59 loc) · 2.22 KB

underflow_error

  • stdexcept[meta header]
  • std[meta namespace]
  • class[meta id-type]
namespace std {
  class underflow_error : public runtime_error;
}
  • runtime_error[link runtime_error.md]

概要

underflow_errorクラスは、算術演算の結果がアンダーフロー(扱える値の下限を超過、または絶対値が小さすぎて表現できない)したことを通知するために送出される例外クラスである。

標準ライブラリのコンポーネントがこの例外を送出することはなく、ユーザーが使用できる。

このクラスはruntime_errorを公開継承しており、runtime_errorとして捕捉できる。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ
(destructor) デストラクタ
operator= 代入演算子
what エラー理由を取得する(std::exceptionから継承)

#include <stdexcept>
#include <iostream>
#include <limits>
#include <cmath>

// 演算結果が非正規化数となり、精度が失われる場合をアンダーフローとして通知する
double divide(double a, double b)
{
  double result = a / b;
  if (result != 0.0 && std::abs(result) < std::numeric_limits<double>::min()) {
    throw std::underflow_error("result is too small to represent");
  }
  return result;
}

int main()
{
  try {
    divide(1e-300, 1e10);
  }
  catch (const std::underflow_error& e) {
    std::cout << e.what() << std::endl;
  }
}
  • std::underflow_error[color ff0000]
  • std::numeric_limits[link /reference/limits/numeric_limits.md]
  • std::abs[link /reference/cmath/abs.md]

出力

result is too small to represent

関連項目

参照