2008-03-28 14:56:47 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
package math
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
import math "floor"
|
2008-03-28 14:56:47 -06:00
|
|
|
export exp
|
|
|
|
|
|
|
|
/*
|
|
|
|
exp returns the exponential func of its
|
|
|
|
floating-point argument.
|
|
|
|
|
|
|
|
The coefficients are #1069 from Hart and Cheney. (22.35D)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const
|
|
|
|
(
|
|
|
|
p0 = .2080384346694663001443843411e7;
|
|
|
|
p1 = .3028697169744036299076048876e5;
|
|
|
|
p2 = .6061485330061080841615584556e2;
|
|
|
|
q0 = .6002720360238832528230907598e7;
|
|
|
|
q1 = .3277251518082914423057964422e6;
|
|
|
|
q2 = .1749287689093076403844945335e4;
|
|
|
|
log2e = .14426950408889634073599247e1;
|
|
|
|
sqrt2 = .14142135623730950488016887e1;
|
|
|
|
maxf = 10000;
|
|
|
|
)
|
|
|
|
|
|
|
|
func
|
|
|
|
exp(arg double) double
|
|
|
|
{
|
|
|
|
var x, fract, temp1, temp2, xsq double;
|
|
|
|
var ent int;
|
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
if arg == 0. {
|
2008-03-28 14:56:47 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if arg < -maxf {
|
2008-06-27 18:06:23 -06:00
|
|
|
return 0.;
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
if arg > maxf {
|
2008-07-07 15:07:46 -06:00
|
|
|
return sys.Inf(1)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
x = arg*log2e;
|
2008-06-27 18:06:23 -06:00
|
|
|
ent = int(floor(x));
|
2008-03-28 14:56:47 -06:00
|
|
|
fract = (x-double(ent)) - 0.5;
|
|
|
|
xsq = fract*fract;
|
|
|
|
temp1 = ((p2*xsq+p1)*xsq+p0)*fract;
|
|
|
|
temp2 = ((xsq+q2)*xsq+q1)*xsq + q0;
|
2008-07-08 18:26:10 -06:00
|
|
|
return sys.ldexp(sqrt2*(temp2+temp1)/(temp2-temp1), ent);
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|