| 1 | module math |
| 2 | |
| 3 | const two54 = f64(1.80143985094819840000e+16) |
| 4 | const ivln10 = f64(4.34294481903251816668e-01) |
| 5 | const log10_2hi = f64(3.01029995663611771306e-01) |
| 6 | const log10_2lo = f64(3.69423907715893078616e-13) |
| 7 | |
| 8 | // log_n returns log base b of x |
| 9 | pub fn log_n(x f64, b f64) f64 { |
| 10 | y := log(x) |
| 11 | z := log(b) |
| 12 | return y / z |
| 13 | } |
| 14 | |
| 15 | // log10 returns the decimal logarithm of x. |
| 16 | // The special cases are the same as for log. |
| 17 | // log10(10**N) = N for N=0,1,...,22. |
| 18 | pub fn log10(x f64) f64 { |
| 19 | // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/dbl-64/e_log10.c |
| 20 | |
| 21 | mut x_ := x |
| 22 | mut hx := i64(f64_bits(x_)) |
| 23 | mut k := i32(0) |
| 24 | if hx < i64(0x0010000000000000) { |
| 25 | // x < 2**-1022 |
| 26 | if hx & 0x7fffffffffffffff == 0 { |
| 27 | return inf(-1) // log(+-0)=-inf |
| 28 | } |
| 29 | if hx < 0 { |
| 30 | return (x_ - x_) / (x_ - x_) // log(-#) = NaN |
| 31 | } |
| 32 | k = k - 54 |
| 33 | x_ *= two54 // subnormal number, scale up x |
| 34 | hx = i64(f64_bits(x_)) |
| 35 | } |
| 36 | |
| 37 | // scale up resulted in a NaN number |
| 38 | if hx >= u64(0x7ff0000000000000) { |
| 39 | return x_ + x_ |
| 40 | } |
| 41 | |
| 42 | k = k + i32((u64((hx >> 52) - 1023))) |
| 43 | i := i32((u64(k) & 0x8000000000000000) >> 63) |
| 44 | hx = (hx & 0x000fffffffffffff) | (u64(0x3ff - i) << 52) |
| 45 | y := f64(k + i) |
| 46 | /* |
| 47 | if FIX_INT_FP_CONVERT_ZERO && y == 0.0 { |
| 48 | y = 0.0 |
| 49 | } |
| 50 | */ |
| 51 | x_ = f64_from_bits(u64(hx)) |
| 52 | z := y * log10_2lo + ivln10 * log(x_) |
| 53 | return z + y * log10_2hi |
| 54 | } |
| 55 | |
| 56 | // log2 returns the binary logarithm of x. |
| 57 | // The special cases are the same as for log. |
| 58 | pub fn log2(x f64) f64 { |
| 59 | frac, expn := frexp(x) |
| 60 | // Make sure exact powers of two give an exact answer. |
| 61 | // Don't depend on log(0.5)*(1/ln2)+expn being exactly expn-1. |
| 62 | if frac == 0.5 { |
| 63 | return f64(expn - 1) |
| 64 | } |
| 65 | return log(frac) * (1.0 / ln2) + f64(expn) |
| 66 | } |
| 67 | |
| 68 | // log1p returns log(1+x) |
| 69 | pub fn log1p(x f64) f64 { |
| 70 | y := 1.0 + x |
| 71 | z := y - 1.0 |
| 72 | return log(y) - (z - x) / y // cancels errors with IEEE arithmetic |
| 73 | } |
| 74 | |
| 75 | // log_b returns the binary exponent of x. |
| 76 | // |
| 77 | // special cases are: |
| 78 | // log_b(±inf) = +inf |
| 79 | // log_b(0) = -inf |
| 80 | // log_b(nan) = nan |
| 81 | pub fn log_b(x f64) f64 { |
| 82 | if x == 0 { |
| 83 | return inf(-1) |
| 84 | } |
| 85 | if is_inf(x, 0) { |
| 86 | return inf(1) |
| 87 | } |
| 88 | if is_nan(x) { |
| 89 | return x |
| 90 | } |
| 91 | return f64(ilog_b_(x)) |
| 92 | } |
| 93 | |
| 94 | // ilog_b returns the binary exponent of x as an integer. |
| 95 | // |
| 96 | // special cases are: |
| 97 | // ilog_b(±inf) = max_i32 |
| 98 | // ilog_b(0) = min_i32 |
| 99 | // ilog_b(nan) = max_i32 |
| 100 | pub fn ilog_b(x f64) int { |
| 101 | if x == 0 { |
| 102 | return int(min_i32) |
| 103 | } |
| 104 | if is_nan(x) { |
| 105 | return int(max_i32) |
| 106 | } |
| 107 | if is_inf(x, 0) { |
| 108 | return int(max_i32) |
| 109 | } |
| 110 | return ilog_b_(x) |
| 111 | } |
| 112 | |
| 113 | // ilog_b returns the binary exponent of x. It assumes x is finite and |
| 114 | // non-zero. |
| 115 | fn ilog_b_(x_ f64) int { |
| 116 | x, expn := normalize(x_) |
| 117 | return int((f64_bits(x) >> shift) & mask) - bias + expn |
| 118 | } |
| 119 | |
| 120 | // log returns the natural logarithm of x |
| 121 | // |
| 122 | // Method : |
| 123 | // 1. Argument Reduction: find k and f such that |
| 124 | // x = 2^k * (1+f), |
| 125 | // where sqrt(2)/2 < 1+f < sqrt(2) . |
| 126 | // |
| 127 | // 2. Approximation of log(1+f). |
| 128 | // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) |
| 129 | // = 2s + 2/3 s**3 + 2/5 s**5 + ....., |
| 130 | // = 2s + s*R |
| 131 | // We use a special Remez algorithm on [0,0.1716] to generate |
| 132 | // a polynomial of degree 14 to approximate R The maximum error |
| 133 | // of this polynomial approximation is bounded by 2**-58.45. In |
| 134 | // other words, |
| 135 | // 2 4 6 8 10 12 14 |
| 136 | // R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s |
| 137 | // (the values of Lg1 to Lg7 are listed in the program) |
| 138 | // and |
| 139 | // | 2 14 | -58.45 |
| 140 | // | Lg1*s +...+Lg7*s - R(z) | <= 2 |
| 141 | // | | |
| 142 | // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. |
| 143 | // In order to guarantee error in log below 1ulp, we compute log |
| 144 | // by |
| 145 | // log(1+f) = f - s*(f - R) (if f is not too large) |
| 146 | // log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) |
| 147 | // |
| 148 | // 3. Finally, log(x) = k*ln2 + log(1+f). |
| 149 | // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) |
| 150 | // Here ln2 is split into two floating point number: |
| 151 | // ln2_hi + ln2_lo, |
| 152 | // where n*ln2_hi is always exact for |n| < 2000. |
| 153 | // |
| 154 | // Special cases: |
| 155 | // log(x) is NaN with signal if x < 0 (including -inf) ; |
| 156 | // log(+inf) is +inf; log(0) is -inf with signal; |
| 157 | // log(NaN) is that NaN with no signal. |
| 158 | // |
| 159 | // Accuracy: |
| 160 | // according to an error analysis, the error is always less than |
| 161 | // 1 ulp (unit in the last place). |
| 162 | pub fn log(a f64) f64 { |
| 163 | ln2_hi := 6.93147180369123816490e-01 // 3fe62e42 fee00000 |
| 164 | ln2_lo := 1.90821492927058770002e-10 // 3dea39ef 35793c76 |
| 165 | l1 := 6.666666666666735130e-01 // 3FE55555 55555593 |
| 166 | l2 := 3.999999999940941908e-01 // 3FD99999 9997FA04 |
| 167 | l3 := 2.857142874366239149e-01 // 3FD24924 94229359 |
| 168 | l4 := 2.222219843214978396e-01 // 3FCC71C5 1D8E78AF |
| 169 | l5 := 1.818357216161805012e-01 // 3FC74664 96CB03DE |
| 170 | l6 := 1.531383769920937332e-01 // 3FC39A09 D078C69F |
| 171 | l7 := 1.479819860511658591e-01 // 3FC2F112 DF3E5244 |
| 172 | |
| 173 | x := a |
| 174 | if is_nan(x) || is_inf(x, 1) { |
| 175 | return x |
| 176 | } else if x < 0 { |
| 177 | return nan() |
| 178 | } else if x == 0 { |
| 179 | return inf(-1) |
| 180 | } |
| 181 | |
| 182 | mut f1, mut ki := frexp(x) |
| 183 | if f1 < sqrt2 / 2 { |
| 184 | f1 *= 2 |
| 185 | ki-- |
| 186 | } |
| 187 | |
| 188 | f := f1 - 1 |
| 189 | k := f64(ki) |
| 190 | |
| 191 | // compute |
| 192 | s := f / (2 + f) |
| 193 | s2 := s * s |
| 194 | s4 := s2 * s2 |
| 195 | t1 := s2 * (l1 + s4 * (l3 + s4 * (l5 + s4 * l7))) |
| 196 | t2 := s4 * (l2 + s4 * (l4 + s4 * l6)) |
| 197 | r := t1 + t2 |
| 198 | hfsq := 0.5 * f * f |
| 199 | return k * ln2_hi - ((hfsq - (s * (hfsq + r) + k * ln2_lo)) - f) |
| 200 | } |
| 201 | |