functionalscript 0.0.571 → 0.0.572
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/bigint/module.f.cjs +43 -25
- package/types/bigint/test.f.cjs +8 -8
package/package.json
CHANGED
|
@@ -45,34 +45,26 @@ const scalar_mul = ({ 0: _0, add }) => a => n => {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* Calculates the
|
|
49
|
-
*
|
|
50
|
-
* The bit length of a number is the number of bits required to represent it in binary,
|
|
51
|
-
* excluding leading zeros. For example:
|
|
52
|
-
* - `0n` (binary `...0`) has a bit length of 0.
|
|
53
|
-
* - `1n` (binary `...0_1`) has a bit length of 1.
|
|
54
|
-
* - `255n` (binary `...0_11111111`) has a bit length of 8.
|
|
48
|
+
* Calculates the base-2 logarithm (floor).
|
|
55
49
|
*
|
|
56
|
-
*
|
|
57
|
-
* -
|
|
58
|
-
* -
|
|
50
|
+
* This function returns the integer part of the logarithm. For example:
|
|
51
|
+
* - `log2(1n)` returns `0n`,
|
|
52
|
+
* - `log2(2n)` returns `1n`,
|
|
53
|
+
* - `log2(15n)` returns `3n`.
|
|
59
54
|
*
|
|
60
|
-
* @param {bigint} v - The input BigInt.
|
|
61
|
-
* @returns {bigint} The
|
|
55
|
+
* @param {bigint} v - The input BigInt.
|
|
56
|
+
* @returns {bigint} The base-2 logarithm (floor) of the input BigInt, or `-1n` if the input is less than or equal to 0.
|
|
62
57
|
*
|
|
63
58
|
* @remarks
|
|
64
|
-
*
|
|
65
|
-
* 1. **Fast Doubling Phase:**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
59
|
+
* The function operates in two phases:
|
|
60
|
+
* 1. **Fast Doubling Phase:** Uses exponential steps to quickly narrow down the range
|
|
61
|
+
* of the most significant bit.
|
|
62
|
+
* 2. **Binary Search Phase:** Refines the result by halving the step size and incrementally
|
|
63
|
+
* determining the exact value of the logarithm.
|
|
69
64
|
*/
|
|
70
|
-
const
|
|
71
|
-
if (v <= 0n) {
|
|
72
|
-
|
|
73
|
-
v = -v
|
|
74
|
-
}
|
|
75
|
-
let result = 1n
|
|
65
|
+
const log2 = v => {
|
|
66
|
+
if (v <= 0n) { return -1n }
|
|
67
|
+
let result = 0n
|
|
76
68
|
let i = 1n
|
|
77
69
|
while (true) {
|
|
78
70
|
const n = v >> i
|
|
@@ -97,7 +89,33 @@ const bitLen = v => {
|
|
|
97
89
|
return result
|
|
98
90
|
}
|
|
99
91
|
|
|
100
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Calculates the bit length of a given BigInt.
|
|
94
|
+
*
|
|
95
|
+
* The bit length of a number is the number of bits required to represent its absolute value in binary,
|
|
96
|
+
* excluding leading zeros. For example:
|
|
97
|
+
* - `0n` has a bit length of 0 (it has no bits).
|
|
98
|
+
* - `1n` (binary `1`) has a bit length of 1.
|
|
99
|
+
* - `255n` (binary `11111111`) has a bit length of 8.
|
|
100
|
+
* - `-255n` (absolute value `255`, binary `11111111`) also has a bit length of 8.
|
|
101
|
+
*
|
|
102
|
+
* The function handles both positive and negative numbers. For negative inputs, the bit length is calculated
|
|
103
|
+
* based on the absolute value of the number. Zero has a bit length of 0.
|
|
104
|
+
*
|
|
105
|
+
* @param {bigint} v - The input BigInt.
|
|
106
|
+
* @returns {bigint} The bit length of the input BigInt.
|
|
107
|
+
*
|
|
108
|
+
* @remark
|
|
109
|
+
* The function uses the `log2` function to calculate the position of the most significant bit(MSB)
|
|
110
|
+
* and adds `1n` to account for the MSB itself.For negative numbers, the absolute value is used.
|
|
111
|
+
*/
|
|
112
|
+
const bitLength = v => {
|
|
113
|
+
if (v <= 0n) {
|
|
114
|
+
if (v === 0n) { return 0n }
|
|
115
|
+
v = -v
|
|
116
|
+
}
|
|
117
|
+
return log2(v) + 1n
|
|
118
|
+
}
|
|
101
119
|
|
|
102
120
|
module.exports = {
|
|
103
121
|
/** @readonly */
|
|
@@ -115,5 +133,5 @@ module.exports = {
|
|
|
115
133
|
/** @readonly */
|
|
116
134
|
log2,
|
|
117
135
|
/** @readonly */
|
|
118
|
-
|
|
136
|
+
bitLength,
|
|
119
137
|
}
|
package/types/bigint/test.f.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { sum, abs, serialize, log2,
|
|
1
|
+
const { sum, abs, serialize, log2, bitLength } = require('./module.f.cjs')
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
4
|
sum: () => {
|
|
@@ -101,36 +101,36 @@ module.exports = {
|
|
|
101
101
|
},
|
|
102
102
|
bitLen: {
|
|
103
103
|
0: () => {
|
|
104
|
-
const s =
|
|
104
|
+
const s = bitLength(0n)
|
|
105
105
|
if (s !== 0n) { throw s }
|
|
106
106
|
},
|
|
107
107
|
m: () => {
|
|
108
108
|
let i = 0n
|
|
109
109
|
while (i < 10_000n) {
|
|
110
|
-
const s =
|
|
110
|
+
const s = bitLength(1n << i)
|
|
111
111
|
if (s !== i + 1n) { throw [s, i] }
|
|
112
112
|
i += 1n
|
|
113
113
|
}
|
|
114
114
|
},
|
|
115
115
|
big: () => {
|
|
116
|
-
const s =
|
|
116
|
+
const s = bitLength(1n << 1_000_000n)
|
|
117
117
|
if (s !== 1_000_001n) { throw s }
|
|
118
118
|
},
|
|
119
119
|
neg: [
|
|
120
120
|
() => {
|
|
121
|
-
const s =
|
|
121
|
+
const s = bitLength(-1n)
|
|
122
122
|
if (s !== 1n) { throw s }
|
|
123
123
|
},
|
|
124
124
|
() => {
|
|
125
|
-
const s =
|
|
125
|
+
const s = bitLength(-2n)
|
|
126
126
|
if (s !== 2n) { throw s }
|
|
127
127
|
},
|
|
128
128
|
() => {
|
|
129
|
-
const s =
|
|
129
|
+
const s = bitLength(-3n)
|
|
130
130
|
if (s !== 2n) { throw s }
|
|
131
131
|
},
|
|
132
132
|
() => {
|
|
133
|
-
const s =
|
|
133
|
+
const s = bitLength(-4n)
|
|
134
134
|
if (s !== 3n) { throw s }
|
|
135
135
|
},
|
|
136
136
|
]
|