MATH_ERRNO, MATH_ERREXCEPT, math_errhandling
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||
| Macro constants | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<cmath>
|
||
|
#define MATH_ERRNO 1
|
(C++11以降) | |
|
#define MATH_ERREXCEPT 2
|
(C++11以降) | |
|
#define math_errhandling /*implementation defined*/
|
(C++11以降) | |
マクロ定数
math_errhandling
は
int
型の式に展開され、
MATH_ERRNO
と等しいか、
MATH_ERREXCEPT
と等しいか、またはそれらのビット単位のOR(
MATH_ERRNO
|
MATH_ERREXCEPT
)と等しい値を持ちます。
math_errhandling
の値は、浮動小数点演算子および
関数
によって実行されるエラー処理のタイプを示します:
| 定数 | 説明 |
MATH_ERREXCEPT
|
浮動小数点例外が使用されることを示す: FE_DIVBYZERO , FE_INVALID , および FE_OVERFLOW が <cfenv> で定義されている。 |
MATH_ERRNO
|
浮動小数点演算が変数 errno を使用してエラーを報告することを示す。 |
実装がIEEE浮動小数点演算(IEC 60559)をサポートしている場合、 math_errhandling & MATH_ERREXCEPT は非ゼロであることが要求されます。
以下の浮動小数点エラー条件が認識されます:
| 条件 | 説明 | errno | 浮動小数点例外 | 例 |
|---|---|---|---|---|
| 定義域エラー | 引数が数学的に定義された操作の範囲外である( 各関数 の説明で必要な定義域エラーを列挙) | EDOM | FE_INVALID | std:: acos ( 2 ) |
| 極エラー | 関数の数学的結果が正確に無限大または未定義である | ERANGE | FE_DIVBYZERO | std:: log ( 0.0 ) , 1.0 / 0.0 |
| オーバーフローによる値域エラー | 数学的結果は有限だが、丸め後に無限大になる、または切り捨て後に表現可能な最大有限値になる | ERANGE | FE_OVERFLOW | std:: pow ( DBL_MAX , 2 ) |
| アンダーフローによる値域エラー | 結果が非ゼロだが、丸め後にゼロになる、または精度を失って非正規化数になる | ERANGE または変更なし(実装定義) | FE_UNDERFLOW またはなし(実装定義) | DBL_TRUE_MIN / 2 |
| 不正確な結果 | 結果を変換先の型に合わせるために丸めが必要である | 変更なし | FE_INEXACT またはなし(未規定) | std:: sqrt ( 2 ) , 1.0 / 10.0 |
注記
数学ライブラリ関数によって FE_INEXACT が発生するかどうかは一般的には未規定ですが、関数の説明で明示的に規定される場合があります(例: std::rint と std::nearbyint の比較)。
C++11以前、浮動小数点例外は規定されておらず、 EDOM はあらゆる定義域エラーに対して必須であり、 ERANGE はオーバーフローに対して必須、アンダーフローに対しては実装定義でした。
例
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "MATH_ERRNO is " << (math_errhandling & MATH_ERRNO ? "set" : "not set") << '\n' << "MATH_ERREXCEPT is " << (math_errhandling & MATH_ERREXCEPT ? "set" : "not set") << '\n'; std::feclearexcept(FE_ALL_EXCEPT); errno = 0; std::cout << "log(0) = " << std::log(0) << '\n'; if (errno == ERANGE) std::cout << "errno = ERANGE (" << std::strerror(errno) << ")\n"; if (std::fetestexcept(FE_DIVBYZERO)) std::cout << "FE_DIVBYZERO (pole error) reported\n"; }
出力例:
MATH_ERRNO is set MATH_ERREXCEPT is set log(0) = -inf errno = ERANGE (Numerical result out of range) FE_DIVBYZERO (pole error) reported
関連項目
|
浮動小数点例外
(マクロ定数) |
|
|
POSIX互換のスレッドローカルエラー番号変数に展開されるマクロ
(マクロ変数) |
|
|
Cドキュメント
for
math_errhandling
|
|