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.
8 // The original C code, the long comment, and the constants
9 // below are from FreeBSD's /usr/src/lib/msun/src/e_exp.c
10 // and came with this notice. The go code is a simplified
11 // version of the original C.
13 // ====================================================
14 // Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
16 // Permission to use, copy, modify, and distribute this
17 // software is freely granted, provided that this notice
19 // ====================================================
23 // Returns the exponential of x.
26 // 1. Argument reduction:
27 // Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
28 // Given x, find r and integer k such that
30 // x = k*ln2 + r, |r| <= 0.5*ln2.
32 // Here r will be represented as r = hi-lo for better
35 // 2. Approximation of exp(r) by a special rational function on
36 // the interval [0,0.34658]:
38 // R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
39 // We use a special Remes algorithm on [0,0.34658] to generate
40 // a polynomial of degree 5 to approximate R. The maximum error
41 // of this polynomial approximation is bounded by 2**-59. In
43 // R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
44 // (where z=r*r, and the values of P1 to P5 are listed below)
47 // | 2.0+P1*z+...+P5*z - R(z) | <= 2
49 // The computation of exp(r) thus becomes
51 // exp(r) = 1 + -------
54 // = 1 + r + ----------- (for better accuracy)
58 // R1(r) = r - (P1*r + P2*r + ... + P5*r ).
60 // 3. Scale back to obtain exp(x):
61 // From step 1, we have
62 // exp(x) = 2**k * exp(r)
65 // exp(INF) is INF, exp(NaN) is NaN;
66 // exp(-INF) is 0, and
67 // for finite argument, only exp(0)=1 is exact.
70 // according to an error analysis, the error is always less than
71 // 1 ulp (unit in the last place).
75 // if x > 7.09782712893383973096e+02 then exp(x) overflow
76 // if x < -7.45133219101941108420e+02 then exp(x) underflow
79 // The hexadecimal values are the intended ones for the following
80 // constants. The decimal values may be used, provided that the
81 // compiler will convert from decimal to binary accurately enough
82 // to produce the hexadecimal values shown.
84 // Exp returns e**x, the base-e exponential of x.
89 // Very large values overflow to 0 or +Inf.
90 // Very small values underflow to 1.
91 func Exp(x float64) float64 {
93 Ln2Hi = 6.93147180369123816490e-01
94 Ln2Lo = 1.90821492927058770002e-10
95 Log2e = 1.44269504088896338700e+00
96 P1 = 1.66666666666666019037e-01 /* 0x3FC55555; 0x5555553E */
97 P2 = -2.77777777770155933842e-03 /* 0xBF66C16C; 0x16BEBD93 */
98 P3 = 6.61375632143793436117e-05 /* 0x3F11566A; 0xAF25DE2C */
99 P4 = -1.65339022054652515390e-06 /* 0xBEBBBD41; 0xC5D26BF1 */
100 P5 = 4.13813679705723846039e-08 /* 0x3E663769; 0x72BEA4D0 */
102 Overflow = 7.09782712893383973096e+02
103 Underflow = -7.45133219101941108420e+02
104 NearZero = 1.0 / (1 << 28) // 2**-28
107 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
108 // when compiler does it for us
111 case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1):
113 case x < -MaxFloat64: // IsInf(x, -1):
119 case -NearZero < x && x < NearZero:
123 // reduce; computed as r = hi - lo for extra precision.
127 k = int(Log2e*x - 0.5)
129 k = int(Log2e*x + 0.5)
131 hi := x - float64(k)*Ln2Hi
132 lo := float64(k) * Ln2Lo
137 c := r - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))))
138 y := 1 - ((lo - (r*c)/(2-c)) - hi)
139 // TODO(rsc): make sure Ldexp can handle boundary k