std::tanh, std::tanhf, std::tanhl
來自 cppreference.com
定義於標頭檔案 <cmath> |
||
(1) | ||
float tanh ( float num ); double tanh ( double num ); |
(直至 C++23) | |
/*floating-point-type*/ tanh ( /*floating-point-type*/ num ); |
(C++23 起) (C++26 起為 constexpr) |
|
float tanhf( float num ); |
(2) | (C++11 起) (C++26 起為 constexpr) |
long double tanhl( long double num ); |
(3) | (C++11 起) (C++26 起為 constexpr) |
SIMD 過載 (C++26 起) |
||
定義於標頭檔案 <simd> |
||
template< /*數學浮點型別*/ V > constexpr /*推導 SIMD 型別*/<V> |
(S) | (C++26 起) |
額外過載 (自 C++11 起) |
||
定義於標頭檔案 <cmath> |
||
template< class Integer > double tanh ( Integer num ); |
(A) | (C++26 起為 constexpr) |
1-3) 計算 num 的雙曲正切。 庫為所有 cv 非限定浮點型別的引數提供
std::tanh
的過載。(C++23 起)
S) SIMD 過載對 v_num 執行元素級別的
std::tanh 。
|
(C++26 起) |
A) 為所有整數型別提供了額外的過載,它們被視為 double。
|
(C++11 起) |
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
如果沒有發生錯誤,返回 num 的雙曲正切值(tanh(num),或enum -e-num |
enum +e-num |
如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。
[編輯] 錯誤處理
錯誤按 math_errhandling 指定的方式報告。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 如果引數是 ±0,則返回 ±0。
- 如果引數是 ±∞,則返回 ±1。
- 如果引數為 NaN,則返回 NaN。
[編輯] 注意
POSIX 指定,在下溢的情況下,num 被原樣返回;如果不支援,則返回不大於 DBL_MIN、FLT_MIN 和 LDBL_MIN 的實現定義值。
不要求提供的額外過載完全如同 (A)。它們只需足以確保對於整數型別的引數 num,std::tanh(num) 具有與 std::tanh(static_cast<double>(num)) 相同的效果。
[編輯] 示例
執行此程式碼
#include <cmath> #include <iostream> #include <random> double get_random_between(double min, double max) { std::random_device rd; std::mt19937 gen(rd()); return std::uniform_real_distribution<>(min, max)(gen); } int main() { const double x = get_random_between(-1.0, 1.0); std::cout << std::showpos << "tanh(+1) = " << std::tanh(+1) << '\n' << "tanh(-1) = " << std::tanh(-1) << '\n' << "tanh(x)*sinh(2*x)-cosh(2*x) = " << std::tanh(x) * std::sinh(2 * x) - std::cosh(2 * x) << '\n' // special values: << "tanh(+0) = " << std::tanh(+0.0) << '\n' << "tanh(-0) = " << std::tanh(-0.0) << '\n'; }
輸出
tanh(+1) = +0.761594 tanh(-1) = -0.761594 tanh(x)*sinh(2*x)-cosh(2*x) = -1 tanh(+0) = +0 tanh(-0) = -0
[編輯] 參閱
(C++11)(C++11) |
計算雙曲正弦(sinh(x)) (函式) |
(C++11)(C++11) |
計算雙曲餘弦(cosh(x)) (函式) |
(C++11)(C++11)(C++11) |
計算反雙曲正切(artanh(x)) (函式) |
計算複數的雙曲正切 (tanh(z)) (函式模板) | |
將 std::tanh 函式應用於 valarray 的每個元素 (函式模板) | |
C 文件 關於 tanh
|