I use Mathematica mostly for numerical simulation, so most of the time double precision (or machine precision) is enough for me. Mathematica has this nice feature of automatically upgrade to arbitrary precision, but sometimes it costs problems for me.
For instance here is a compiled function that calculate a modulus of a vector
mymod = Compile[{{x, _Real, 1}}, x.x]
Now a vector has a Gaussian distribution
ListPlot[Array[Exp[-(#)^2./10.] &, 500, {-100., 100.}]]
and we want to calculate its modulus
mymod[Array[Exp[-(#)^2./10.]&,500,{-100.,100.}]]
(* 9.8885 *)
we get the right answer but with a warning saying that the argument type is incorrect:
Argument {5.075958897549*10^-435,1.513073408369*10^-431,4.367658965168*10^-428,<<45>>,5.42928*10^-284,3.46203*10^-281,<<450>>} at position 1 should be a rank 1 tensor of machine-size real numbers.
That's because Exp
has upgraded the machine precision number we use to arbitrary precision. And thus the uncompiled function is invoked.
Consider the situation where we use extensively the numerical functions in a fairly large package, this automatic upgrading of precision may occur at multiple places. Although the final answer may probably be correct, invoking the uncompiled functions may greatly degrade the performance.
One solution point out by Daniel Lichtblau here is to turn off the tracking of the underflow by setting "CatchMachineUnderflow"->False
to every function that uses arbitrary precision. But first we need to find those functions.
So my question are:
- Is it possible to tell Mathematica to generate a message when the upgrading to an arbitrary precision occurs? Just like we can have a message if the unpacking of an array occurs.
- This precision problem seems to be quite common for people dealing with numerical calculation in Mathematica. What do you think are good ways to deal with this problem?