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.

  • 2
    $\begingroup$ 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}}, ...]. $\endgroup$
    – Szabolcs
    Commented Sep 23, 2013 at 20:10

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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