@typeonce/ritmo-math 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CHANGELOG.md +183 -0
  2. package/README.md +14 -4
  3. package/dist/Collide2.d.ts +2 -1
  4. package/dist/Collide2.d.ts.map +1 -1
  5. package/dist/Collide2.js +10 -6
  6. package/dist/Collide2.js.map +1 -1
  7. package/dist/FlowField.d.ts.map +1 -1
  8. package/dist/FlowField.js +2 -1
  9. package/dist/FlowField.js.map +1 -1
  10. package/dist/InputAxis.js +1 -1
  11. package/dist/Ops.d.ts +209 -0
  12. package/dist/Ops.d.ts.map +1 -0
  13. package/dist/Ops.js +210 -0
  14. package/dist/Ops.js.map +1 -0
  15. package/dist/Random.d.ts +63 -10
  16. package/dist/Random.d.ts.map +1 -1
  17. package/dist/Random.js +57 -32
  18. package/dist/Random.js.map +1 -1
  19. package/dist/Scalar.d.ts +7 -2
  20. package/dist/Scalar.d.ts.map +1 -1
  21. package/dist/Scalar.js +2 -1
  22. package/dist/Scalar.js.map +1 -1
  23. package/dist/SpatialGrid.d.ts +7 -2
  24. package/dist/SpatialGrid.d.ts.map +1 -1
  25. package/dist/SpatialGrid.js +35 -4
  26. package/dist/SpatialGrid.js.map +1 -1
  27. package/dist/Vec2.d.ts.map +1 -1
  28. package/dist/Vec2.js +8 -7
  29. package/dist/Vec2.js.map +1 -1
  30. package/dist/Vector2.d.ts +12 -0
  31. package/dist/Vector2.d.ts.map +1 -1
  32. package/dist/Vector2.js +12 -0
  33. package/dist/Vector2.js.map +1 -1
  34. package/dist/index.d.ts +5 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +5 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/internal/fdlibm.d.ts +43 -0
  39. package/dist/internal/fdlibm.d.ts.map +1 -0
  40. package/dist/internal/fdlibm.js +1291 -0
  41. package/dist/internal/fdlibm.js.map +1 -0
  42. package/dist/internal/vector2.d.ts.map +1 -1
  43. package/dist/internal/vector2.js +2 -1
  44. package/dist/internal/vector2.js.map +1 -1
  45. package/package.json +3 -2
  46. package/src/Collide2.ts +10 -6
  47. package/src/FlowField.ts +2 -1
  48. package/src/InputAxis.ts +1 -1
  49. package/src/Ops.ts +232 -0
  50. package/src/Random.ts +104 -16
  51. package/src/Scalar.ts +7 -2
  52. package/src/SpatialGrid.ts +40 -6
  53. package/src/Vec2.ts +9 -7
  54. package/src/Vector2.ts +12 -0
  55. package/src/index.ts +5 -0
  56. package/src/internal/fdlibm.ts +1256 -0
  57. package/src/internal/vector2.ts +2 -1
