File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -60,7 +60,8 @@ namespace std {
60
60
- `x = -∞` の場合、戻り値は `+∞` となる。
61
61
- `x = +∞` の場合、戻り値は `+∞` となる。
62
62
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
63
-
63
+ - この関数はガンマ関数 ([`tgamma`](tgamma.md)) がオーバーフローするような場合に使う。
64
+ 具体例については[ガンマ関数の備考](tgamma.md#remarks-lgamma)を参照のこと。
64
65
65
66
## 例
66
67
```cpp example
Original file line number Diff line number Diff line change @@ -64,6 +64,32 @@ $$ \Gamma (x) = \int_0^\infty t^{x-1} e^{-t} dt $$
64
64
- `gamma` という関数は既にあったが処理系によって定義が違ったため、本当の (true) ガンマ関数 `tgamma` と名付けられた。
65
65
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
66
66
67
+ ### <a id="remarks-lgamma" href="#remarks-lgamma">lgamma との使い分け</a>
68
+ ガンマ関数は急激に増加し容易にオーバーフローするので、代わりにガンマ関数の結果を自然対数で返す関数 [`lgamma`](lgamma.md) を用いた方が良いことが多くある。
69
+ 例えばガンマ関数の比を計算する場合には、 ガンマ関数の対数の差を取ってから指数関数 [`std::exp`](exp.md) を適用するのが賢明である。
70
+
71
+ $$ \frac{\Gamma(2026)}{\Gamma(2025)} = \exp[\ln\Gamma(2026) - \ln\Gamma(2025)] $$
72
+
73
+ ```cpp example
74
+ #include <cmath>
75
+ #include <iostream>
76
+ int main() {
77
+ std::cout << std::tgamma(2026.0) / std::tgamma(2025.0) << std::endl;
78
+ std::cout << std::exp(std::lgamma(2026.0) - std::lgamma(2025.0)) << std::endl;
79
+ }
80
+ ```
81
+ * std::tgamma[ color ff0000]
82
+ * std::lgamma[ color 0000ff] [ link lgamma.md ]
83
+
84
+ 出力例
85
+ ```
86
+ -nan
87
+ 2025
88
+ ```
89
+
90
+ 上の結果では、直接ガンマ関数を計算した場合はオーバーフローによって inf / inf となり最終結果が -nan になっているが、` lgamma ` を使った場合には正しい値が計算できている。
91
+ ただし、` lgamma ` は飽くまでガンマ関数の「絶対値」の対数であることに注意する。
92
+ ガンマ関数の引数が負になる場合はガンマ関数が負の値を取りうるので符号は別に求める必要がある。
67
93
68
94
## 例
69
95
``` cpp example
You can’t perform that action at this time.
0 commit comments