1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package math
6
7// Exp returns e**x, the base-e exponential of x.
8//
9// Special cases are:
10//	Exp(+Inf) = +Inf
11//	Exp(NaN) = NaN
12// Very large values overflow to 0 or +Inf.
13// Very small values underflow to 1.
14
15//extern exp
16func libc_exp(float64) float64
17
18func Exp(x float64) float64 {
19	return libc_exp(x)
20}
21
22// The original C code, the long comment, and the constants
23// below are from FreeBSD's /usr/src/lib/msun/src/e_exp.c
24// and came with this notice. The go code is a simplified
25// version of the original C.
26//
27// ====================================================
28// Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
29//
30// Permission to use, copy, modify, and distribute this
31// software is freely granted, provided that this notice
32// is preserved.
33// ====================================================
34//
35//
36// exp(x)
37// Returns the exponential of x.
38//
39// Method
40//   1. Argument reduction:
41//      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
42//      Given x, find r and integer k such that
43//
44//               x = k*ln2 + r,  |r| <= 0.5*ln2.
45//
46//      Here r will be represented as r = hi-lo for better
47//      accuracy.
48//
49//   2. Approximation of exp(r) by a special rational function on
50//      the interval [0,0.34658]:
51//      Write
52//          R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
53//      We use a special Remez algorithm on [0,0.34658] to generate
54//      a polynomial of degree 5 to approximate R. The maximum error
55//      of this polynomial approximation is bounded by 2**-59. In
56//      other words,
57//          R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
58//      (where z=r*r, and the values of P1 to P5 are listed below)
59//      and
60//          |                  5          |     -59
61//          | 2.0+P1*z+...+P5*z   -  R(z) | <= 2
62//          |                             |
63//      The computation of exp(r) thus becomes
64//                             2*r
65//              exp(r) = 1 + -------
66//                            R - r
67//                                 r*R1(r)
68//                     = 1 + r + ----------- (for better accuracy)
69//                                2 - R1(r)
70//      where
71//                               2       4             10
72//              R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
73//
74//   3. Scale back to obtain exp(x):
75//      From step 1, we have
76//         exp(x) = 2**k * exp(r)
77//
78// Special cases:
79//      exp(INF) is INF, exp(NaN) is NaN;
80//      exp(-INF) is 0, and
81//      for finite argument, only exp(0)=1 is exact.
82//
83// Accuracy:
84//      according to an error analysis, the error is always less than
85//      1 ulp (unit in the last place).
86//
87// Misc. info.
88//      For IEEE double
89//          if x >  7.09782712893383973096e+02 then exp(x) overflow
90//          if x < -7.45133219101941108420e+02 then exp(x) underflow
91//
92// Constants:
93// The hexadecimal values are the intended ones for the following
94// constants. The decimal values may be used, provided that the
95// compiler will convert from decimal to binary accurately enough
96// to produce the hexadecimal values shown.
97
98func exp(x float64) float64 {
99	const (
100		Ln2Hi = 6.93147180369123816490e-01
101		Ln2Lo = 1.90821492927058770002e-10
102		Log2e = 1.44269504088896338700e+00
103
104		Overflow  = 7.09782712893383973096e+02
105		Underflow = -7.45133219101941108420e+02
106		NearZero  = 1.0 / (1 << 28) // 2**-28
107	)
108
109	// special cases
110	switch {
111	case IsNaN(x) || IsInf(x, 1):
112		return x
113	case IsInf(x, -1):
114		return 0
115	case x > Overflow:
116		return Inf(1)
117	case x < Underflow:
118		return 0
119	case -NearZero < x && x < NearZero:
120		return 1 + x
121	}
122
123	// reduce; computed as r = hi - lo for extra precision.
124	var k int
125	switch {
126	case x < 0:
127		k = int(Log2e*x - 0.5)
128	case x > 0:
129		k = int(Log2e*x + 0.5)
130	}
131	hi := x - float64(k)*Ln2Hi
132	lo := float64(k) * Ln2Lo
133
134	// compute
135	return expmulti(hi, lo, k)
136}
137
138// Exp2 returns 2**x, the base-2 exponential of x.
139//
140// Special cases are the same as Exp.
141func Exp2(x float64) float64 {
142	return exp2(x)
143}
144
145func exp2(x float64) float64 {
146	const (
147		Ln2Hi = 6.93147180369123816490e-01
148		Ln2Lo = 1.90821492927058770002e-10
149
150		Overflow  = 1.0239999999999999e+03
151		Underflow = -1.0740e+03
152	)
153
154	// special cases
155	switch {
156	case IsNaN(x) || IsInf(x, 1):
157		return x
158	case IsInf(x, -1):
159		return 0
160	case x > Overflow:
161		return Inf(1)
162	case x < Underflow:
163		return 0
164	}
165
166	// argument reduction; x = r×lg(e) + k with |r| ≤ ln(2)/2.
167	// computed as r = hi - lo for extra precision.
168	var k int
169	switch {
170	case x > 0:
171		k = int(x + 0.5)
172	case x < 0:
173		k = int(x - 0.5)
174	}
175	t := x - float64(k)
176	hi := t * Ln2Hi
177	lo := -t * Ln2Lo
178
179	// compute
180	return expmulti(hi, lo, k)
181}
182
183// exp1 returns e**r × 2**k where r = hi - lo and |r| ≤ ln(2)/2.
184func expmulti(hi, lo float64, k int) float64 {
185	const (
186		P1 = 1.66666666666666657415e-01  /* 0x3FC55555; 0x55555555 */
187		P2 = -2.77777777770155933842e-03 /* 0xBF66C16C; 0x16BEBD93 */
188		P3 = 6.61375632143793436117e-05  /* 0x3F11566A; 0xAF25DE2C */
189		P4 = -1.65339022054652515390e-06 /* 0xBEBBBD41; 0xC5D26BF1 */
190		P5 = 4.13813679705723846039e-08  /* 0x3E663769; 0x72BEA4D0 */
191	)
192
193	r := hi - lo
194	t := r * r
195	c := r - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))))
196	y := 1 - ((lo - (r*c)/(2-c)) - hi)
197	// TODO(rsc): make sure Ldexp can handle boundary k
198	return Ldexp(y, k)
199}
200