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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.571",
3
+ "version": "0.0.572",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "scripts": {
6
6
  "tsc": "tsc",
@@ -45,34 +45,26 @@ const scalar_mul = ({ 0: _0, add }) => a => n => {
45
45
  }
46
46
 
47
47
  /**
48
- * Calculates the bit length of a given BigInt.
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
- * Negative inputs are converted to their absolute values before calculation. For example:
57
- * - `-255n` has a bit length of 8.
58
- * - `-1n` has a bit length of 1.
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. It can be positive, negative, or zero.
61
- * @returns {bigint} The bit length of the input BigInt.
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
- * This function determines the bit length in two phases:
65
- * 1. **Fast Doubling Phase:** Quickly identifies the range of the most significant bit using exponential steps.
66
- * 2. **Binary Search Phase:** Refines the result to precisely count all significant bits.
67
- *
68
- * The algorithm operates with logarithmic complexity, making it efficient for very large BigInts.
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 bitLen = v => {
71
- if (v <= 0n) {
72
- if (v === 0n) { return 0n }
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
- const log2 = (/** @type {bigint} */v) => v <= 0n ? -1n : bitLen(v) - 1n
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
- bitLen,
136
+ bitLength,
119
137
  }
@@ -1,4 +1,4 @@
1
- const { sum, abs, serialize, log2, bitLen } = require('./module.f.cjs')
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 = bitLen(0n)
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 = bitLen(1n << i)
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 = bitLen(1n << 1_000_000n)
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 = bitLen(-1n)
121
+ const s = bitLength(-1n)
122
122
  if (s !== 1n) { throw s }
123
123
  },
124
124
  () => {
125
- const s = bitLen(-2n)
125
+ const s = bitLength(-2n)
126
126
  if (s !== 2n) { throw s }
127
127
  },
128
128
  () => {
129
- const s = bitLen(-3n)
129
+ const s = bitLength(-3n)
130
130
  if (s !== 2n) { throw s }
131
131
  },
132
132
  () => {
133
- const s = bitLen(-4n)
133
+ const s = bitLength(-4n)
134
134
  if (s !== 3n) { throw s }
135
135
  },
136
136
  ]