Skip to content

Commit ee014a0

Browse files
committed
cmath : NaNを受け取ったらNaNを返す仕様を記載 #373
1 parent 67a2559 commit ee014a0

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

reference/cmath.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [乗算・加算](#floating-multiply-add)
2121
- [線形補完](#linear-interpolation)
2222
- [数値分類](#classification-functions)
23+
- [NaNの扱い](#nan-handling)
2324
- [数値比較](#comparison-functions)
2425
- [](#types)
2526
- [数値のマクロ](#value-macros)
@@ -214,6 +215,35 @@ C++03 までの場合、[`errno`](cerrno/errno.md) でしか通知されない
214215
- [`math_errhandling`](cmath/math_errhandling.md) `&` [`MATH_ERRNO`](cmath/math_errno.md)が真で、[`errno`](cerrno/errno.md)が設定された場合
215216

216217

218+
## <a id="nan-handling" href="#nan-handling">NaN の扱い</a>
219+
220+
浮動小数点規格ISO/IEC 60559 (IEEE 754) に準拠する環境では、C言語規格の Annex F (F.10 paragraph 11) により、以下の一般規則が定められている:
221+
222+
> NaN の引数をもつ関数は、NaN の結果を返し、浮動小数点例外を発生させない。ただし、個別に規定がある場合を除く。
223+
224+
この規則は `__STDC_IEC_559__` が定義されている(ISO/IEC 60559 に準拠する)環境で適用される。
225+
226+
また、推奨される実装として、ひとつ以上の NaN 引数をもつ関数が NaN を返す場合、その結果は入力の NaN 引数のいずれかと同じ値(型変換後、符号は異なる可能性がある)であることが望ましいとされている。
227+
228+
### 例外となる関数
229+
230+
以下の関数は、NaN の引数を受け取っても NaN を返さない場合がある:
231+
232+
| 関数 | 条件 | 結果 |
233+
|------|------|------|
234+
| [`pow`](cmath/pow.md) | `pow(1.0, NaN)` | `1.0` |
235+
| [`pow`](cmath/pow.md) | `pow(NaN, ±0.0)` | `1.0` |
236+
| [`hypot`](cmath/hypot.md) | `hypot(±∞, NaN)` | `+∞` |
237+
| [`fmax`](cmath/fmax.md) | 一方のみが NaN | NaN でない方の引数 |
238+
| [`fmin`](cmath/fmin.md) | 一方のみが NaN | NaN でない方の引数 |
239+
240+
[`pow`](cmath/pow.md)[`hypot`](cmath/hypot.md) の例外は、NaN をどのような実数値に置き換えても結果が同じになるという原則に基づいている(任意の実数の 0 乗は 1、任意の実数と無限大の `hypot` は無限大)。
241+
242+
[`fmax`](cmath/fmax.md)[`fmin`](cmath/fmin.md) は、NaN を欠損値として扱い、有効な数値を優先して返す。ただし、両方の引数が NaN の場合は NaN を返す。
243+
244+
なお、数値分類関数([`fpclassify`](cmath/fpclassify.md)[`isnan`](cmath/isnan.md)[`isinf`](cmath/isinf.md) 等)は戻り値が整数型であるため、NaN の伝播規則の対象外である。
245+
246+
217247
## <a id="trigonometric-functions" href="#trigonometric-functions">三角関数</a>
218248

219249
| 名前 | 説明 | 対応バージョン |

0 commit comments

Comments
 (0)