Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can I compile a function that contains ArcTanh[]? The following doesn't work

c = Compile[ {{x}}, ArcTanh[x]^2];

Mathematica gives me

"CompiledFunction::cfn: Numerical error encountered at instruction 1;
 proceeding with uncompiled evaluation>>"

However, in the answer to this question, a reply indicates that ArcTanh can be compiled.

share|improve this question
The values of $\tanh x$ are between -1 and 1 for real arguments. If you wish to work with complex numbers, use the appropriate type annotation in Compile[{{x, _Complex}}, ...]. – Szabolcs Sep 23 '13 at 20:10

The values of $\tanh x$ are between -1 and 1 for real arguments. ArcTanh[x] yields a complex answer if x is not between -1 and 1. Compile does support complex numbers, but you need to specify this using a type annotation:

cf = Compile[{{x, _Complex}}, ArcTanh[x]^2];

(* ==> -2.28792 - 1.33093 I *)

If you allow for complex inputs in cf, it'll be able to return complex results as well.

Side note: you don't need to load CCodeGenerator` to use Compile.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.