functionalscript 0.0.513 → 0.0.514
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.
- package/package.json +1 -1
- package/types/bigfloat/module.f.cjs +25 -13
package/package.json
CHANGED
|
@@ -4,6 +4,8 @@ const { todo } = require('../../dev/module.f.cjs')
|
|
|
4
4
|
|
|
5
5
|
/** @typedef {readonly[bigint,number]} BigFloat */
|
|
6
6
|
|
|
7
|
+
/** @typedef {readonly[BigFloat,bigint]} BigFloatWithRemainder */
|
|
8
|
+
|
|
7
9
|
const twoPow53 = 0b0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
|
|
8
10
|
const twoPow54 = 0b0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
|
|
9
11
|
|
|
@@ -44,20 +46,25 @@ const pow = base => exp => base ** BigInt(exp)
|
|
|
44
46
|
|
|
45
47
|
const pow5 = pow(5n)
|
|
46
48
|
|
|
47
|
-
/** @type {(
|
|
48
|
-
const
|
|
49
|
+
/** @type {(b: BigFloat) => (mul: bigint) => BigFloat} */
|
|
50
|
+
const multiply = ([m, e]) => mul => [m * mul, e]
|
|
51
|
+
|
|
52
|
+
/** @type {(b: BigFloat) => (div: bigint) => BigFloatWithRemainder} */
|
|
53
|
+
const divide = ([m, e]) => div => [[m / div, e], m % div]
|
|
54
|
+
|
|
55
|
+
/** @type {(b: BigFloatWithRemainder) => BigFloat} */
|
|
56
|
+
const round53 = ([[m, e], r]) => {
|
|
49
57
|
const mabs = abs(m)
|
|
50
|
-
const q = mabs / div
|
|
51
58
|
const s = BigInt(sign(m))
|
|
52
|
-
const [
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
const odd =
|
|
58
|
-
return [
|
|
59
|
+
const [m54, e54] = decreaseMantissa([mabs, e])(twoPow54)
|
|
60
|
+
const o54 = m54 & 1n
|
|
61
|
+
const m53 = m54 >> 1n
|
|
62
|
+
const e53 = e54 + 1
|
|
63
|
+
if (o54 === 1n && r === 0n && mabs === m54 >> BigInt(e - e54)) {
|
|
64
|
+
const odd = m53 & 1n
|
|
65
|
+
return multiply([m53 + odd, e53])(s)
|
|
59
66
|
}
|
|
60
|
-
return [
|
|
67
|
+
return multiply([m53 + o54, e53])(s)
|
|
61
68
|
}
|
|
62
69
|
|
|
63
70
|
/** @type {(dec: BigFloat) => BigFloat} */
|
|
@@ -68,11 +75,16 @@ const decToBin = dec => {
|
|
|
68
75
|
if (dec[1] >= 0) {
|
|
69
76
|
/** @type {BigFloat} */
|
|
70
77
|
const bin = [dec[0] * pow5(dec[1]), dec[1]]
|
|
71
|
-
|
|
78
|
+
const inc = increaseMantissa(bin)(twoPow53)
|
|
79
|
+
return round53([inc, 0n])
|
|
72
80
|
}
|
|
73
81
|
const p = pow5(-dec[1])
|
|
74
82
|
const [m, e] = increaseMantissa(dec)(p * twoPow53)
|
|
75
|
-
|
|
83
|
+
const mAbs = abs(m)
|
|
84
|
+
const s = BigInt(sign(m))
|
|
85
|
+
const qr = divide([mAbs, e])(p)
|
|
86
|
+
const r53 = round53(qr)
|
|
87
|
+
return multiply(r53)(s)
|
|
76
88
|
}
|
|
77
89
|
|
|
78
90
|
module.exports = {
|