@@ -0,0 +1,1291 @@
1
+ /**
2
+ * Pure-JavaScript ports of FreeBSD msun / musl libm (originally Sun fdlibm),
3
+ * built only from `+ - * /`, `Math.sqrt`, and bit access. ECMAScript rounds
4
+ * each of those exactly, so every engine and CPU returns the same bits.
5
+ *
6
+ * Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved.
7
+ * Developed at SunSoft, a Sun Microsystems, Inc. business.
8
+ * Permission to use, copy, modify, and distribute this software is freely
9
+ * granted, provided that this notice is preserved.
10
+ *
11
+ * musl libm is MIT licensed, Copyright (c) 2005-2020 Rich Felker, et al.
12
+ *
13
+ * Changes from the C sources: results follow ECMAScript where it differs from
14
+ * C (`pow(1, NaN)` and `pow(±1, ±Infinity)` are NaN), arguments of sin,
15
+ * cos, and tan beyond 2^20·π/2 are reduced exactly with BigInt instead of
16
+ * `__rem_pio2_large`, and `exp(1)` returns `Math.E`.
17
+ *
18
+ * @internal
19
+ */
20
+ const scratch = new Float64Array(1);
21
+ const words = new Uint32Array(scratch.buffer);
22
+ const LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
23
+ const HI = LITTLE_ENDIAN ? 1 : 0;
24
+ const LO = LITTLE_ENDIAN ? 0 : 1;
25
+ /** The high 32 bits of `x`, as a signed integer (sign, exponent, top of the mantissa). */
26
+ const highWord = (x) => {
27
+ scratch[0] = x;
28
+ return words[HI] | 0;
29
+ };
30
+ /** The low 32 bits of `x`, unsigned. */
31
+ const lowWord = (x) => {
32
+ scratch[0] = x;
33
+ return words[LO];
34
+ };
35
+ const fromWords = (high, low) => {
36
+ words[HI] = high;
37
+ words[LO] = low;
38
+ return scratch[0];
39
+ };
40
+ /** `x` with its low 32 bits cleared. */
41
+ const clearLow = (x) => {
42
+ scratch[0] = x;
43
+ words[LO] = 0;
44
+ return scratch[0];
45
+ };
46
+ const TWO_1023 = fromWords(0x7fe00000, 0);
47
+ const TWO_M1022 = fromWords(0x00100000, 0);
48
+ const TWO_53 = 9007199254740992;
49
+ /** `x * 2^n`, rounded once. */
50
+ const scalbn = (x, n) => {
51
+ let y = x;
52
+ let k = n;
53
+ if (k > 1023) {
54
+ y *= TWO_1023;
55
+ k -= 1023;
56
+ if (k > 1023) {
57
+ y *= TWO_1023;
58
+ k -= 1023;
59
+ if (k > 1023)
60
+ k = 1023;
61
+ }
62
+ }
63
+ else if (k < -1022) {
64
+ // Keep the final k below -53 to avoid double rounding into subnormals.
65
+ y *= TWO_M1022 * TWO_53;
66
+ k += 1022 - 53;
67
+ if (k < -1022) {
68
+ y *= TWO_M1022 * TWO_53;
69
+ k += 1022 - 53;
70
+ if (k < -1022)
71
+ k = -1022;
72
+ }
73
+ }
74
+ return y * fromWords((0x3ff + k) << 20, 0);
75
+ };
76
+ // ---------------------------------------------------------------------------
77
+ // Argument reduction: x = n·(π/2) + (y0 + y1), |y0 + y1| <= π/4
78
+ let reducedHigh = 0;
79
+ let reducedLow = 0;
80
+ const TO_INT = 1.5 / 2.220446049250313e-16;
81
+ const PIO4 = 7.85398163397448278999e-01;
82
+ const INVPIO2 = 6.36619772367581382433e-01;
83
+ const PIO2_1 = 1.57079632673412561417e+00;
84
+ const PIO2_1T = 6.07710050650619224932e-11;
85
+ const PIO2_2 = 6.07710050630396597660e-11;
86
+ const PIO2_2T = 2.02226624879595063154e-21;
87
+ const PIO2_3 = 2.02226624871116645580e-21;
88
+ const PIO2_3T = 8.47842766036889956997e-32;
89
+ /** Sets `reducedHigh`/`reducedLow` and returns n (only n mod 4 is meaningful for huge x). */
90
+ const remPio2 = (x) => {
91
+ const ix = highWord(x) & 0x7fffffff;
92
+ if (ix < 0x413921fb) {
93
+ // |x| < 2^20·π/2: Cody–Waite reduction with up to three rounds.
94
+ let fn = x * INVPIO2 + TO_INT - TO_INT;
95
+ let n = fn | 0;
96
+ let r = x - fn * PIO2_1;
97
+ let w = fn * PIO2_1T;
98
+ if (r - w < -PIO4) {
99
+ n--;
100
+ fn--;
101
+ r = x - fn * PIO2_1;
102
+ w = fn * PIO2_1T;
103
+ }
104
+ else if (r - w > PIO4) {
105
+ n++;
106
+ fn++;
107
+ r = x - fn * PIO2_1;
108
+ w = fn * PIO2_1T;
109
+ }
110
+ let y0 = r - w;
111
+ const ex = ix >> 20;
112
+ let ey = (highWord(y0) >> 20) & 0x7ff;
113
+ if (ex - ey > 16) {
114
+ let t = r;
115
+ w = fn * PIO2_2;
116
+ r = t - w;
117
+ w = fn * PIO2_2T - ((t - r) - w);
118
+ y0 = r - w;
119
+ ey = (highWord(y0) >> 20) & 0x7ff;
120
+ if (ex - ey > 49) {
121
+ t = r;
122
+ w = fn * PIO2_3;
123
+ r = t - w;
124
+ w = fn * PIO2_3T - ((t - r) - w);
125
+ y0 = r - w;
126
+ }
127
+ }
128
+ reducedHigh = y0;
129
+ reducedLow = (r - y0) - w;
130
+ return n;
131
+ }
132
+ if (ix >= 0x7ff00000) {
133
+ reducedHigh = x - x;
134
+ reducedLow = reducedHigh;
135
+ return 0;
136
+ }
137
+ return remPio2Large(x);
138
+ };
139
+ // Huge arguments: exact reduction with BigInt against 2/π to 1280 bits.
140
+ const PI_BITS = 1400;
141
+ let twoOverPi = 0n;
142
+ let halfPi130 = 0n;
143
+ /** arctan(1/k) · 2^bits, by its Taylor series in fixed point. */
144
+ const arctanInverse = (k, bits) => {
145
+ const one = 1n << bits;
146
+ const k2 = k * k;
147
+ let power = one / k;
148
+ let sum = power;
149
+ for (let i = 1n; power !== 0n; i++) {
150
+ power /= k2;
151
+ const term = power / (2n * i + 1n);
152
+ sum = i % 2n === 1n ? sum - term : sum + term;
153
+ }
154
+ return sum;
155
+ };
156
+ const TWO_OVER_PI_BITS = 1280n;
157
+ const initReduction = () => {
158
+ const bits = BigInt(PI_BITS);
159
+ // Machin: π = 16·atan(1/5) − 4·atan(1/239)
160
+ const pi = 16n * arctanInverse(5n, bits) - 4n * arctanInverse(239n, bits);
161
+ twoOverPi = (1n << (TWO_OVER_PI_BITS + 1n + bits)) / pi;
162
+ halfPi130 = pi >> (bits - 129n);
163
+ };
164
+ const remPio2Large = (x) => {
165
+ if (twoOverPi === 0n)
166
+ initReduction();
167
+ const negative = x < 0;
168
+ const ax = negative ? -x : x;
169
+ // ax = mantissa · 2^exponent, exactly (ax >= 2^20 here, so it is normal).
170
+ const exponent = ((highWord(ax) >> 20) & 0x7ff) - 1075;
171
+ const mantissa = BigInt(ax / fromWords((exponent + 1023) << 20, 0));
172
+ // ax · 2/π = product / 2^shift
173
+ const shift = TWO_OVER_PI_BITS - BigInt(exponent);
174
+ const product = mantissa * twoOverPi;
175
+ const unit = 1n << shift;
176
+ let n = Number((product >> shift) & 3n);
177
+ let fraction = product & (unit - 1n);
178
+ if (fraction >= unit >> 1n) {
179
+ fraction -= unit;
180
+ n = (n + 1) & 3;
181
+ }
182
+ // Keep 250 fraction bits, multiply by π/2 (130 bits): r = fraction·π/2 · 2^-380
183
+ const r = (fraction >> (shift - 250n)) * halfPi130;
184
+ const scale = fromWords((380 + 1023) << 20, 0);
185
+ const high = Number(r) / scale;
186
+ const low = Number(r - BigInt(high * scale)) / scale;
187
+ reducedHigh = negative ? -high : high;
188
+ reducedLow = negative ? -low : low;
189
+ return negative ? -n : n;
190
+ };
191
+ // ---------------------------------------------------------------------------
192
+ // Kernels on [-π/4, π/4]
193
+ const S1 = -1.66666666666666324348e-01;
194
+ const S2 = 8.33333333332248946124e-03;
195
+ const S3 = -1.98412698298579493134e-04;
196
+ const S4 = 2.75573137070700676789e-06;
197
+ const S5 = -2.50507602534068634195e-08;
198
+ const S6 = 1.58969099521155010221e-10;
199
+ const kernelSin = (x, y, hasTail) => {
200
+ const z = x * x;
201
+ const w = z * z;
202
+ const r = S2 + z * (S3 + z * S4) + z * w * (S5 + z * S6);
203
+ const v = z * x;
204
+ if (!hasTail)
205
+ return x + v * (S1 + z * r);
206
+ return x - ((z * (0.5 * y - v * r) - y) - v * S1);
207
+ };
208
+ const C1 = 4.16666666666666019037e-02;
209
+ const C2 = -1.38888888888741095749e-03;
210
+ const C3 = 2.48015872894767294178e-05;
211
+ const C4 = -2.75573143513906633035e-07;
212
+ const C5 = 2.08757232129817482790e-09;
213
+ const C6 = -1.13596475577881948265e-11;
214
+ const kernelCos = (x, y) => {
215
+ const z = x * x;
216
+ const w = z * z;
217
+ const r = z * (C1 + z * (C2 + z * C3)) + w * w * (C4 + z * (C5 + z * C6));
218
+ const hz = 0.5 * z;
219
+ const v = 1.0 - hz;
220
+ return v + (((1.0 - v) - hz) + (z * r - x * y));
221
+ };
222
+ const T = [
223
+ 3.33333333333334091986e-01,
224
+ 1.33333333333201242699e-01,
225
+ 5.39682539762260521377e-02,
226
+ 2.18694882948595424599e-02,
227
+ 8.86323982359930005737e-03,
228
+ 3.59207910759131235356e-03,
229
+ 1.45620945432529025516e-03,
230
+ 5.88041240820264096874e-04,
231
+ 2.46463134818469906812e-04,
232
+ 7.81794442939557092300e-05,
233
+ 7.14072491382608190305e-05,
234
+ -1.85586374855275456654e-05,
235
+ 2.59073051863633712884e-05
236
+ ];
237
+ const PIO4_LO = 3.06161699786838301793e-17;
238
+ /** tan(x + y) when `odd` is false, -1/tan(x + y) when it is true. */
239
+ const kernelTan = (xIn, yIn, odd) => {
240
+ let x = xIn;
241
+ let y = yIn;
242
+ const hx = highWord(x);
243
+ const big = (hx & 0x7fffffff) >= 0x3fe59428; // |x| >= 0.6744
244
+ const negative = hx < 0;
245
+ if (big) {
246
+ if (negative) {
247
+ x = -x;
248
+ y = -y;
249
+ }
250
+ x = (PIO4 - x) + (PIO4_LO - y);
251
+ y = 0.0;
252
+ }
253
+ const z = x * x;
254
+ let w = z * z;
255
+ let r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + w * T[11]))));
256
+ let v = z * (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + w * T[12])))));
257
+ let s = z * x;
258
+ r = y + z * (s * (r + v) + y) + s * T[0];
259
+ w = x + r;
260
+ if (big) {
261
+ s = odd ? -1 : 1;
262
+ v = s - 2.0 * (x + (r - w * w / (w + s)));
263
+ return negative ? -v : v;
264
+ }
265
+ if (!odd)
266
+ return w;
267
+ // -1/(x + r) has up to 2 ulp error, so compute it accurately.
268
+ const w0 = clearLow(w);
269
+ v = r - (w0 - x);
270
+ const a = -1.0 / w;
271
+ const a0 = clearLow(a);
272
+ return a0 + a * (1.0 + a0 * w0 + a0 * v);
273
+ };
274
+ // ---------------------------------------------------------------------------
275
+ // Trigonometric functions
276
+ export const sin = (x) => {
277
+ const ix = highWord(x) & 0x7fffffff;
278
+ if (ix <= 0x3fe921fb) {
279
+ if (ix < 0x3e500000)
280
+ return x; // |x| < 2^-26
281
+ return kernelSin(x, 0, false);
282
+ }
283
+ if (ix >= 0x7ff00000)
284
+ return x - x;
285
+ const n = remPio2(x);
286
+ switch (n & 3) {
287
+ case 0:
288
+ return kernelSin(reducedHigh, reducedLow, true);
289
+ case 1:
290
+ return kernelCos(reducedHigh, reducedLow);
291
+ case 2:
292
+ return -kernelSin(reducedHigh, reducedLow, true);
293
+ default:
294
+ return -kernelCos(reducedHigh, reducedLow);
295
+ }
296
+ };
297
+ export const cos = (x) => {
298
+ const ix = highWord(x) & 0x7fffffff;
299
+ if (ix <= 0x3fe921fb) {
300
+ if (ix < 0x3e46a09e)
301
+ return 1.0; // |x| < 2^-27·√2
302
+ return kernelCos(x, 0);
303
+ }
304
+ if (ix >= 0x7ff00000)
305
+ return x - x;
306
+ const n = remPio2(x);
307
+ switch (n & 3) {
308
+ case 0:
309
+ return kernelCos(reducedHigh, reducedLow);
310
+ case 1:
311
+ return -kernelSin(reducedHigh, reducedLow, true);
312
+ case 2:
313
+ return -kernelCos(reducedHigh, reducedLow);
314
+ default:
315
+ return kernelSin(reducedHigh, reducedLow, true);
316
+ }
317
+ };
318
+ export const tan = (x) => {
319
+ const ix = highWord(x) & 0x7fffffff;
320
+ if (ix <= 0x3fe921fb) {
321
+ if (ix < 0x3e400000)
322
+ return x; // |x| < 2^-27
323
+ return kernelTan(x, 0.0, false);
324
+ }
325
+ if (ix >= 0x7ff00000)
326
+ return x - x;
327
+ const n = remPio2(x);
328
+ return kernelTan(reducedHigh, reducedLow, (n & 1) !== 0);
329
+ };
330
+ // ---------------------------------------------------------------------------
331
+ // Inverse trigonometric functions
332
+ const PIO2_HI = 1.57079632679489655800e+00;
333
+ const PIO2_LO = 6.12323399573676603587e-17;
334
+ const PS0 = 1.66666666666666657415e-01;
335
+ const PS1 = -3.25565818622400915405e-01;
336
+ const PS2 = 2.01212532134862925881e-01;
337
+ const PS3 = -4.00555345006794114027e-02;
338
+ const PS4 = 7.91534994289814532176e-04;
339
+ const PS5 = 3.47933107596021167570e-05;
340
+ const QS1 = -2.40339491173441421878e+00;
341
+ const QS2 = 2.02094576023350569471e+00;
342
+ const QS3 = -6.88283971605453293030e-01;
343
+ const QS4 = 7.70381505559019352791e-02;
344
+ const asinRatio = (z) => {
345
+ const p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z * (PS4 + z * PS5)))));
346
+ const q = 1.0 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
347
+ return p / q;
348
+ };
349
+ export const asin = (x) => {
350
+ const hx = highWord(x);
351
+ const ix = hx & 0x7fffffff;
352
+ if (ix >= 0x3ff00000) {
353
+ // |x| >= 1 or NaN
354
+ if (((ix - 0x3ff00000) | lowWord(x)) === 0)
355
+ return x * PIO2_HI;
356
+ return 0 / (x - x);
357
+ }
358
+ if (ix < 0x3fe00000) {
359
+ // |x| < 0.5
360
+ if (ix < 0x3e500000 && ix >= 0x00100000)
361
+ return x;
362
+ return x + x * asinRatio(x * x);
363
+ }
364
+ // 0.5 <= |x| < 1
365
+ const z = (1 - Math.abs(x)) * 0.5;
366
+ const s = Math.sqrt(z);
367
+ const r = asinRatio(z);
368
+ let result;
369
+ if (ix >= 0x3fef3333) {
370
+ // |x| > 0.975
371
+ result = PIO2_HI - (2 * (s + s * r) - PIO2_LO);
372
+ }
373
+ else {
374
+ const f = clearLow(s);
375
+ const c = (z - f * f) / (s + f);
376
+ result = 0.5 * PIO2_HI - (2 * s * r - (PIO2_LO - 2 * c) - (0.5 * PIO2_HI - 2 * f));
377
+ }
378
+ return hx < 0 ? -result : result;
379
+ };
380
+ export const acos = (x) => {
381
+ const hx = highWord(x);
382
+ const ix = hx & 0x7fffffff;
383
+ if (ix >= 0x3ff00000) {
384
+ // |x| >= 1 or NaN
385
+ if (((ix - 0x3ff00000) | lowWord(x)) === 0)
386
+ return hx < 0 ? 2 * PIO2_HI : 0;
387
+ return 0 / (x - x);
388
+ }
389
+ if (ix < 0x3fe00000) {
390
+ // |x| < 0.5
391
+ if (ix <= 0x3c600000)
392
+ return PIO2_HI; // |x| < 2^-57
393
+ return PIO2_HI - (x - (PIO2_LO - x * asinRatio(x * x)));
394
+ }
395
+ if (hx < 0) {
396
+ // x < -0.5
397
+ const z = (1.0 + x) * 0.5;
398
+ const s = Math.sqrt(z);
399
+ const w = asinRatio(z) * s - PIO2_LO;
400
+ return 2 * (PIO2_HI - (s + w));
401
+ }
402
+ // x > 0.5
403
+ const z = (1.0 - x) * 0.5;
404
+ const s = Math.sqrt(z);
405
+ const df = clearLow(s);
406
+ const c = (z - df * df) / (s + df);
407
+ const w = asinRatio(z) * s + c;
408
+ return 2 * (df + w);
409
+ };
410
+ const ATAN_HI = [
411
+ 4.63647609000806093515e-01,
412
+ 7.85398163397448278999e-01,
413
+ 9.82793723247329054082e-01,
414
+ 1.57079632679489655800e+00
415
+ ];
416
+ const ATAN_LO = [
417
+ 2.26987774529616870924e-17,
418
+ 3.06161699786838301793e-17,
419
+ 1.39033110312309984516e-17,
420
+ 6.12323399573676603587e-17
421
+ ];
422
+ const AT = [
423
+ 3.33333333333329318027e-01,
424
+ -1.99999999998764832476e-01,
425
+ 1.42857142725034663711e-01,
426
+ -1.11111104054623557880e-01,
427
+ 9.09088713343650656196e-02,
428
+ -7.69187620504482999495e-02,
429
+ 6.66107313738753120669e-02,
430
+ -5.83357013379057348645e-02,
431
+ 4.97687799461593236017e-02,
432
+ -3.65315727442169155270e-02,
433
+ 1.62858201153657823623e-02
434
+ ];
435
+ export const atan = (xIn) => {
436
+ let x = xIn;
437
+ const hx = highWord(x);
438
+ const negative = hx < 0;
439
+ const ix = hx & 0x7fffffff;
440
+ if (ix >= 0x44100000) {
441
+ // |x| >= 2^66 or NaN
442
+ if (x !== x)
443
+ return x;
444
+ return negative ? -ATAN_HI[3] : ATAN_HI[3];
445
+ }
446
+ let id;
447
+ if (ix < 0x3fdc0000) {
448
+ // |x| < 0.4375
449
+ if (ix < 0x3e400000)
450
+ return x; // |x| < 2^-27
451
+ id = -1;
452
+ }
453
+ else {
454
+ x = Math.abs(x);
455
+ if (ix < 0x3ff30000) {
456
+ // |x| < 1.1875
457
+ if (ix < 0x3fe60000) {
458
+ // 7/16 <= |x| < 11/16
459
+ id = 0;
460
+ x = (2.0 * x - 1.0) / (2.0 + x);
461
+ }
462
+ else {
463
+ // 11/16 <= |x| < 19/16
464
+ id = 1;
465
+ x = (x - 1.0) / (x + 1.0);
466
+ }
467
+ }
468
+ else if (ix < 0x40038000) {
469
+ // |x| < 2.4375
470
+ id = 2;
471
+ x = (x - 1.5) / (1.0 + 1.5 * x);
472
+ }
473
+ else {
474
+ // 2.4375 <= |x| < 2^66
475
+ id = 3;
476
+ x = -1.0 / x;
477
+ }
478
+ }
479
+ const z = x * x;
480
+ const w = z * z;
481
+ const s1 = z * (AT[0] + w * (AT[2] + w * (AT[4] + w * (AT[6] + w * (AT[8] + w * AT[10])))));
482
+ const s2 = w * (AT[1] + w * (AT[3] + w * (AT[5] + w * (AT[7] + w * AT[9]))));
483
+ if (id < 0)
484
+ return x - x * (s1 + s2);
485
+ const result = ATAN_HI[id] - ((x * (s1 + s2) - ATAN_LO[id]) - x);
486
+ return negative ? -result : result;
487
+ };
488
+ const PI = 3.1415926535897931160e+00;
489
+ const PI_LO = 1.2246467991473531772e-16;
490
+ export const atan2 = (y, x) => {
491
+ if (x !== x || y !== y)
492
+ return x + y;
493
+ const hx = highWord(x);
494
+ const lx = lowWord(x);
495
+ const hy = highWord(y);
496
+ const ly = lowWord(y);
497
+ if (((hx - 0x3ff00000) | lx) === 0)
498
+ return atan(y); // x = 1
499
+ const m = ((hy >>> 31) & 1) | ((hx >>> 30) & 2); // 2·sign(x) + sign(y)
500
+ const ix = hx & 0x7fffffff;
501
+ const iy = hy & 0x7fffffff;
502
+ if ((iy | ly) === 0) {
503
+ // y = ±0
504
+ switch (m) {
505
+ case 0:
506
+ case 1:
507
+ return y;
508
+ case 2:
509
+ return PI;
510
+ default:
511
+ return -PI;
512
+ }
513
+ }
514
+ if ((ix | lx) === 0)
515
+ return m & 1 ? -PI / 2 : PI / 2; // x = ±0
516
+ if (ix === 0x7ff00000) {
517
+ // x = ±Infinity
518
+ if (iy === 0x7ff00000) {
519
+ switch (m) {
520
+ case 0:
521
+ return PI / 4;
522
+ case 1:
523
+ return -PI / 4;
524
+ case 2:
525
+ return 3 * PI / 4;
526
+ default:
527
+ return -3 * PI / 4;
528
+ }
529
+ }
530
+ switch (m) {
531
+ case 0:
532
+ return 0.0;
533
+ case 1:
534
+ return -0.0;
535
+ case 2:
536
+ return PI;
537
+ default:
538
+ return -PI;
539
+ }
540
+ }
541
+ // |y/x| > 2^64
542
+ if (ix + (64 << 20) < iy || iy === 0x7ff00000)
543
+ return m & 1 ? -PI / 2 : PI / 2;
544
+ // atan(|y/x|) without spurious underflow
545
+ const z = (m & 2) !== 0 && iy + (64 << 20) < ix ? 0 : atan(Math.abs(y / x));
546
+ switch (m) {
547
+ case 0:
548
+ return z;
549
+ case 1:
550
+ return -z;
551
+ case 2:
552
+ return PI - (z - PI_LO);
553
+ default:
554
+ return (z - PI_LO) - PI;
555
+ }
556
+ };
557
+ // ---------------------------------------------------------------------------
558
+ // Exponential functions
559
+ const LN2_HI = 6.93147180369123816490e-01;
560
+ const LN2_LO = 1.90821492927058770002e-10;
561
+ const INV_LN2 = 1.44269504088896338700e+00;
562
+ const P1 = 1.66666666666666019037e-01;
563
+ const P2 = -2.77777777770155933842e-03;
564
+ const P3 = 6.61375632143793436117e-05;
565
+ const P4 = -1.65339022054652515390e-06;
566
+ const P5 = 4.13813679705723846039e-08;
567
+ export const exp = (xIn) => {
568
+ let x = xIn;
569
+ const hx = highWord(x);
570
+ const negative = hx < 0;
571
+ const ix = hx & 0x7fffffff;
572
+ if (ix >= 0x4086232b) {
573
+ // |x| >= 708.39 or NaN
574
+ if (x !== x)
575
+ return x;
576
+ if (x > 709.782712893383973096)
577
+ return x * TWO_1023; // overflow
578
+ if (x < -745.13321910194110842)
579
+ return 0; // underflow
580
+ }
581
+ if (x === 1)
582
+ return 2.718281828459045; // Math.E: the reduction below lands 1 ulp above it
583
+ let hi = 0;
584
+ let lo = 0;
585
+ let k = 0;
586
+ if (ix > 0x3fd62e42) {
587
+ // |x| > 0.5·ln2
588
+ if (ix >= 0x3ff0a2b2) {
589
+ // |x| >= 1.5·ln2
590
+ k = (INV_LN2 * x + (negative ? -0.5 : 0.5)) | 0;
591
+ }
592
+ else {
593
+ k = negative ? -1 : 1;
594
+ }
595
+ hi = x - k * LN2_HI;
596
+ lo = k * LN2_LO;
597
+ x = hi - lo;
598
+ }
599
+ else if (ix > 0x3e300000) {
600
+ // |x| > 2^-28
601
+ hi = x;
602
+ }
603
+ else {
604
+ return 1 + x;
605
+ }
606
+ const xx = x * x;
607
+ const c = x - xx * (P1 + xx * (P2 + xx * (P3 + xx * (P4 + xx * P5))));
608
+ const y = 1 + (x * c / (2 - c) - lo + hi);
609
+ return k === 0 ? y : scalbn(y, k);
610
+ };
611
+ const O_THRESHOLD = 7.09782712893383973096e+02;
612
+ const Q1 = -3.33333333333331316428e-02;
613
+ const Q2 = 1.58730158725481460165e-03;
614
+ const Q3 = -7.93650757867487942473e-05;
615
+ const Q4 = 4.00821782732936239552e-06;
616
+ const Q5 = -2.01099218183624371326e-07;
617
+ /** e^x − 1, accurate near 0. */
618
+ export const expm1 = (xIn) => {
619
+ let x = xIn;
620
+ const hx = highWord(x);
621
+ const negative = hx < 0;
622
+ const ix = hx & 0x7fffffff;
623
+ if (ix >= 0x4043687a) {
624
+ // |x| >= 56·ln2
625
+ if (x !== x)
626
+ return x;
627
+ if (negative)
628
+ return -1;
629
+ if (x > O_THRESHOLD)
630
+ return x * TWO_1023;
631
+ }
632
+ let k;
633
+ let c = 0;
634
+ if (ix > 0x3fd62e42) {
635
+ // |x| > 0.5·ln2
636
+ let hi;
637
+ let lo;
638
+ if (ix < 0x3ff0a2b2) {
639
+ // |x| < 1.5·ln2
640
+ if (!negative) {
641
+ hi = x - LN2_HI;
642
+ lo = LN2_LO;
643
+ k = 1;
644
+ }
645
+ else {
646
+ hi = x + LN2_HI;
647
+ lo = -LN2_LO;
648
+ k = -1;
649
+ }
650
+ }
651
+ else {
652
+ k = (INV_LN2 * x + (negative ? -0.5 : 0.5)) | 0;
653
+ hi = x - k * LN2_HI;
654
+ lo = k * LN2_LO;
655
+ }
656
+ x = hi - lo;
657
+ c = (hi - x) - lo;
658
+ }
659
+ else if (ix < 0x3c900000) {
660
+ return x; // |x| < 2^-54
661
+ }
662
+ else {
663
+ k = 0;
664
+ }
665
+ const hfx = 0.5 * x;
666
+ const hxs = x * hfx;
667
+ const r1 = 1.0 + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
668
+ const t = 3.0 - r1 * hfx;
669
+ let e = hxs * ((r1 - t) / (6.0 - x * t));
670
+ if (k === 0)
671
+ return x - (x * e - hxs);
672
+ e = x * (e - c) - c;
673
+ e -= hxs;
674
+ if (k === -1)
675
+ return 0.5 * (x - e) - 0.5;
676
+ if (k === 1) {
677
+ if (x < -0.25)
678
+ return -2.0 * (e - (x + 0.5));
679
+ return 1.0 + 2.0 * (x - e);
680
+ }
681
+ if (k < 0 || k > 56) {
682
+ const y = x - e + 1.0;
683
+ return (k === 1024 ? y * 2.0 * TWO_1023 : y * fromWords((0x3ff + k) << 20, 0)) - 1.0;
684
+ }
685
+ const twopk = fromWords((0x3ff + k) << 20, 0);
686
+ const twomk = fromWords((0x3ff - k) << 20, 0);
687
+ if (k < 20)
688
+ return (x - e + (1 - twomk)) * twopk;
689
+ return (x - (e + twomk) + 1) * twopk;
690
+ };
691
+ export const tanh = (xIn) => {
692
+ const negative = highWord(xIn) < 0;
693
+ const x = Math.abs(xIn);
694
+ const w = highWord(x);
695
+ let t;
696
+ if (w > 0x3fe193ea) {
697
+ // |x| > log(3)/2 or NaN
698
+ if (w > 0x40340000) {
699
+ t = 1 - 0 / x; // |x| > 20 or NaN
700
+ }
701
+ else {
702
+ t = expm1(2 * x);
703
+ t = 1 - 2 / (t + 2);
704
+ }
705
+ }
706
+ else if (w > 0x3fd058ae) {
707
+ // |x| > log(5/3)/2
708
+ t = expm1(2 * x);
709
+ t = t / (t + 2);
710
+ }
711
+ else if (w >= 0x00100000) {
712
+ t = expm1(-2 * x);
713
+ t = -t / (t + 2);
714
+ }
715
+ else {
716
+ t = x; // subnormal
717
+ }
718
+ return negative ? -t : t;
719
+ };
720
+ // ---------------------------------------------------------------------------
721
+ // Power
722
+ const BP = [1.0, 1.5];
723
+ const DP_H = [0.0, 5.84962487220764160156e-01];
724
+ const DP_L = [0.0, 1.35003920212974897128e-08];
725
+ const HUGE = 1.0e300;
726
+ const TINY = 1.0e-300;
727
+ const L1 = 5.99999999999994648725e-01;
728
+ const L2 = 4.28571428578550184252e-01;
729
+ const L3 = 3.33333329818377432918e-01;
730
+ const L4 = 2.72728123808534006489e-01;
731
+ const L5 = 2.30660745775561754067e-01;
732
+ const L6 = 2.06975017800338417784e-01;
733
+ const LG2 = 6.93147180559945286227e-01;
734
+ const LG2_H = 6.93147182464599609375e-01;
735
+ const LG2_L = -1.90465429995776804525e-09;
736
+ const OVT = 8.0085662595372944372e-17;
737
+ const CP = 9.61796693925975554329e-01;
738
+ const CP_H = 9.61796700954437255859e-01;
739
+ const CP_L = -7.02846165095275826516e-09;
740
+ const IVLN2 = 1.44269504088896338700e+00;
741
+ const IVLN2_H = 1.44269502162933349609e+00;
742
+ const IVLN2_L = 1.92596299112661746887e-08;
743
+ export const pow = (x, y) => {
744
+ // ECMAScript Number::exponentiate, where it differs from C pow.
745
+ if (y !== y)
746
+ return NaN;
747
+ if (y === 0)
748
+ return 1;
749
+ if (x !== x)
750
+ return NaN;
751
+ const hx = highWord(x);
752
+ const lx = lowWord(x);
753
+ const hy = highWord(y);
754
+ const ly = lowWord(y);
755
+ let ix = hx & 0x7fffffff;
756
+ const iy = hy & 0x7fffffff;
757
+ if (iy === 0x7ff00000 && ly === 0 && ((ix - 0x3ff00000) | lx) === 0)
758
+ return NaN; // (±1) ** ±Infinity
759
+ if (hx === 0x3ff00000 && lx === 0)
760
+ return 1.0; // 1 ** y
761
+ // yisint: 0 when y is not an integer, 1 when odd, 2 when even (only when x < 0).
762
+ let yisint = 0;
763
+ if (hx < 0) {
764
+ if (iy >= 0x43400000) {
765
+ yisint = 2;
766
+ }
767
+ else if (iy >= 0x3ff00000) {
768
+ const e = (iy >> 20) - 0x3ff;
769
+ if (e > 20) {
770
+ const j = ly >>> (52 - e);
771
+ if (((j << (52 - e)) >>> 0) === ly)
772
+ yisint = 2 - (j & 1);
773
+ }
774
+ else if (ly === 0) {
775
+ const j = iy >> (20 - e);
776
+ if ((j << (20 - e)) === iy)
777
+ yisint = 2 - (j & 1);
778
+ }
779
+ }
780
+ }
781
+ if (ly === 0) {
782
+ if (iy === 0x7ff00000) {
783
+ // y = ±Infinity
784
+ if (ix >= 0x3ff00000)
785
+ return hy >= 0 ? y : 0.0; // (|x| > 1) ** ±Infinity
786
+ return hy >= 0 ? 0.0 : -y; // (|x| < 1) ** ±Infinity
787
+ }
788
+ if (iy === 0x3ff00000)
789
+ return hy >= 0 ? x : 1 / x; // y = ±1
790
+ if (hy === 0x40000000)
791
+ return x * x; // y = 2
792
+ if (hy === 0x3fe00000 && hx >= 0)
793
+ return Math.sqrt(x); // y = 0.5, x >= +0
794
+ }
795
+ let ax = Math.abs(x);
796
+ if (lx === 0 && (ix === 0x7ff00000 || ix === 0 || ix === 0x3ff00000)) {
797
+ // x = ±0, ±Infinity, ±1
798
+ let z = ax;
799
+ if (hy < 0)
800
+ z = 1.0 / z;
801
+ if (hx < 0) {
802
+ if (((ix - 0x3ff00000) | yisint) === 0) {
803
+ z = (z - z) / (z - z); // (-1) ** non-integer
804
+ }
805
+ else if (yisint === 1) {
806
+ z = -z;
807
+ }
808
+ }
809
+ return z;
810
+ }
811
+ let s = 1.0; // sign of the result
812
+ if (hx < 0) {
813
+ if (yisint === 0)
814
+ return (x - x) / (x - x); // (x < 0) ** non-integer
815
+ if (yisint === 1)
816
+ s = -1.0;
817
+ }
818
+ let t1;
819
+ let t2;
820
+ if (iy > 0x41e00000) {
821
+ // |y| > 2^31
822
+ if (iy > 0x43f00000) {
823
+ // |y| > 2^64: must overflow or underflow
824
+ if (ix <= 0x3fefffff)
825
+ return hy < 0 ? HUGE * HUGE : TINY * TINY;
826
+ if (ix >= 0x3ff00000)
827
+ return hy > 0 ? HUGE * HUGE : TINY * TINY;
828
+ }
829
+ // Overflow or underflow when x is not close to one.
830
+ if (ix < 0x3fefffff)
831
+ return hy < 0 ? s * HUGE * HUGE : s * TINY * TINY;
832
+ if (ix > 0x3ff00000)
833
+ return hy > 0 ? s * HUGE * HUGE : s * TINY * TINY;
834
+ // |1 - x| <= 2^-20: log(x) by x - x^2/2 + x^3/3 - x^4/4
835
+ const t = ax - 1.0;
836
+ const w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
837
+ const u = IVLN2_H * t;
838
+ const v = t * IVLN2_L - w * IVLN2;
839
+ t1 = clearLow(u + v);
840
+ t2 = v - (t1 - u);
841
+ }
842
+ else {
843
+ let n = 0;
844
+ if (ix < 0x00100000) {
845
+ // subnormal x
846
+ ax *= TWO_53;
847
+ n -= 53;
848
+ ix = highWord(ax);
849
+ }
850
+ n += (ix >> 20) - 0x3ff;
851
+ const j = ix & 0x000fffff;
852
+ ix = j | 0x3ff00000;
853
+ let k;
854
+ if (j <= 0x3988e) {
855
+ k = 0; // |x| < sqrt(3/2)
856
+ }
857
+ else if (j < 0xbb67a) {
858
+ k = 1; // |x| < sqrt(3)
859
+ }
860
+ else {
861
+ k = 0;
862
+ n += 1;
863
+ ix -= 0x00100000;
864
+ }
865
+ ax = fromWords(ix, lowWord(ax));
866
+ // ss = s_h + s_l = (x - 1)/(x + 1) or (x - 1.5)/(x + 1.5)
867
+ let u = ax - BP[k];
868
+ let v = 1.0 / (ax + BP[k]);
869
+ const ss = u * v;
870
+ const sH = clearLow(ss);
871
+ let tH = fromWords(((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18), 0);
872
+ let tL = ax - (tH - BP[k]);
873
+ const sL = v * ((u - sH * tH) - sH * tL);
874
+ // log(ax)
875
+ let s2 = ss * ss;
876
+ let r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
877
+ r += sL * (sH + ss);
878
+ s2 = sH * sH;
879
+ tH = clearLow(3.0 + s2 + r);
880
+ tL = r - ((tH - 3.0) - s2);
881
+ u = sH * tH;
882
+ v = sL * tH + tL * ss;
883
+ const pH = clearLow(u + v);
884
+ const pL = v - (pH - u);
885
+ const zH = CP_H * pH;
886
+ const zL = CP_L * pH + pL * CP + DP_L[k];
887
+ const t = n;
888
+ t1 = clearLow(((zH + zL) + DP_H[k]) + t);
889
+ t2 = zL - (((t1 - t) - DP_H[k]) - zH);
890
+ }
891
+ // (y1 + y2)·(t1 + t2)
892
+ const y1 = clearLow(y);
893
+ const pL = (y - y1) * t1 + y * t2;
894
+ let pH = y1 * t1;
895
+ let z = pL + pH;
896
+ let j = highWord(z);
897
+ const i = lowWord(z);
898
+ if (j >= 0x40900000) {
899
+ // z >= 1024
900
+ if (((j - 0x40900000) | i) !== 0)
901
+ return s * HUGE * HUGE;
902
+ if (pL + OVT > z - pH)
903
+ return s * HUGE * HUGE;
904
+ }
905
+ else if ((j & 0x7fffffff) >= 0x4090cc00) {
906
+ // z <= -1075
907
+ if (((j - (0xc090cc00 | 0)) | i) !== 0)
908
+ return s * TINY * TINY;
909
+ if (pL <= z - pH)
910
+ return s * TINY * TINY;
911
+ }
912
+ // 2^(pH + pL)
913
+ const abs = j & 0x7fffffff;
914
+ let k = (abs >> 20) - 0x3ff;
915
+ let n = 0;
916
+ if (abs > 0x3fe00000) {
917
+ // |z| > 0.5: n = [z + 0.5]
918
+ n = j + (0x00100000 >> (k + 1));
919
+ k = ((n & 0x7fffffff) >> 20) - 0x3ff;
920
+ const t = fromWords(n & ~(0x000fffff >> k), 0);
921
+ n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
922
+ if (j < 0)
923
+ n = -n;
924
+ pH -= t;
925
+ }
926
+ const t = clearLow(pL + pH);
927
+ const u = t * LG2_H;
928
+ const v = (pL - (t - pH)) * LG2 + t * LG2_L;
929
+ z = u + v;
930
+ const w = v - (z - u);
931
+ const tt = z * z;
932
+ const r1 = z - tt * (P1 + tt * (P2 + tt * (P3 + tt * (P4 + tt * P5))));
933
+ const r = (z * r1) / (r1 - 2.0) - (w + z * w);
934
+ z = 1.0 - (r - z);
935
+ j = highWord(z);
936
+ j += n << 20;
937
+ if ((j >> 20) <= 0) {
938
+ z = scalbn(z, n); // subnormal result
939
+ }
940
+ else {
941
+ z = fromWords(j, lowWord(z));
942
+ }
943
+ return s * z;
944
+ };
945
+ // ---------------------------------------------------------------------------
946
+ // hypot
947
+ const SPLIT = 134217729; // 2^27 + 1
948
+ const TWO_700 = fromWords((700 + 1023) << 20, 0);
949
+ const TWO_M700 = fromWords((-700 + 1023) << 20, 0);
950
+ export const hypot = (xIn, yIn) => {
951
+ let hx = highWord(xIn) & 0x7fffffff;
952
+ let lx = lowWord(xIn);
953
+ let hy = highWord(yIn) & 0x7fffffff;
954
+ let ly = lowWord(yIn);
955
+ // Arrange |x| >= |y| by their bits, so NaN sorts above Infinity.
956
+ if (hx < hy || (hx === hy && lx < ly)) {
957
+ const th = hx;
958
+ const tl = lx;
959
+ hx = hy;
960
+ lx = ly;
961
+ hy = th;
962
+ ly = tl;
963
+ }
964
+ const ex = hx >>> 20;
965
+ const ey = hy >>> 20;
966
+ let x = fromWords(hx, lx);
967
+ let y = fromWords(hy, ly);
968
+ if (ey === 0x7ff)
969
+ return y; // hypot(Infinity, NaN) is Infinity
970
+ if (ex === 0x7ff || (hy | ly) === 0)
971
+ return x;
972
+ if (ex - ey > 64)
973
+ return x + y;
974
+ // Scale so xh·xh cannot overflow and xl·xl cannot underflow.
975
+ let z = 1;
976
+ if (ex > 0x3ff + 510) {
977
+ z = TWO_700;
978
+ x *= TWO_M700;
979
+ y *= TWO_M700;
980
+ }
981
+ else if (ey < 0x3ff - 450) {
982
+ z = TWO_M700;
983
+ x *= TWO_700;
984
+ y *= TWO_700;
985
+ }
986
+ // x² and y² as exact double-double sums (Dekker split).
987
+ let c = x * SPLIT;
988
+ const xh = x - c + c;
989
+ const xl = x - xh;
990
+ const hxx = x * x;
991
+ const lxx = xh * xh - hxx + 2 * xh * xl + xl * xl;
992
+ c = y * SPLIT;
993
+ const yh = y - c + c;
994
+ const yl = y - yh;
995
+ const hyy = y * y;
996
+ const lyy = yh * yh - hyy + 2 * yh * yl + yl * yl;
997
+ return z * Math.sqrt(lyy + lxx + hyy + hxx);
998
+ };
999
+ // ---------------------------------------------------------------------------
1000
+ // Logarithms
1001
+ const LOG1 = 6.666666666666735130e-01;
1002
+ const LOG2 = 3.999999999940941908e-01;
1003
+ const LOG3 = 2.857142874366239149e-01;
1004
+ const LOG4 = 2.222219843214978396e-01;
1005
+ const LOG5 = 1.818357216161805012e-01;
1006
+ const LOG6 = 1.531383769920937332e-01;
1007
+ const LOG7 = 1.479819860511658591e-01;
1008
+ const TWO_54 = fromWords(0x43500000, 0);
1009
+ let logK = 0;
1010
+ let logF = 0;
1011
+ /**
1012
+ * Splits a positive finite non-unit `x` into `2^logK · (1 + logF)` with
1013
+ * `1 + logF` in [√2/2, √2]. Returns a result for the special cases
1014
+ * (±0, negative, NaN, Infinity), or `undefined` once split.
1015
+ */
1016
+ const logSplit = (xIn) => {
1017
+ let x = xIn;
1018
+ let hx = highWord(x) >>> 0;
1019
+ let k = 0;
1020
+ if (hx < 0x00100000 || hx >>> 31 !== 0) {
1021
+ if (x === 0)
1022
+ return -1 / (x * x); // log(±0) = -Infinity
1023
+ if (hx >>> 31 !== 0)
1024
+ return (x - x) / 0.0; // log(negative) = NaN
1025
+ k -= 54; // subnormal: scale up
1026
+ x *= TWO_54;
1027
+ hx = highWord(x) >>> 0;
1028
+ }
1029
+ else if (hx >= 0x7ff00000) {
1030
+ return x; // Infinity or NaN
1031
+ }
1032
+ else if (hx === 0x3ff00000 && lowWord(x) === 0) {
1033
+ return 0; // log(1) = +0
1034
+ }
1035
+ hx += 0x3ff00000 - 0x3fe6a09e;
1036
+ k += (hx >>> 20) - 0x3ff;
1037
+ hx = (hx & 0x000fffff) + 0x3fe6a09e;
1038
+ logK = k;
1039
+ logF = fromWords(hx, lowWord(x)) - 1.0;
1040
+ return undefined;
1041
+ };
1042
+ /** s·(hfsq + R) of the log kernel on f = logF. */
1043
+ const logKernel = (f, hfsq) => {
1044
+ const s = f / (2.0 + f);
1045
+ const z = s * s;
1046
+ const w = z * z;
1047
+ const t1 = w * (LOG2 + w * (LOG4 + w * LOG6));
1048
+ const t2 = z * (LOG1 + w * (LOG3 + w * (LOG5 + w * LOG7)));
1049
+ return s * (hfsq + t2 + t1);
1050
+ };
1051
+ export const log = (x) => {
1052
+ const special = logSplit(x);
1053
+ if (special !== undefined)
1054
+ return special;
1055
+ const f = logF;
1056
+ const hfsq = 0.5 * f * f;
1057
+ const dk = logK;
1058
+ return logKernel(f, hfsq) + dk * LN2_LO - hfsq + f + dk * LN2_HI;
1059
+ };
1060
+ const IVLN2HI = 1.44269504072144627571e+00;
1061
+ const IVLN2LO = 1.67517131648865118353e-10;
1062
+ export const log2 = (x) => {
1063
+ const special = logSplit(x);
1064
+ if (special !== undefined)
1065
+ return special;
1066
+ const f = logF;
1067
+ const hfsq = 0.5 * f * f;
1068
+ const hi = clearLow(f - hfsq);
1069
+ const lo = f - hi - hfsq + logKernel(f, hfsq);
1070
+ let valHi = hi * IVLN2HI;
1071
+ let valLo = (lo + hi) * IVLN2LO + lo * IVLN2HI;
1072
+ const y = logK;
1073
+ const w = y + valHi;
1074
+ valLo += (y - w) + valHi;
1075
+ valHi = w;
1076
+ return valLo + valHi;
1077
+ };
1078
+ const IVLN10HI = 4.34294481878168880939e-01;
1079
+ const IVLN10LO = 2.50829467116452752298e-11;
1080
+ const LOG10_2HI = 3.01029995663611771306e-01;
1081
+ const LOG10_2LO = 3.69423907715893078616e-13;
1082
+ export const log10 = (x) => {
1083
+ const special = logSplit(x);
1084
+ if (special !== undefined)
1085
+ return special;
1086
+ const f = logF;
1087
+ const hfsq = 0.5 * f * f;
1088
+ const hi = clearLow(f - hfsq);
1089
+ const lo = f - hi - hfsq + logKernel(f, hfsq);
1090
+ let valHi = hi * IVLN10HI;
1091
+ const dk = logK;
1092
+ const y = dk * LOG10_2HI;
1093
+ let valLo = dk * LOG10_2LO + (lo + hi) * IVLN10LO + lo * IVLN10HI;
1094
+ const w = y + valHi;
1095
+ valLo += (y - w) + valHi;
1096
+ valHi = w;
1097
+ return valLo + valHi;
1098
+ };
1099
+ export const log1p = (x) => {
1100
+ const hx = highWord(x) >>> 0;
1101
+ let k = 1;
1102
+ let c = 0;
1103
+ let f = 0;
1104
+ if (hx < 0x3fda827a || hx >>> 31 !== 0) {
1105
+ // 1 + x < √2
1106
+ if (hx >= 0xbff00000) {
1107
+ // x <= -1
1108
+ if (x === -1)
1109
+ return x / 0.0; // -Infinity
1110
+ return (x - x) / 0.0; // NaN
1111
+ }
1112
+ if (((hx << 1) >>> 0) < 0x79400000)
1113
+ return x; // |x| < 2^-53
1114
+ if (hx <= 0xbfd2bec4) {
1115
+ // √2/2 <= 1 + x < √2
1116
+ k = 0;
1117
+ f = x;
1118
+ }
1119
+ }
1120
+ else if (hx >= 0x7ff00000) {
1121
+ return x;
1122
+ }
1123
+ if (k !== 0) {
1124
+ const u = 1 + x;
1125
+ let hu = highWord(u) >>> 0;
1126
+ hu += 0x3ff00000 - 0x3fe6a09e;
1127
+ k = (hu >>> 20) - 0x3ff;
1128
+ // Correction term ~ log(1 + x) - log(u), avoiding underflow in c/u.
1129
+ if (k < 54) {
1130
+ c = k >= 2 ? 1 - (u - x) : x - (u - 1);
1131
+ c /= u;
1132
+ }
1133
+ else {
1134
+ c = 0;
1135
+ }
1136
+ hu = (hu & 0x000fffff) + 0x3fe6a09e;
1137
+ f = fromWords(hu, lowWord(u)) - 1;
1138
+ }
1139
+ const hfsq = 0.5 * f * f;
1140
+ const dk = k;
1141
+ return logKernel(f, hfsq) + (dk * LN2_LO + c) - hfsq + f + dk * LN2_HI;
1142
+ };
1143
+ // ---------------------------------------------------------------------------
1144
+ // Hyperbolic functions
1145
+ const EXPO2_KLN2 = fromWords(0x40962066, 0x151add8b); // 2043·ln2 (musl __expo2)
1146
+ const EXPO2_SCALE = fromWords((0x3ff + 1021) << 20, 0); // 2^1021
1147
+ /** exp(x)/2·sign for x > log(DBL_MAX): exp(x - 2043·ln2)·2^2042, without overflowing early. */
1148
+ const expo2 = (x, sign) => exp(x - EXPO2_KLN2) * (sign * EXPO2_SCALE) * EXPO2_SCALE;
1149
+ export const sinh = (x) => {
1150
+ const h = highWord(x) < 0 ? -0.5 : 0.5;
1151
+ const absx = Math.abs(x);
1152
+ const w = highWord(absx);
1153
+ if (w < 0x40862e42) {
1154
+ // |x| < log(DBL_MAX)
1155
+ const t = expm1(absx);
1156
+ if (w < 0x3ff00000) {
1157
+ if (w < 0x3ff00000 - (26 << 20))
1158
+ return x;
1159
+ return h * (2 * t - t * t / (t + 1));
1160
+ }
1161
+ return h * (t + t / (t + 1));
1162
+ }
1163
+ return expo2(absx, 2 * h); // overflow or NaN
1164
+ };
1165
+ export const cosh = (xIn) => {
1166
+ const x = Math.abs(xIn);
1167
+ const w = highWord(x);
1168
+ if (w < 0x3fe62e42) {
1169
+ // |x| < log(2)
1170
+ if (w < 0x3ff00000 - (26 << 20))
1171
+ return 1;
1172
+ const t = expm1(x);
1173
+ return 1 + t * t / (2 * (1 + t));
1174
+ }
1175
+ if (w < 0x40862e42) {
1176
+ // |x| < log(DBL_MAX)
1177
+ const t = exp(x);
1178
+ return 0.5 * (t + 1 / t);
1179
+ }
1180
+ return expo2(x, 1.0); // overflow or NaN
1181
+ };
1182
+ const LN2 = 6.93147180559945286227e-01;
1183
+ export const asinh = (x) => {
1184
+ const hx = highWord(x);
1185
+ const ix = hx & 0x7fffffff;
1186
+ if (ix >= 0x7ff00000)
1187
+ return x + x; // Infinity or NaN
1188
+ if (ix < 0x3e300000)
1189
+ return x; // |x| < 2^-28
1190
+ let w;
1191
+ if (ix > 0x41b00000) {
1192
+ // |x| > 2^28
1193
+ w = log(Math.abs(x)) + LN2;
1194
+ }
1195
+ else if (ix > 0x40000000) {
1196
+ // 2 < |x| <= 2^28
1197
+ const t = Math.abs(x);
1198
+ w = log(2.0 * t + 1 / (Math.sqrt(x * x + 1) + t));
1199
+ }
1200
+ else {
1201
+ // 2^-28 <= |x| <= 2
1202
+ const t = x * x;
1203
+ w = log1p(Math.abs(x) + t / (1 + Math.sqrt(1 + t)));
1204
+ }
1205
+ return hx > 0 ? w : -w;
1206
+ };
1207
+ export const acosh = (x) => {
1208
+ const hx = highWord(x);
1209
+ if (hx < 0x3ff00000)
1210
+ return (x - x) / (x - x); // x < 1 or negative NaN
1211
+ if (hx >= 0x41b00000) {
1212
+ // x > 2^28
1213
+ if (hx >= 0x7ff00000)
1214
+ return x + x; // Infinity or NaN
1215
+ return log(x) + LN2;
1216
+ }
1217
+ if (((hx - 0x3ff00000) | lowWord(x)) === 0)
1218
+ return 0; // acosh(1) = 0
1219
+ if (hx > 0x40000000) {
1220
+ // 2 < x <= 2^28
1221
+ const t = x * x;
1222
+ return log(2.0 * x - 1 / (x + Math.sqrt(t - 1)));
1223
+ }
1224
+ // 1 < x <= 2
1225
+ const t = x - 1;
1226
+ return log1p(t + Math.sqrt(2.0 * t + t * t));
1227
+ };
1228
+ export const atanh = (xIn) => {
1229
+ const hx = highWord(xIn);
1230
+ const lx = lowWord(xIn);
1231
+ const ix = hx & 0x7fffffff;
1232
+ if ((ix | ((lx | ((-lx) >>> 0)) >>> 31)) > 0x3ff00000)
1233
+ return (xIn - xIn) / (xIn - xIn); // |x| > 1 or NaN
1234
+ if (ix === 0x3ff00000)
1235
+ return xIn / 0; // ±Infinity
1236
+ if (ix < 0x3e300000)
1237
+ return xIn; // |x| < 2^-28
1238
+ const x = Math.abs(xIn);
1239
+ let t;
1240
+ if (ix < 0x3fe00000) {
1241
+ // |x| < 0.5
1242
+ t = x + x;
1243
+ t = 0.5 * log1p(t + t * x / (1 - x));
1244
+ }
1245
+ else {
1246
+ t = 0.5 * log1p((x + x) / (1 - x));
1247
+ }
1248
+ return hx >= 0 ? t : -t;
1249
+ };
1250
+ // ---------------------------------------------------------------------------
1251
+ // Cube root
1252
+ const B1 = 715094163; // (1023 - 1023/3 - 0.03306235651)·2^20
1253
+ const B2 = 696219795; // (1023 - 1023/3 - 54/3 - 0.03306235651)·2^20
1254
+ const CP0 = 1.87595182427177009643;
1255
+ const CP1 = -1.88497979543377169875;
1256
+ const CP2 = 1.621429720105354466140;
1257
+ const CP3 = -0.758397934778766047437;
1258
+ const CP4 = 0.145996192886612446982;
1259
+ export const cbrt = (x) => {
1260
+ const high = highWord(x);
1261
+ const sign = high & 0x80000000;
1262
+ let hx = high & 0x7fffffff;
1263
+ if (hx >= 0x7ff00000)
1264
+ return x + x; // Infinity or NaN
1265
+ // Rough cube root to 5 bits.
1266
+ if (hx < 0x00100000) {
1267
+ // zero or subnormal
1268
+ hx = highWord(x * TWO_54) & 0x7fffffff;
1269
+ if (hx === 0)
1270
+ return x; // ±0
1271
+ hx = Math.floor(hx / 3) + B2;
1272
+ }
1273
+ else {
1274
+ hx = Math.floor(hx / 3) + B1;
1275
+ }
1276
+ let t = fromWords((sign | hx) >>> 0, 0);
1277
+ // New cube root to 23 bits.
1278
+ let r = (t * t) * (t / x);
1279
+ t = t * ((CP0 + r * (CP1 + r * CP2)) + ((r * r) * r) * (CP3 + r * CP4));
1280
+ // Round t away from zero to 23 bits.
1281
+ const sum = lowWord(t) + 0x80000000;
1282
+ const carry = sum >= 0x100000000 ? 1 : 0;
1283
+ t = fromWords(((highWord(t) >>> 0) + carry) >>> 0, (sum & 0xc0000000) >>> 0);
1284
+ // One Newton step to 53 bits, error < 0.667 ulp.
1285
+ const s = t * t;
1286
+ r = x / s;
1287
+ const w = t + t;
1288
+ r = (r - t) / (w + r);
1289
+ return t + t * r;
1290
+ };
1291
+ //# sourceMappingURL=fdlibm.js.map