當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ tanh()用法及代碼示例


tanh()是C++ STL中的內置函數,可返回以弧度表示的角度的雙曲線正切值。

用法:

tanh(data_type x)

參數:該函數接受一個強製性參數x,該參數指定弧度中的雙曲角。該參數可以是double,float或long double數據類型。


返回值:該函數返回參數的雙曲正切值。

以下示例程序旨在說明上述方法:

示例1:

// CPP program to demonstrate the 
// tanh() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    double x = 4.1, result; 
  
    result = tanh(x); 
    cout << "tanh(4.1) = " << result << endl; 
  
    // x in Degrees 
    double xDegrees = 90; 
    x = xDegrees * 3.14159 / 180; 
  
    result = tanh(x); 
    cout << "tanh(90 degrees) = " << result << endl; 
  
    return 0; 
}
輸出:
tanh(4.1) = 0.999451
tanh(90 degrees) = 0.917152

示例2:

// CPP program to demonstrate the 
// tanh() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int x = -4; 
    double result; 
  
    result = tanh(x); 
    cout << "tanh(-4) = " << result << endl; 
  
    // x in Degrees 
    double xDegrees = 90; 
    x = xDegrees * 3.14159 / 180; 
  
    result = tanh(x); 
    cout << "tanh(90 degrees) = " << result << endl; 
  
    return 0; 
}
輸出:
tanh(-4) = -0.999329
tanh(90 degrees) = 0.761594

錯誤和異常:當字符串或字符作為參數傳遞時,該函數不返回任何匹配函數來調用錯誤。

示例3:

// CPP program to demonstrate the tanh() 
// function when a string is passed as argument  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string x = "gfg"; 
    double result; 
  
    result = tanh(x); 
    cout << "tanh(x) = " << result << endl; 
  
    return 0; 
}

輸出:

prog.cpp:14:20: error: no matching function for call to 'tanh(std::__cxx11::string&)'
result = tanh(x);


相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 tanh() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。