This site is supported by donations to The OEIS Foundation.

# Template:Atan2

(Redirected from Template:Arctan2)

The {{atan2}} circular function template returns the four quadrant arc tangent (a circular angle in ${\displaystyle \scriptstyle (-\pi ,\,+\pi ]\,}$ radians as default, or a circular angle in ${\displaystyle \scriptstyle [0,\,2\pi )\,}$ radians) from ${\displaystyle \scriptstyle y\,}$ and ${\displaystyle \scriptstyle x\,}$, which are real numbers not both zero.

## Usage

{{atan2|y=a real number|x=a real number|cut=an integer}}

or

{{atan2|a real number|a real number|an integer}}

## Valid input

Two real numbers: ${\displaystyle \scriptstyle y\,}$ and ${\displaystyle \scriptstyle x\,}$, not both zero. Third argument (optional) as integer, where an odd integer (default 1) implies a circular angle in ${\displaystyle \scriptstyle (-\pi ,\,+\pi ]\,}$ radians and an even integer implies a circular angle in ${\displaystyle \scriptstyle [0,\,2\pi )\,}$ radians.

## Examples

Examples with valid input

Code Result
{{atan2|y=0|x=1|cut=1}} 0
{{atan2|y={{sin|pi/6}}|x={{cos|pi/6}}}} 0.5235987755983
{{atan2|y={{sin|pi/4}}|x={{cos|pi/4}}}} 0.78539816339745
{{atan2|y={{sin|pi/3}}|x={{cos|pi/3}}}} 1.0471975511966
{{atan2|y=1|x=0|cut=3}} 1.5707963267949
{{atan2|y={{sin|2*pi/3}}|x={{cos|2*pi/3}}}} 2.0943951023932
{{atan2|y={{sin|3*pi/4}}|x={{cos|3*pi/4}}}} 2.3561944901923
{{atan2|y={{sin|5*pi/6}}|x={{cos|5*pi/6}}}} 2.6179938779915
{{atan2|y=0|x=-1|cut=5}} 3.1415926535898
{{atan2|y={{sin|7*pi/6}}|x={{cos|7*pi/6}}}} -2.6179938779915
{{atan2|y={{sin|5*pi/4}}|x={{cos|5*pi/4}}}} -2.3561944901923
{{atan2|y={{sin|4*pi/3}}|x={{cos|4*pi/3}}}} -2.0943951023932
{{atan2|y=-1|x=0|cut=-1}} -1.5707963267949
{{atan2|y={{sin|5*pi/3}}|x={{cos|5*pi/3}}}} -1.0471975511966
{{atan2|y={{sin|7*pi/4}}|x={{cos|7*pi/4}}}} -0.78539816339745
{{atan2|y={{sin|11*pi/6}}|x={{cos|11*pi/6}}}} -0.5235987755983
{{atan2|y={{sin|2*pi}}|x={{cos|2*pi}}}} -2.4492935982947E-16

Code Result
{{atan2|y=0|x=1|cut=0}} 0
{{atan2|y={{sin|pi/6}}|x={{cos|pi/6}}|cut=2}} 0.5235987755983
{{atan2|y={{sin|pi/4}}|x={{cos|pi/4}}|cut=2}} 0.78539816339745
{{atan2|y={{sin|pi/3}}|x={{cos|pi/3}}|cut=2}} 1.0471975511966
{{atan2|y=1|x=0|cut=2}} 1.5707963267949
{{atan2|y={{sin|2*pi/3}}|x={{cos|2*pi/3}}|cut=2}} 2.0943951023932
{{atan2|y={{sin|3*pi/4}}|x={{cos|3*pi/4}}|cut=2}} 2.3561944901923
{{atan2|y={{sin|5*pi/6}}|x={{cos|5*pi/6}}|cut=2}} 2.6179938779915
{{atan2|y=0|x=-1|cut=4}} 3.1415926535898
{{atan2|y={{sin|7*pi/6}}|x={{cos|7*pi/6}}|cut=2}} 3.6651914291881
{{atan2|y={{sin|5*pi/4}}|x={{cos|5*pi/4}}|cut=2}} 3.9269908169872
{{atan2|y={{sin|4*pi/3}}|x={{cos|4*pi/3}}|cut=2}} 4.1887902047864
{{atan2|y=-1|x=0|cut=-2}} 4.7123889803847
{{atan2|{{sin|5*pi/3}}|{{cos|5*pi/3}}|2}} 5.235987755983
{{atan2|{{sin|7*pi/4}}|{{cos|7*pi/4}}|2}} 5.4977871437821
{{atan2|{{sin|11*pi/6}}|{{cos|11*pi/6}}|2}} 5.7595865315813
{{atan2|y={{sin|2*pi}}|x={{cos|2*pi}}|cut=2}} 6.2831853071796

