名前空間
変種
操作

std::atan2, std::atan2f, std::atan2l

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
ヘッダー <cmath> で定義
(1)
float       atan2 ( float y, float x );

double      atan2 ( double y, double x );

long double atan2 ( long double y, long double x );
(C++23まで)
/*浮動小数点数型*/

            atan2 ( /*浮動小数点型*/ y,

                    /*浮動小数点型*/ x );
(C++23から)
(C++26 以降 constexpr)
float       atan2f( float y, float x );
(2) (C++11以降)
(C++26 以降 constexpr)
long double atan2l( long double y, long double x );
(3) (C++11以降)
(C++26 以降 constexpr)
ヘッダー <simd> で定義
template< class V0, class V1 >

constexpr /*math-common-simd-t*/<V0, V1>

            atan2 ( const V0& v_y, const V1& v_x );
(S) (C++26以降)
ヘッダー <cmath> で定義
template< class Integer >
double      atan2 ( Integer y, Integer x );
(A) (C++26 以降 constexpr)
1-3) 引数の符号を使用して正しい象限を決定し、y / x のアークタンジェントを計算します。ライブラリは、パラメータの型としてすべての cv 修飾されていない浮動小数点型に対する std::atan2 のオーバーロードを提供します。(since C++23)
S) SIMD オーバーロードは、v_y および v_x に対して要素ごとの std::atan2 を実行します。
(定義についてはmath-common-simd-tを参照のこと)
(C++26以降)
A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
(C++11以降)

目次

[編集] パラメータ

y, x - 浮動小数点または整数値

[編集] 戻り値

エラーが発生しなかった場合、y / x のアークタンジェント(arctan(
y
x
)
)が、範囲 [-π, +π] ラジアンで返されます。
y 引数
戻り値
math-atan2.png
x 引数

領域エラーが発生した場合、実装定義の値が返される (サポートされている場合はNaN)。

アンダーフローによる範囲エラーが発生した場合、正確な結果 (丸め後) が返される。

[編集] エラー処理

エラーは math_errhandling で指定された通りに報告される。

xy が両方ともゼロの場合、ドメインエラーが発生する可能性があります。

実装がIEEE浮動小数点算術 (IEC 60559) をサポートしている場合、

  • xy が両方ともゼロの場合、ドメインエラーは発生しません。
  • xy が両方ともゼロの場合、範囲エラーも発生しません。
  • y がゼロの場合、極エラーは発生しません。
  • y が ±0 で x が負または -0 の場合、±π が返されます。
  • y が ±0 で x が正または +0 の場合、±0 が返されます。
  • y が ±∞ で x が有限の場合、±π/2 が返されます。
  • y が ±∞ で x が -∞ の場合、±3π/4 が返されます。
  • y が ±∞ で x が +∞ の場合、±π/4 が返されます。
  • x が ±0 で y が負の場合、-π/2 が返されます。
  • x が ±0 で y が正の場合、+π/2 が返されます。
  • x が -∞ で y が有限で正の場合、+π が返されます。
  • x が -∞ で y が有限で負の場合、-π が返されます。
  • x が +∞ で y が有限で正の場合、+0 が返されます。
  • x が +∞ で y が有限で負の場合、-0 が返されます。
  • x または y が NaN の場合、NaN が返されます。

[編集] 注釈

std::atan2(y, x)std::arg(std::complex<std::common_type_t<decltype(x), decltype(y)>>(x, y)) と同等です。

POSIX は、アンダーフローの場合、値 y / x が返され、それがサポートされない場合は、DBL_MINFLT_MIN、および LDBL_MIN 以下の実装定義値が返されることを規定しています。

追加のオーバーロードは (A) とまったく同じように提供される必要はない。それらは、最初の引数 num1 と2番目の引数 num2 に対して以下を保証するのに十分である必要がある。

  • num1 または num2 の型が long double の場合、std::atan2(num1, num2)std::atan2(static_cast<long double>(num1),
               static_cast<long double>(num2))
    と同じ効果を持ちます。
  • それ以外の場合、num1 および/または num2 の型が double または整数型の場合、std::atan2(num1, num2)std::atan2(static_cast<double>(num1),
               static_cast<double>(num2))
    と同じ効果を持ちます。
  • それ以外の場合、num1 または num2 の型が float の場合、std::atan2(num1, num2)std::atan2(static_cast<float>(num1),
               static_cast<float>(num2))
    と同じ効果を持ちます。
(C++23まで)

num1num2 が算術型の場合、std::atan2(num1, num2)std::atan2(static_cast</*共通浮動小数点型*/>(num1),
           static_cast</*共通浮動小数点型*/>(num2))
と同じ効果を持ちます。ここで、/*共通浮動小数点型*/ は、num1num2 の型の間で、浮動小数点変換ランクが最も高く、浮動小数点変換サブランクが最も高い浮動小数点型です。整数型の引数は、double と同じ浮動小数点変換ランクを持つとみなされます。

そのような最高のランクとサブランクセを持つ浮動小数点型が存在しない場合、オーバーロード解決は提供されたオーバーロードから使用可能な候補を導出しません。

(C++23から)

[編集]

#include <cmath>
#include <iostream>
 
void print_coordinates(int x, int y)
{
    std::cout << std::showpos
              << "(x:" << x << ", y:" << y << ") cartesian is "
              << "(r:" << std::hypot(x, y)
              << ", phi:" << std::atan2(y, x) << ") polar\n";
}
 
int main()
{
    // normal usage: the signs of the two arguments determine the quadrant
    print_coordinates(+1, +1); // atan2( 1,  1) =  +pi/4, Quad I
    print_coordinates(-1, +1); // atan2( 1, -1) = +3pi/4, Quad II
    print_coordinates(-1, -1); // atan2(-1, -1) = -3pi/4, Quad III
    print_coordinates(+1, -1); // atan2(-1,  1) =  -pi/4, Quad IV
 
    // special values
    std::cout << std::noshowpos
              << "atan2(0, 0) = " << atan2(0, 0) << '\n'
              << "atan2(0,-0) = " << atan2(0, -0.0) << '\n'
              << "atan2(7, 0) = " << atan2(7, 0) << '\n'
              << "atan2(7,-0) = " << atan2(7, -0.0) << '\n';
}

出力

(x:+1, y:+1) cartesian is (r:1.41421, phi:0.785398) polar
(x:-1, y:+1) cartesian is (r:1.41421, phi:2.35619) polar
(x:-1, y:-1) cartesian is (r:1.41421, phi:-2.35619) polar
(x:+1, y:-1) cartesian is (r:1.41421, phi:-0.785398) polar
atan2(0, 0) = 0
atan2(0,-0) = 3.14159
atan2(7, 0) = 1.5708
atan2(7,-0) = 1.5708

[編集] 関連項目

(C++11)(C++11)
逆正弦を計算する (arcsin(x))
(関数) [編集]
(C++11)(C++11)
逆余弦を計算する (arccos(x))
(関数) [編集]
(C++11)(C++11)
逆正接を計算する (arctan(x))
(関数) [編集]
偏角を返す
(関数テンプレート) [編集]
valarray と値に std::atan2 関数を適用します。
(function template) [編集]
English 日本語 中文(简体) 中文(繁體)