std::cos(std::valarray)
來自 cppreference.com
定義於標頭檔案 <valarray> |
||
template< class T > valarray<T> cos( const valarray<T>& va ); |
||
對於 va 中的每個元素,計算該值的餘弦。
目錄 |
[編輯] 引數
va | - | 要應用操作的值陣列 |
[編輯] 返回值
包含 va 中所有值的餘弦的陣列。
[編輯] 注意
使用非限定函式(cos)執行計算。如果此類函式不可用,則由於實參依賴查詢,將使用 std::cos。
該函式可以使用與 std::valarray 不同的返回型別實現。在這種情況下,替換型別具有以下屬性:
- 提供 std::valarray 的所有 const 成員函式。
- std::valarray、std::slice_array、std::gslice_array、std::mask_array 和 std::indirect_array 可以從替換型別構造。
- 對於每個接受 const std::valarray<T>& (除了 begin() 和 end() 之外)(C++11 起) 的函式,應新增接受替換型別的相同函式;
- 對於每個接受兩個 const std::valarray<T>& 引數的函式,應新增接受 const std::valarray<T>& 和替換型別所有組合的相同函式。
- 返回型別不會在最深層巢狀的引數型別之上增加超過兩個模板巢狀級別。
[編輯] 可能的實現
template<class T> valarray<T> cos(const valarray<T>& va) { valarray<T> other = va; for (T& i : other) i = cos(i); return other; // proxy object may be returned } |
[編輯] 示例
執行此程式碼
#include <cmath> #include <iomanip> #include <iostream> #include <valarray> void show(const char* title, const std::valarray<float>& data) { const int w{9}; std::cout << std::setw(w) << title << " | "; for (float x : data) std::cout << std::setw(w) << x << " | "; std::cout << '\n'; } int main() { const std::valarray<float> x{.1, .2, .3, .4}; const auto sin = std::sin(x); const auto cos = std::cos(x); const auto z = (sin * sin) + (cos * cos); show("x", x); show("sin(x)", sin); show("cos(x)", cos); show("z", z); }
輸出
x | 0.1 | 0.2 | 0.3 | 0.4 | sin(x) | 0.0998334 | 0.198669 | 0.29552 | 0.389418 | cos(x) | 0.995004 | 0.980067 | 0.955337 | 0.921061 | z | 1 | 1 | 1 | 1 |
[編輯] 參閱
將函式 std::sin 應用於 valarray 的每個元素 (函式模板) | |
將函式 std::tan 應用於 valarray 的每個元素 (函式模板) | |
將函式 std::acos 應用於 valarray 的每個元素 (函式模板) | |
(C++11)(C++11) |
計算餘弦(cos(x)) (函式) |
計算複數的餘弦 (cos(z)) (函式模板) |