How do I manually escape out of a compiled function without forcing an error and compromising its speed?
Here is an example:
f::zero = "x is zero.";
f = Compile[{{x, _Complex}}, If[x == 0, Return[1/x], x^2],
"RuntimeOptions" -> {"RuntimeErrorHandler" ->
Function[Message[f::zero]; Throw[$Failed]]}];
As you can see, I want f
to throw an error when x=0
, but in order to do that, I needed to artificially think there actually was an error by returning 1/x
. Here is the usage:
Catch[f[0]]
CompiledFunction::cfne : Numeriacal Error encountered; proceeding with uncompiled evaluation. >>
f::zero : x is zero
How do I prevent the kernel from proceeding with uncompiled evaluation of the artificial 1/0 like it says (which I believe compromises speed).
How do I escape out of a CompiledFunction without mimicking an error?