gitee.com/quant1x/num@v0.3.2/math32/log.go (about) 1 package math32 2 3 /* 4 Floating-point logarithm. 5 */ 6 7 // The original C code, the long comment, and the constants 8 // below are from FreeBSD's /usr/src/lib/msun/src/e_log.c 9 // and came with this notice. The go code is a simpler 10 // version of the original C. 11 // 12 // ==================================================== 13 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 14 // 15 // Developed at SunPro, a Sun Microsystems, Inc. business. 16 // Permission to use, copy, modify, and distribute this 17 // software is freely granted, provided that this notice 18 // is preserved. 19 // ==================================================== 20 // 21 // __ieee754_log(x) 22 // Return the logarithm of x 23 // 24 // Method : 25 // 1. Argument Reduction: find k and f such that 26 // x = 2**k * (1+f), 27 // where sqrt(2)/2 < 1+f < sqrt(2) . 28 // 29 // 2. Approximation of log(1+f). 30 // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 31 // = 2s + 2/3 s**3 + 2/5 s**5 + ....., 32 // = 2s + s*R 33 // We use a special Reme algorithm on [0,0.1716] to generate 34 // a polynomial of degree 14 to approximate R. The maximum error 35 // of this polynomial approximation is bounded by 2**-58.45. In 36 // other words, 37 // 2 4 6 8 10 12 14 38 // R(z) ~ L1*s +L2*s +L3*s +L4*s +L5*s +L6*s +L7*s 39 // (the values of L1 to L7 are listed in the program) and 40 // | 2 14 | -58.45 41 // | L1*s +...+L7*s - R(z) | <= 2 42 // | | 43 // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 44 // In order to guarantee error in log below 1ulp, we compute log by 45 // log(1+f) = f - s*(f - R) (if f is not too large) 46 // log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) 47 // 48 // 3. Finally, log(x) = k*Ln2 + log(1+f). 49 // = k*Ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*Ln2_lo))) 50 // Here Ln2 is split into two floating point number: 51 // Ln2_hi + Ln2_lo, 52 // where n*Ln2_hi is always exact for |n| < 2000. 53 // 54 // Special cases: 55 // log(x) is NaN with signal if x < 0 (including -INF) ; 56 // log(+INF) is +INF; log(0) is -INF with signal; 57 // log(NaN) is that NaN with no signal. 58 // 59 // Accuracy: 60 // according to an error analysis, the error is always less than 61 // 1 ulp (unit in the last place). 62 // 63 // Constants: 64 // The hexadecimal values are the intended ones for the following 65 // constants. The decimal values may be used, provided that the 66 // compiler will convert from decimal to binary accurately enough 67 // to produce the hexadecimal values shown. 68 69 // Log returns the natural logarithm of x. 70 // 71 // Special cases are: 72 // 73 // Log(+Inf) = +Inf 74 // Log(0) = -Inf 75 // Log(x < 0) = NaN 76 // Log(NaN) = NaN 77 func Log(x float32) float32 78 79 func log(x float32) float32 { 80 const ( 81 Ln2Hi = 6.9313812256e-01 /* 0x3f317180 */ 82 Ln2Lo = 9.0580006145e-06 /* 0x3717f7d1 */ 83 L1 = 6.6666668653e-01 /* 0x3f2aaaab */ 84 L2 = 4.0000000596e-01 /* 0x3ecccccd */ 85 L3 = 2.8571429849e-01 /* 0x3e924925 */ 86 L4 = 2.2222198546e-01 /* 0x3e638e29 */ 87 L5 = 1.8183572590e-01 /* 0x3e3a3325 */ 88 L6 = 1.5313838422e-01 /* 0x3e1cd04f */ 89 L7 = 1.4798198640e-01 /* 0x3e178897 */ 90 ) 91 92 // special cases 93 switch { 94 case IsNaN(x) || IsInf(x, 1): 95 return x 96 case x < 0: 97 return NaN() 98 case x == 0: 99 return Inf(-1) 100 } 101 102 // reduce 103 f1, ki := Frexp(x) 104 if f1 < Sqrt2/2 { 105 f1 *= 2 106 ki-- 107 } 108 f := f1 - 1 109 k := float32(ki) 110 111 // compute 112 s := f / (2 + f) 113 s2 := s * s 114 s4 := s2 * s2 115 t1 := s2 * (L1 + s4*(L3+s4*(L5+s4*L7))) 116 t2 := s4 * (L2 + s4*(L4+s4*L6)) 117 R := t1 + t2 118 hfsq := 0.5 * f * f 119 return k*Ln2Hi - ((hfsq - (s*(hfsq+R) + k*Ln2Lo)) - f) 120 }