Re: avoiding non-machine numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg115581] Re: avoiding non-machine numbers
- From: Peter Pein <petsie at dordos.net>
- Date: Sat, 15 Jan 2011 04:46:03 -0500 (EST)
- References: <igpbd4$fae$1@smc.vnet.net>
On 14.01.2011 12:17, wpb wrote: > By default, Mathematica switches to non-machine numbers when needed, > eg, > > In[4334]:= Exp[-50.^2] > > Out[4334]= 1.835672669162*10^-1086 > > But this has a severe possible drawback in terms of computational > speed. For example, the following is very quick > > In[4420]:= t = RandomReal[NormalDistribution[0, .1], 400000]; > Exp[-t^2]; // Timing > > Out[4421]= {0., Null} > > but in the following computation non-machine numbers are generated, > and we get an enormous decrease in speed: > > In[4422]:= t = RandomReal[NormalDistribution[0, 20], 400000]; > Exp[-t^2]; // Timing > > Out[4423]= {1.25, Null} > > So is it possible to get a computation such as Exp[-50.^2] to evaluate > to zero in a very fast way? That is, that small numbers outside > machine-precision range evaluate to zero automatically? > > Thanks, Wicher > -$MachinePrecision Log[10] gives -36.7368. So all values less than this value should be ignored: mExp[x_] := 0. /; x < -36.7368; mExp[x_] := Exp[x]; SetAttributes[mExp, {Listable, NumericFunction}]; t=-RandomReal[NormalDistribution[0, 20], 400000]^2; {t0, r0} = Timing[Exp[t]]; {t1, r1} = Timing[mExp[t]]; Union[r1 - r0 // Chop] 1 - t1 / t0 returns {0} as (chopped) differences and an improvement in speed of ~ 30% Peter