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