functionalscript 0.0.503 → 0.0.504
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 +47 -0
- package/types/bigfloat/test.f.cjs +61 -0
- package/types/bigint/module.f.cjs +12 -0
- package/types/bigint/test.f.cjs +12 -2
package/package.json
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const compare = require('../function/compare/module.f.cjs')
|
|
2
|
+
const { abs, sign } = require('../bigint/module.f.cjs')
|
|
3
|
+
|
|
4
|
+
/** @typedef {readonly[bigint,number]} BigFloat */
|
|
5
|
+
|
|
6
|
+
const minSignificand = 0b10_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
|
|
7
|
+
|
|
8
|
+
/** @type {(value: BigFloat) => (min: bigint) => BigFloat} */
|
|
9
|
+
const increaseMantissa = ([m,e]) => min => {
|
|
10
|
+
if (m === 0n) {
|
|
11
|
+
return [m,e]
|
|
12
|
+
}
|
|
13
|
+
const s = sign(m)
|
|
14
|
+
m = abs(m)
|
|
15
|
+
while (true) {
|
|
16
|
+
if (m >= min) {
|
|
17
|
+
return [BigInt(s) * m, e]
|
|
18
|
+
}
|
|
19
|
+
m = m << 1n
|
|
20
|
+
e--
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @type {(base: bigint) => (exp: number) => bigint} */
|
|
25
|
+
const pow = base => exp => base ** BigInt(exp)
|
|
26
|
+
|
|
27
|
+
const pow5 = pow(5n)
|
|
28
|
+
|
|
29
|
+
/** @type {(dec: BigFloat) => BigFloat} */
|
|
30
|
+
const decToBin = dec => {
|
|
31
|
+
if (dec[0] === 0n) {
|
|
32
|
+
return [0n, 0]
|
|
33
|
+
}
|
|
34
|
+
if (dec[1] >= 0) {
|
|
35
|
+
/** @type {BigFloat} */
|
|
36
|
+
const bin = [dec[0] * pow5(dec[1]), dec[1]]
|
|
37
|
+
return increaseMantissa(bin)(minSignificand)
|
|
38
|
+
}
|
|
39
|
+
const p = pow5(-dec[1])
|
|
40
|
+
const inc = increaseMantissa(dec)(p * minSignificand)
|
|
41
|
+
return [inc[0] / p, inc[1]]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = {
|
|
45
|
+
/** @readonly */
|
|
46
|
+
decToBin,
|
|
47
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const { decToBin } = require('./module.f.cjs')
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
decToBin: [
|
|
5
|
+
() => {
|
|
6
|
+
const result = decToBin([0n, 0])
|
|
7
|
+
if (result[0] !== 0b00_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
8
|
+
if (result[1] !== 0) { throw result[1] }
|
|
9
|
+
},
|
|
10
|
+
() => {
|
|
11
|
+
const result = decToBin([0n, 10])
|
|
12
|
+
if (result[0] !== 0b00_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
13
|
+
if (result[1] !== 0) { throw result[1] }
|
|
14
|
+
},
|
|
15
|
+
() => {
|
|
16
|
+
const result = decToBin([0n, -10])
|
|
17
|
+
if (result[0] !== 0b00_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
18
|
+
if (result[1] !== 0) { throw result[1] }
|
|
19
|
+
},
|
|
20
|
+
() => {
|
|
21
|
+
const result = decToBin([1n, 0])
|
|
22
|
+
if (result[0] !== 0b10_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
23
|
+
if (result[1] !== -53) { throw result[1] }
|
|
24
|
+
},
|
|
25
|
+
() => {
|
|
26
|
+
const result = decToBin([1n, 1])
|
|
27
|
+
if (result[0] !== 0b10_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
28
|
+
if (result[1] !== -50) { throw result[1] }
|
|
29
|
+
},
|
|
30
|
+
() => {
|
|
31
|
+
const result = decToBin([1000n, -2])
|
|
32
|
+
if (result[0] !== 0b10_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
33
|
+
if (result[1] !== -50) { throw result[1] }
|
|
34
|
+
},
|
|
35
|
+
() => {
|
|
36
|
+
const result = decToBin([1n, -1])
|
|
37
|
+
if (result[0] !== 0b11_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011n) { throw result[0] }
|
|
38
|
+
if (result[1] !== -57) { throw result[1] }
|
|
39
|
+
},
|
|
40
|
+
() => {
|
|
41
|
+
const result = decToBin([-1n, 0])
|
|
42
|
+
if (result[0] !== -0b10_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
43
|
+
if (result[1] !== -53) { throw result[1] }
|
|
44
|
+
},
|
|
45
|
+
() => {
|
|
46
|
+
const result = decToBin([-1n, 1])
|
|
47
|
+
if (result[0] !== -0b10_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
48
|
+
if (result[1] !== -50) { throw result[1] }
|
|
49
|
+
},
|
|
50
|
+
() => {
|
|
51
|
+
const result = decToBin([-1000n, -2])
|
|
52
|
+
if (result[0] !== -0b10_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n) { throw result[0] }
|
|
53
|
+
if (result[1] !== -50) { throw result[1] }
|
|
54
|
+
},
|
|
55
|
+
() => {
|
|
56
|
+
const result = decToBin([-1n, -1])
|
|
57
|
+
if (result[0] !== -0b11_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011_0011n) { throw result[0] }
|
|
58
|
+
if (result[1] !== -57) { throw result[1] }
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const compare = require('../function/compare/module.f.cjs')
|
|
2
|
+
const { unsafeCmp } = compare
|
|
1
3
|
const { reduce } = require('../list/module.f.cjs')
|
|
2
4
|
|
|
3
5
|
/** @type {(a: bigint) => (b: bigint) => bigint} */
|
|
@@ -5,9 +7,19 @@ const addition = a => b => a + b
|
|
|
5
7
|
|
|
6
8
|
const sum = reduce(addition)(0n)
|
|
7
9
|
|
|
10
|
+
/** @type {(a: bigint) => bigint} */
|
|
11
|
+
const abs = a => a >= 0 ? a : -a
|
|
12
|
+
|
|
13
|
+
/** @type {(a: bigint) => compare.Sign} */
|
|
14
|
+
const sign = a => unsafeCmp(a)(0n)
|
|
15
|
+
|
|
8
16
|
module.exports = {
|
|
9
17
|
/** @readonly */
|
|
10
18
|
addition,
|
|
11
19
|
/** @readonly */
|
|
12
20
|
sum,
|
|
21
|
+
/** @readonly */
|
|
22
|
+
abs,
|
|
23
|
+
/** @readonly */
|
|
24
|
+
sign,
|
|
13
25
|
}
|
package/types/bigint/test.f.cjs
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
const { sum } = require('./module.f.cjs')
|
|
1
|
+
const { sum, abs } = require('./module.f.cjs')
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
4
|
sum: () => {
|
|
5
5
|
const result = sum([2n, 3n, 4n, 5n])
|
|
6
6
|
if (result !== 14n) { throw result }
|
|
7
|
-
}
|
|
7
|
+
},
|
|
8
|
+
abs: [
|
|
9
|
+
() => {
|
|
10
|
+
const result = abs(10n)
|
|
11
|
+
if (result !== 10n) { throw result }
|
|
12
|
+
},
|
|
13
|
+
() => {
|
|
14
|
+
const result = abs(-10n)
|
|
15
|
+
if (result !== 10n) { throw result }
|
|
16
|
+
}
|
|
17
|
+
]
|
|
8
18
|
}
|