1
0
mirror of https://github.com/golang/go synced 2024-10-04 11:21:21 -06:00
go/src/lib/math/exp.go

48 lines
1.0 KiB
Go
Raw Normal View History

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.
package math
2008-03-28 14:56:47 -06:00
import "math"
2008-03-28 14:56:47 -06:00
/*
2008-07-08 21:48:41 -06:00
* exp returns the exponential func of its
* floating-point argument.
*
* The coefficients are #1069 from Hart and Cheney. (22.35D)
*/
2008-03-28 14:56:47 -06:00
const
(
p0 = .2080384346694663001443843411e7;
p1 = .3028697169744036299076048876e5;
p2 = .6061485330061080841615584556e2;
q0 = .6002720360238832528230907598e7;
q1 = .3277251518082914423057964422e6;
q2 = .1749287689093076403844945335e4;
log2e = .14426950408889634073599247e1;
sqrt2 = .14142135623730950488016887e1;
maxf = 10000;
)
export func Exp(arg float64) float64 {
if arg == 0. {
2008-03-28 14:56:47 -06:00
return 1;
}
if arg < -maxf {
2008-07-08 21:48:41 -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;
ent := int(Floor(x));
fract := (x-float64(ent)) - 0.5;
xsq := fract*fract;
temp1 := ((p2*xsq+p1)*xsq+p0)*fract;
temp2 := ((xsq+q2)*xsq+q1)*xsq + q0;
return sys.ldexp(sqrt2*(temp2+temp1)/(temp2-temp1), ent);
2008-03-28 14:56:47 -06:00
}