C++ Valarray::cosh 函式



C++ Valarray::cosh() 函式生成一個包含所有元素雙曲餘弦的 valarray,用於計算每個元素值的雙曲餘弦。cmath 中的 cosh() 方法被此函式過載,它會為每個元素呼叫一次該方法。

語法

以下是 C++ Valarray::cosh 函式的語法:

cosh (const valarray<T>& x);

引數

x − 包含元素的型別,對此型別定義了一元函式 cosh。

示例

示例 1

讓我們來看下面的例子,我們將使用 cosh() 函式並檢索輸出。

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   valarray<double>
   valarray0 = { 0.15, 0.3, 0.65, 0.89, 0.4 };
   valarray<double> valarray1;
   valarray1 = cosh(valarray0);
   cout << "The New cosh"
      << " Valarray is : "
      << endl;
   for (double& x : valarray1) {
      cout << x << " ";
   }
   cout << endl;
   return 0;
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The New cosh Valarray is : 
1.01127 1.04534 1.21879 1.42289 1.08107

示例 2

讓我們考慮下面的例子,我們將使用 cosh() 函式並執行算術運算並檢索輸出。

#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>

void show(const char* title, const std::valarray<float>& data) {
   const int w { 8 };
   std::cout << std::setw(w) << title << " | ";
   for (float a : data)
      std::cout << std::setw(w) << a << " | ";
   std::cout << '\n';
}
int main() {
   const std::valarray<float> a { .23, .25, .36, .45 };
   const auto sinh = std::sinh(a);
   const auto cosh = std::cosh(a);
   const auto Multiply = (cosh * sinh);
   show("sinh(a)", sinh);
   show("cosh(a)", cosh);
   show("Result(cosh * sinh)", Multiply   );
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

sinh(a)             | 0.232033 | 0.252612 | 0.367827 | 0.465342 | 
cosh(a)             |  1.02657 |  1.03141 |   1.0655 |  1.10297 | 
Result(cosh * sinh) | 0.238198 | 0.260548 |  0.39192 | 0.513258 |  

示例 3

在下面的例子中,我們將使用 cos() 函式並檢索原始 valarray 和 cos() valarray 的輸出。

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   valarray<double> myvalarr = { 0.2, -0.3, -0.4, 0.5, -0.1 };
   cout << "The Orignal Valarray is : ";
   
   for (double& ele : myvalarr)
      cout << ele << " ";
   valarray<double> coshvalarray = cosh(myvalarr);
   cout << "\nThe cosh Valarray is : ";
   
   for (double& ele : coshvalarray)
      cout << ele << " ";
   return 0;
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The Orignal Valarray is : 0.2 -0.3 -0.4 0.5 -0.1 
The cosh Valarray is : 1.02007 1.04534 1.08107 1.12763 1.005
廣告