I noticed that Exp
have a strange behaviour with big negative numbers
Exp[-Range[1, 700, 0.001]] // Developer`PackedArrayQ // AbsoluteTiming
Exp[-Range[1, 800, 0.001]] // Developer`PackedArrayQ // AbsoluteTiming
{0.014877, True}
{1.551377, False}
I understand than Exp[-800.0]
is less than $MinMachineNumber
, so I expected that the answer will be 0.0, but it is 3.668*10^-348 wich is not a machine number.
MachineNumberQ@Exp[-800.0]
False
What is the best way to work with these numbers? N@Exp
do nothing.
My current solution is
NExp = Exp@Clip[#, {-100.0, 100.0}] &;
NExp[-Range[1, 800, 0.001]] // Developer`PackedArrayQ // AbsoluteTiming
{0.016354, True}
Moreover, I notice that Exp
works fine with complex numbers (only in v9)
Exp[0.0 I - Range[1, 700, 0.001]] // Developer`PackedArrayQ // AbsoluteTiming
Exp[0.0 I - Range[1, 800, 0.001]] // Developer`PackedArrayQ // AbsoluteTiming
{0.049367, True}
{0.066757, True}
Edit:
I saw the answer with suggestion of compilation but for some reason it was removed. It is not very fast solution but it works. So I post it here
NExp = Compile[{{x, _Real}}, Exp[x], RuntimeAttributes -> {Listable}];
NExp[-Range[1, 800, 0.001]]) // Developer`PackedArrayQ // AbsoluteTiming
{0.114772, True}
{0.0080005, True}
and{0.0160009, True}
as the output of the first two lines. Apparently this is version specific; what are you running? $\endgroup$