名稱空間
變體
操作

std::atan2(std::valarray)

來自 cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
定義於標頭檔案 <valarray>
template< class T >
std::valarray<T> atan2( const std::valarray<T>& y, const std::valarray<T>& x );
(1)
template< class T >

std::valarray<T> atan2( const std::valarray<T>& y,

                        const typename std::valarray<T>::value_type& vx );
(2)
template< class T >

std::valarray<T> atan2( const typename std::valarray<T>::value_type& vy,

                        const std::valarray<T>& x );
(3)

計算 y / x 的反正切,並根據引數符號正確確定象限。

1) 計算 yx 中每對對應值的反正切。

如果 x.size() != y.size(),則行為未定義。

2) 計算 vx 和數字陣列 y 中每個值的反正切。
3) 計算 vy 和數字陣列 x 中每個值的反正切。

目錄

[edit] 引數

x, y - 用於計算反正切的數字陣列
vy, vx - 用於計算反正切的值

[edit] 返回值

一個包含反正切計算結果的數字陣列。

[edit] 注意

使用非限定函式 (atan2) 執行計算。如果該函式不可用,則由於實參依賴查詢,將使用 std::atan2

該函式可以使用與 std::valarray 不同的返回型別實現。在這種情況下,替換型別具有以下屬性:

[edit] 示例

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>
 
void show(char const* title, const std::valarray<double>& va)
{
    std::cout << title << ' ';
    std::for_each(std::begin(va), std::end(va), [](const double x)
    { 
        std::cout << ' ' << std::right << std::setw(4) << x << "°";
    });
    std::cout << '\n';
}
 
const double pi = std::acos(-1.0); // C++20: std::numbers::pi
 
int main()
{
    auto degrees_to_radians = [](double const& x) { return (pi * x / 180); };
    auto radians_to_degrees = [](double const& x) { return (180 * x / pi); };
 
    const std::valarray<double> degrees{-90, -60, -45, -30, 0, 30, 45, 60, 90};
    const std::valarray<double> radians = degrees.apply(degrees_to_radians);
 
    const auto sin = std::sin(radians);
    const auto cos = std::cos(radians);
 
    show("(1)", std::atan2(sin, cos).apply(radians_to_degrees));
    show("(2)", std::atan2(sin/cos, 1.0).apply(radians_to_degrees));
    show("(3)", std::atan2(1.0, cos/sin).apply(radians_to_degrees));
}

輸出

(1)   -90°  -60°  -45°  -30°    0°   30°   45°   60°   90°
(2)   -90°  -60°  -45°  -30°    0°   30°   45°   60°   90°
(3)    90°  120°  135°  150°    0°   30°   45°   60°   90°

[edit] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3074 C++98 對於 (2,3)T 從標量和 valarray 推導,不允許混合型別呼叫 僅從 valarray 推導 T

[edit] 參閱

將函式 std::asin 應用於 valarray 的每個元素
(函式模板) [編輯]
將函式 std::acos 應用於 valarray 的每個元素
(函式模板) [編輯]
將函式 std::atan 應用於 valarray 的每個元素
(函式模板) [編輯]
(C++11)(C++11)
反正切,使用符號確定象限
(函式) [編輯]
返回相角
(函式模板) [編輯]