std:: atan2 (std::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,
|
(2) | |
|
template
<
class
T
>
std::
valarray
<
T
>
atan2
(
const
typename
std::
valarray
<
T
>
::
value_type
&
vy,
|
(3) | |
计算 y / x 的反正切值,通过参数的符号正确确定象限。
如果 x. size ( ) ! = y. size ( ) ,则行为未定义。
目录 |
参数
| x, y | - | 用于计算反正切值的数值数组 |
| vy, vx | - | 用于计算反正切值的数值 |
返回值
包含反正切计算结果的数值数组。
注释
使用非限定函数( atan2 )执行计算。若该函数不可用,由于 实参依赖查找 机制,将采用 std:: atan2 。
该函数可以实现为返回类型不同于 std::valarray 的情况。此时,替换类型需具备以下特性:
-
- 提供了 const 成员函数的所有 std::valarray 。
- 可以从替换类型构造 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 > & 与替换类型所有组合的相同函数。
- 返回类型不会在最深嵌套的参数类型基础上增加超过两层的模板嵌套。
示例
#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°
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 3074 | C++98 |
T
同时从标量和
valarray
推导
(2,3)
,不允许混合类型调用
|
仅从
valarray
推导
T
|
参见
|
对 valarray 的每个元素应用函数
std::asin
(函数模板) |
|
|
对 valarray 的每个元素应用函数
std::acos
(函数模板) |
|
|
对 valarray 的每个元素应用函数
std::atan
(函数模板) |
|
|
(C++11)
(C++11)
|
使用符号确定象限的反正切函数
(函数) |
|
返回相位角
(函数模板) |