# Mathematica's ArcTan function [duplicate]

I'm trying to calculate the angle made by a vector from two vector components but am having some trouble. The x-component has a magnitude of -2.7388321862151737*^10 and the y-component has a magnitude of 1.4452659458183087*^11. Now, using the ArcTan function I have:

a = ArcTan[1.4452659458183087*^11/-2.7388321862151737*^10]/Degree


But this gives me an answer of -79.2695 degrees which lies in the 4th Quadrant, and I would have thought that it should have made an angle lying in the 2nd Quadrant (i.e. between 90 and 270 degrees). Any help would be appreciated.

-

## marked as duplicate by Artes, Sjoerd C. de Vries, belisarius has settled, Yves Klett, PickettNov 7 '13 at 13:12

Have you read the ArcTan documentation? – gpap Nov 7 '13 at 11:11
Thanks gpap, I appreciate your help. – user7388 Nov 7 '13 at 11:20
May be this is a silly question but you say that "One line lies along the x-axis.. and the other lies along the y-axis" so aren't they supposed to be perpendicular to each other? Also if theta is the angle between ur two lines then 180-theta would be the supplementary angle. – Hubble07 Nov 7 '13 at 11:47
It's true as Hubble07 says, you're not calculating the angle between the lines, you're calculating the angle between the hypotenuse of the right sided triangle formed by those two catheti. Using the documentation, we can see that it should be written ArcTan[x,y]: ArcTan[-2.7388321862151737*^10, 1.4452659458183087*^11]/Degree which returns 100.731. – Pickett Nov 7 '13 at 12:19
Well then your phrasing of the question is quite wrong, suggest you edit the question to say what you actually mean. – george2079 Nov 7 '13 at 15:19

Another way, not found in nor appropriate to the proposed duplicate, ArcTan[z] and ArcTan[x,y], Restriction and Conversion, is

myArcTan[x_Real] := Piecewise[{{Pi + ArcTan[x], x < 0}}, ArcTan[x]]

myArcTan[1.4452659458183087*^11/-2.7388321862151737*^10]/Degree
(* 100.731 *)


More generally, if you're willing to let the function have arbitrary arguments, we can specify an arbitrary range for the real part of the value of arc tangent. We need to define a custom Mod that will apply only to the real part of the input.

Clear[myArcTan, reMod];
reMod[m_, n_, d_] := Mod[Re@m, n, d] + Im[m] I;
myArcTan::range = "The range  must have length Pi.";
myArcTan[x_, range_List: {-Pi/2, Pi/2}] /;
If[range[[2]] - range[[1]] == Pi, True, Message[myArcTan::range, range]; False] :=
reMod[ArcTan[x], Pi, range[[1]]];


Examples

The standard ArcTan.

myArcTan[1.4452659458183087*^11/-2.7388321862151737*^10]/Degree
(* -79.2695 *)


The OP's desired range {0, Pi}.

myArcTan[1.4452659458183087*^11/-2.7388321862151737*^10, {0, Pi}]/Degree
(* 100.731 *)


Error checking.

myArcTan[1.4452659458183087*^11/-2.7388321862151737*^10, {0, Pi/2}]/Degree


myArcTan::range: The range {0,[Pi]/2} must have length Pi.

(* myArcTan[-5.27694, {0, \[Pi]/2}]/\[Degree] *)


Listability is inherited. Note the discontinuity at 0 if the range is {0, Pi}.

myArcTan[Range[-10., 10, 2], {0, Pi}]/Degree
(* {95.7106, 97.125, 99.4623, 104.036, 116.565,
0., 63.4349, 75.9638, 80.5377, 82.875, 84.2894} *)


Arbitrary expressions. Note that symbolic expressions do not simplify easily.

myArcTan[N@{{1, 2}, 3 I, {{x}}}, {0, Pi}]
(* {{0.785398, 1.10715}, 1.5708 + 0.346574 I,
{{I Im[ArcTan[x]] + Mod[Re[ArcTan[x]], \[Pi], 0]}}} *)

(* Check *)
Tan[%] // Simplify
(* {{1., 2.}, 4.89859*10^-16 + 3. I,
{{I Tanh[Im[ArcTan[x]] - I Mod[Re[ArcTan[x]], \[Pi], 0]]}}} *)

-