Examples with invalid input (returns an error message)

Code Result Comment
{{atan2}} Atan2 error: Both arguments must be real numbers (not both zero)
{{atan2|i}} Atan2 error: Both arguments must be real numbers (not both zero)
{{atan2|2}} Atan2 error: Both arguments must be real numbers (not both zero)
{{atan2|2|i}} Atan2 error: Both arguments must be real numbers (not both zero)
{{atan2|i|2}} Atan2 error: Both arguments must be real numbers (not both zero)
{{atan2|0|0}} Atan2 error: Result is undefined when both arguments are zero
{{atan2|-1|0|2 pi}} Expression error: Unexpected < operator.
{{atan2|-1|0|6.28}} 4.7123889803847 (parity of cut value rounded towards zero, please use integers for clarity)
{{atan2|-1|0|6.7}} 4.7123889803847 (parity of cut value rounded towards zero, please use integers for clarity)
{{atan2|-1|0|-6.28}} 4.7123889803847 (parity of cut value rounded towards zero, please use integers for clarity)
{{atan2|-1|0|-6.7}} 4.7123889803847 (parity of cut value rounded towards zero, please use integers for clarity)

## Code


<!-- Returns angle in (-pi, +pi] (default) or [0, 2*pi) -->{{ifnum| {{{y|{{{1|NAN}}}}}}{{{x|{{{2|NAN}}}}}}<!-- {{{cut|{{{3|1}}}}}} odd (for 1*pi, default) or even (for 2*pi) -->
| {{#switch: {{#expr: 3*{{sgn| {{{y|{{{1}}}}}} }} + {{sgn| {{{x|{{{2}}}}}} }} }}
| -4 = {{#expr: pi * {{#ifexpr: ({{{cut|{{{3|1}}}}}}) mod 2 = 0 | 1 | -1 }} + atan ( ({{{y|{{{1}}}}}}) / ({{{x|{{{2}}}}}}) ) }} <!--  y < 0 and x < 0 (third quadrant) -->
| -3 = {{#expr: (pi / 2) * {{#ifexpr: ({{{cut|{{{3|1}}}}}}) mod 2 = 0 | 3 | -1 }} }} <!-- y < 0 and x = 0 -->
| -2 = {{#expr: 2*pi * {{#ifexpr: ({{{cut|{{{3|1}}}}}}) mod 2 = 0 | 1 | 0 }} + atan ( ({{{y|{{{1}}}}}}) / ({{{x|{{{2}}}}}}) ) }} <!-- y < 0 and x > 0 (fourth quadrant) -->
| -1 = {{#expr: pi }} <!--                                                              y = 0 and x < 0 -->
|  0 = {{error| Atan2 error: Result is undefined when both arguments are zero }} <!--   y = 0 and x = 0 -->
| +1 = 0 <!--                                                                           y = 0 and x > 0 -->
| +2 = {{#expr: pi + atan ( ({{{y|{{{1}}}}}}) / ({{{x|{{{2}}}}}}) ) }} <!--             y > 0 and x < 0 (second quadrant) -->
| +3 = {{#expr: pi / 2 }} <!--                                                          y > 0 and x = 0 -->
| +4 = {{#expr: atan ( ({{{y|{{{1}}}}}}) / ({{{x|{{{2}}}}}}) ) }} <!--                  y > 0 and x > 0 (first quadrant) -->
}}
| {{error| Atan2 error: Both arguments must be real numbers (not both zero) }}
}}