# ArcTan and Compile

See following three snippet code:

(*can compile*)
range = Range[-2, 2, 0.005];
Compile[{},
With[{r = range}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];

(*can compile*)
Compile[{},
With[{r = Range[-2, 2, 0.005 - 10^-8]}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];

(*can't compile*)
Compile[{},
With[{r = Range[-2, 2, 0.005]}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];


Why the last code can't be compiled? I used Mathematica 9.0.1.

-
Your first "can compiled" doesn't compile in V.9.0.1. Gives the errors: "CompiledFunction::cfn: Numerical error encountered at instruction 12; proceeding with uncompiled evaluation. >>" and "ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>". – m_goldberg Sep 6 '13 at 17:54

In addition to Mr. Wizard's analysis, one can also avoid the indeterminacy by replacing ArcTan as follows:

Compile[{},
With[{r = Range[-2, 2, 0.005]},
Table[Arg[Complex[x, y]], {x, r}, {y, r}]]][];


The fact that ArcTan[0,0] is undefined is a real nuisance, and I never saw the point of it because that form of the function is mainly used for practical applications such as plotting, where the purely mathematical reasons against defining that special value don't really bother anyone.

-
I didn't know of this workaround. Nice. +1 – Mr.Wizard Sep 6 '13 at 17:53
@AndreasLauschke Could you explain what the links above are intended for? – Jens Sep 6 '13 at 18:43
@AndreasLauschke I'm guessing you want to confirm the equivalence of the Arg and ArcTan methods. But the links don't seem to address the discrepancy between the definitions at the origin. – Jens Sep 6 '13 at 18:51
@Jens: indeed, just the equivalence of the two functions. I'd be glad to remove my comments if you think it's more misleading than helpful. – Andreas Lauschke Sep 6 '13 at 19:01

The warning messages tell you:

CompiledFunction::cfn: Numerical error encountered at instruction 33; proceeding with uncompiled evaluation. >>

ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>

By using an offset of 10^-8 you avoid having exactly zero in the list, and avoid the error.

The first version does not compile in version 7 so I can't tell you about that one.

-