@obinexusmk2/hypernum 0.1.0 → 0.1.1
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/LICENSE +21 -21
- package/README.md +355 -256
- package/dist/index.cjs +4 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/types/{config → src/config}/config-loader.d.ts +0 -0
- package/dist/types/src/config/config-loader.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-parser.d.ts +0 -0
- package/dist/types/src/config/config-parser.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-resolver.d.ts +0 -0
- package/dist/types/src/config/config-resolver.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-source.d.ts +0 -0
- package/dist/types/src/config/config-source.d.ts.map +1 -0
- package/dist/types/{config → src/config}/index.d.ts +0 -0
- package/dist/types/src/config/index.d.ts.map +1 -0
- package/dist/types/{core → src/core}/common.d.ts +0 -0
- package/dist/types/src/core/common.d.ts.map +1 -0
- package/dist/types/{core → src/core}/config.d.ts +0 -0
- package/dist/types/src/core/config.d.ts.map +1 -0
- package/dist/types/{core → src/core}/constants.d.ts +0 -0
- package/dist/types/src/core/constants.d.ts.map +1 -0
- package/dist/types/{core → src/core}/errors.d.ts +0 -0
- package/dist/types/src/core/errors.d.ts.map +1 -0
- package/dist/types/{core → src/core}/hypernum.d.ts +0 -0
- package/dist/types/src/core/hypernum.d.ts.map +1 -0
- package/dist/types/{core → src/core}/index.d.ts +0 -0
- package/dist/types/src/core/index.d.ts.map +1 -0
- package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/arithmetic.d.ts +0 -0
- package/dist/types/src/operations/arithmetic.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/bitwise.d.ts +0 -0
- package/dist/types/src/operations/bitwise.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/comparison.d.ts +0 -0
- package/dist/types/src/operations/comparison.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/conversion.d.ts +0 -0
- package/dist/types/src/operations/conversion.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/factorial.d.ts +0 -0
- package/dist/types/src/operations/factorial.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/index.d.ts +0 -0
- package/dist/types/src/operations/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/power.d.ts +0 -0
- package/dist/types/src/operations/power.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/Heap.d.ts +0 -0
- package/dist/types/src/storage/Heap.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/index.d.ts +0 -0
- package/dist/types/src/storage/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/ackermann.d.ts +0 -0
- package/dist/types/src/structures/ackermann.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/big-array.d.ts +0 -0
- package/dist/types/src/structures/big-array.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/index.d.ts +0 -0
- package/dist/types/src/structures/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/number-tree.d.ts +0 -0
- package/dist/types/src/structures/number-tree.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/power-tower.d.ts +0 -0
- package/dist/types/src/structures/power-tower.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/formatting.d.ts +0 -0
- package/dist/types/src/utils/formatting.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/index.d.ts +0 -0
- package/dist/types/src/utils/index.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/parser.d.ts +0 -0
- package/dist/types/src/utils/parser.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/precision.d.ts +0 -0
- package/dist/types/src/utils/precision.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
- package/dist/types/src/utils/validation.d.ts.map +1 -0
- package/package.json +169 -164
- package/rollup.config.js +163 -161
- package/src/cli/hypernum.js +272 -0
- package/src/config/config-loader.ts +0 -0
- package/src/config/config-parser.ts +160 -160
- package/src/config/config-resolver.ts +0 -0
- package/src/config/config-source.ts +0 -0
- package/src/config/index.ts +0 -0
- package/src/core/common.ts +184 -184
- package/src/core/config.ts +392 -392
- package/src/core/constants.ts +101 -101
- package/src/core/errors.ts +202 -202
- package/src/core/hypernum.ts +240 -240
- package/src/core/index.ts +4 -4
- package/src/index.ts +179 -182
- package/src/operations/arithmetic.ts +332 -332
- package/src/operations/bitwise.ts +366 -366
- package/src/operations/comparison.ts +271 -271
- package/src/operations/conversion.ts +399 -399
- package/src/operations/factorial.ts +278 -278
- package/src/operations/index.ts +4 -4
- package/src/operations/power.ts +315 -315
- package/src/storage/Heap.ts +237 -237
- package/src/storage/index.ts +0 -0
- package/src/structures/ackermann.ts +232 -232
- package/src/structures/big-array.ts +305 -305
- package/src/structures/index.ts +3 -3
- package/src/structures/number-tree.ts +403 -403
- package/src/structures/power-tower.ts +277 -277
- package/src/types/common.d.ts +356 -356
- package/src/types/core.d.ts +160 -160
- package/src/types/index.d.ts +1 -1
- package/src/utils/formatting.ts +245 -245
- package/src/utils/index.ts +4 -4
- package/src/utils/parser.ts +244 -244
- package/src/utils/precision.ts +216 -216
- package/src/utils/validation.ts +182 -182
- package/tsconfig.json +83 -83
- package/dist/types/config/config-loader.d.ts.map +0 -1
- package/dist/types/config/config-parser.d.ts.map +0 -1
- package/dist/types/config/config-resolver.d.ts.map +0 -1
- package/dist/types/config/config-source.d.ts.map +0 -1
- package/dist/types/config/index.d.ts.map +0 -1
- package/dist/types/core/common.d.ts.map +0 -1
- package/dist/types/core/config.d.ts.map +0 -1
- package/dist/types/core/constants.d.ts.map +0 -1
- package/dist/types/core/errors.d.ts.map +0 -1
- package/dist/types/core/hypernum.d.ts.map +0 -1
- package/dist/types/core/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/operations/arithmetic.d.ts.map +0 -1
- package/dist/types/operations/bitwise.d.ts.map +0 -1
- package/dist/types/operations/comparison.d.ts.map +0 -1
- package/dist/types/operations/conversion.d.ts.map +0 -1
- package/dist/types/operations/factorial.d.ts.map +0 -1
- package/dist/types/operations/index.d.ts.map +0 -1
- package/dist/types/operations/power.d.ts.map +0 -1
- package/dist/types/storage/Heap.d.ts.map +0 -1
- package/dist/types/storage/index.d.ts.map +0 -1
- package/dist/types/structures/ackermann.d.ts.map +0 -1
- package/dist/types/structures/big-array.d.ts.map +0 -1
- package/dist/types/structures/index.d.ts.map +0 -1
- package/dist/types/structures/number-tree.d.ts.map +0 -1
- package/dist/types/structures/power-tower.d.ts.map +0 -1
- package/dist/types/utils/formatting.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/parser.d.ts.map +0 -1
- package/dist/types/utils/precision.d.ts.map +0 -1
- package/dist/types/utils/validation.d.ts.map +0 -1
package/src/operations/power.ts
CHANGED
|
@@ -1,316 +1,316 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Power operations module for Hypernum library
|
|
3
|
-
* Provides efficient implementations for exponentiation and related operations
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
toBigInt,
|
|
8
|
-
ValidationError,
|
|
9
|
-
OverflowError,
|
|
10
|
-
validateNonNegative,
|
|
11
|
-
checkPowerOverflow
|
|
12
|
-
} from '../utils/validation';
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
RoundingMode,
|
|
16
|
-
round,
|
|
17
|
-
} from '../utils/precision';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Options for power operations
|
|
21
|
-
*/
|
|
22
|
-
export interface PowerOptions {
|
|
23
|
-
/** Precision for decimal operations */
|
|
24
|
-
precision?: number;
|
|
25
|
-
/** Rounding mode for decimal operations */
|
|
26
|
-
roundingMode?: RoundingMode;
|
|
27
|
-
/** Whether to check for overflow */
|
|
28
|
-
checkOverflow?: boolean;
|
|
29
|
-
/** Maximum allowed computation steps */
|
|
30
|
-
maxSteps?: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const DEFAULT_OPTIONS: Required<PowerOptions> = {
|
|
34
|
-
precision: 0,
|
|
35
|
-
roundingMode: RoundingMode.HALF_EVEN,
|
|
36
|
-
checkOverflow: true,
|
|
37
|
-
maxSteps: 1000
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Raises a number to an integer power using binary exponentiation
|
|
42
|
-
*/
|
|
43
|
-
export function power(
|
|
44
|
-
baseValue: bigint | string | number,
|
|
45
|
-
exponentValue: bigint | string | number,
|
|
46
|
-
options: PowerOptions = {}
|
|
47
|
-
): bigint {
|
|
48
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
49
|
-
const bigBase = toBigInt(baseValue);
|
|
50
|
-
const bigExponent = toBigInt(exponentValue);
|
|
51
|
-
|
|
52
|
-
// Handle special cases
|
|
53
|
-
if (bigExponent === 0n) {
|
|
54
|
-
return 1n;
|
|
55
|
-
}
|
|
56
|
-
if (bigExponent === 1n) {
|
|
57
|
-
return bigBase;
|
|
58
|
-
}
|
|
59
|
-
if (bigBase === 0n && bigExponent < 0n) {
|
|
60
|
-
throw new ValidationError('Zero cannot be raised to a negative power');
|
|
61
|
-
}
|
|
62
|
-
if (bigBase === 0n) {
|
|
63
|
-
return 0n;
|
|
64
|
-
}
|
|
65
|
-
if (bigBase === 1n) {
|
|
66
|
-
return 1n;
|
|
67
|
-
}
|
|
68
|
-
if (bigBase === -1n) {
|
|
69
|
-
return bigExponent % 2n === 0n ? 1n : -1n;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Validate inputs
|
|
73
|
-
if (bigExponent < 0n) {
|
|
74
|
-
throw new ValidationError('Negative exponents not supported for integer power');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (opts.checkOverflow) {
|
|
78
|
-
checkPowerOverflow(bigBase, bigExponent);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Binary exponentiation algorithm
|
|
82
|
-
let result = 1n;
|
|
83
|
-
let base = bigBase;
|
|
84
|
-
let exponent = bigExponent;
|
|
85
|
-
let steps = 0;
|
|
86
|
-
|
|
87
|
-
while (exponent > 0n) {
|
|
88
|
-
if (steps++ > opts.maxSteps) {
|
|
89
|
-
throw new OverflowError('Power operation exceeded maximum computation steps');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (exponent & 1n) {
|
|
93
|
-
result *= base;
|
|
94
|
-
}
|
|
95
|
-
base *= base;
|
|
96
|
-
exponent >>= 1n;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (opts.precision > 0) {
|
|
100
|
-
return round(result, opts.precision, opts.roundingMode);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Calculates square root using Newton's method
|
|
108
|
-
*/
|
|
109
|
-
export function sqrt(
|
|
110
|
-
value: bigint | string | number,
|
|
111
|
-
options: PowerOptions = {}
|
|
112
|
-
): bigint {
|
|
113
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
114
|
-
const bigValue = toBigInt(value);
|
|
115
|
-
|
|
116
|
-
validateNonNegative(bigValue);
|
|
117
|
-
|
|
118
|
-
if (bigValue === 0n) {
|
|
119
|
-
return 0n;
|
|
120
|
-
}
|
|
121
|
-
if (bigValue === 1n) {
|
|
122
|
-
return 1n;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Newton's method for square root
|
|
126
|
-
let guess = bigValue >> 1n;
|
|
127
|
-
let lastGuess: bigint;
|
|
128
|
-
let steps = 0;
|
|
129
|
-
|
|
130
|
-
do {
|
|
131
|
-
if (steps++ > opts.maxSteps) {
|
|
132
|
-
throw new OverflowError('Square root operation exceeded maximum computation steps');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
lastGuess = guess;
|
|
136
|
-
guess = (guess + bigValue / guess) >> 1n;
|
|
137
|
-
} while (guess < lastGuess);
|
|
138
|
-
|
|
139
|
-
if (opts.precision > 0) {
|
|
140
|
-
return round(lastGuess, opts.precision, opts.roundingMode);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return lastGuess;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Calculates nth root using Newton's method
|
|
148
|
-
*/
|
|
149
|
-
export function nthRoot(
|
|
150
|
-
value: bigint | string | number,
|
|
151
|
-
n: bigint | string | number,
|
|
152
|
-
options: PowerOptions = {}
|
|
153
|
-
): bigint {
|
|
154
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
155
|
-
const bigValue = toBigInt(value);
|
|
156
|
-
const bigN = toBigInt(n);
|
|
157
|
-
|
|
158
|
-
validateNonNegative(bigValue);
|
|
159
|
-
if (bigN <= 0n) {
|
|
160
|
-
throw new ValidationError('Root index must be positive');
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (bigValue === 0n) {
|
|
164
|
-
return 0n;
|
|
165
|
-
}
|
|
166
|
-
if (bigValue === 1n) {
|
|
167
|
-
return 1n;
|
|
168
|
-
}
|
|
169
|
-
if (bigN === 1n) {
|
|
170
|
-
return bigValue;
|
|
171
|
-
}
|
|
172
|
-
if (bigN === 2n) {
|
|
173
|
-
return sqrt(bigValue, opts);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Newton's method for nth root
|
|
177
|
-
let guess = bigValue >> 1n;
|
|
178
|
-
let lastGuess: bigint;
|
|
179
|
-
let steps = 0;
|
|
180
|
-
|
|
181
|
-
const nMinus1 = bigN - 1n;
|
|
182
|
-
|
|
183
|
-
do {
|
|
184
|
-
if (steps++ > opts.maxSteps) {
|
|
185
|
-
throw new OverflowError('Nth root operation exceeded maximum computation steps');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
lastGuess = guess;
|
|
189
|
-
const powered = power(guess, nMinus1, opts);
|
|
190
|
-
guess = ((nMinus1 * guess) + (bigValue / powered)) / bigN;
|
|
191
|
-
} while (guess < lastGuess);
|
|
192
|
-
|
|
193
|
-
if (opts.precision > 0) {
|
|
194
|
-
return round(lastGuess, opts.precision, opts.roundingMode);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return lastGuess;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Calculates tetration (repeated exponentiation)
|
|
202
|
-
* a↑↑n = a^(a^(a^...)) (n times)
|
|
203
|
-
*/
|
|
204
|
-
export function tetration(
|
|
205
|
-
base: bigint | string | number,
|
|
206
|
-
height: bigint | string | number,
|
|
207
|
-
options: PowerOptions = {}
|
|
208
|
-
): bigint {
|
|
209
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
210
|
-
const bigBase = toBigInt(base);
|
|
211
|
-
const bigHeight = toBigInt(height);
|
|
212
|
-
|
|
213
|
-
validateNonNegative(bigHeight);
|
|
214
|
-
|
|
215
|
-
if (bigHeight === 0n) {
|
|
216
|
-
return 1n;
|
|
217
|
-
}
|
|
218
|
-
if (bigHeight === 1n) {
|
|
219
|
-
return bigBase;
|
|
220
|
-
}
|
|
221
|
-
if (bigBase === 0n) {
|
|
222
|
-
return bigHeight % 2n === 0n ? 1n : 0n;
|
|
223
|
-
}
|
|
224
|
-
if (bigBase === 1n) {
|
|
225
|
-
return 1n;
|
|
226
|
-
}
|
|
227
|
-
if (bigBase === 2n && bigHeight > 4n) {
|
|
228
|
-
throw new OverflowError('Tetration would overflow for base 2 and height > 4');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
let result = bigBase;
|
|
232
|
-
let steps = 0;
|
|
233
|
-
|
|
234
|
-
for (let i = 1n; i < bigHeight; i++) {
|
|
235
|
-
if (steps++ > opts.maxSteps) {
|
|
236
|
-
throw new OverflowError('Tetration operation exceeded maximum computation steps');
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
result = power(bigBase, result, opts);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (opts.precision > 0) {
|
|
243
|
-
return round(result, opts.precision, opts.roundingMode);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return result;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Calculates super-root (inverse tetration)
|
|
251
|
-
* Finds x where x↑↑n = value
|
|
252
|
-
*/
|
|
253
|
-
export function superRoot(
|
|
254
|
-
value: bigint | string | number,
|
|
255
|
-
height: bigint | string | number,
|
|
256
|
-
options: PowerOptions = {}
|
|
257
|
-
): bigint {
|
|
258
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
259
|
-
const bigValue = toBigInt(value);
|
|
260
|
-
const bigHeight = toBigInt(height);
|
|
261
|
-
|
|
262
|
-
validateNonNegative(bigHeight);
|
|
263
|
-
if (bigHeight === 0n) {
|
|
264
|
-
throw new ValidationError('Height cannot be zero for super-root');
|
|
265
|
-
}
|
|
266
|
-
if (bigValue < 1n) {
|
|
267
|
-
throw new ValidationError('Value must be at least 1 for super-root');
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if (bigValue === 1n) {
|
|
271
|
-
return 1n;
|
|
272
|
-
}
|
|
273
|
-
if (bigHeight === 1n) {
|
|
274
|
-
return bigValue;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Binary search for super-root
|
|
278
|
-
let left = 1n;
|
|
279
|
-
let right = bigValue;
|
|
280
|
-
let steps = 0;
|
|
281
|
-
|
|
282
|
-
while (left <= right) {
|
|
283
|
-
if (steps++ > opts.maxSteps) {
|
|
284
|
-
throw new OverflowError('Super-root operation exceeded maximum computation steps');
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const mid = (left + right) >> 1n;
|
|
288
|
-
try {
|
|
289
|
-
const test = tetration(mid, bigHeight, opts);
|
|
290
|
-
if (test === bigValue) {
|
|
291
|
-
return mid;
|
|
292
|
-
}
|
|
293
|
-
if (test < bigValue) {
|
|
294
|
-
left = mid + 1n;
|
|
295
|
-
} else {
|
|
296
|
-
right = mid - 1n;
|
|
297
|
-
}
|
|
298
|
-
} catch (error) {
|
|
299
|
-
right = mid - 1n;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
if (opts.precision > 0) {
|
|
304
|
-
return round(right, opts.precision, opts.roundingMode);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return right;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
export default {
|
|
311
|
-
power,
|
|
312
|
-
sqrt,
|
|
313
|
-
nthRoot,
|
|
314
|
-
tetration,
|
|
315
|
-
superRoot
|
|
1
|
+
/**
|
|
2
|
+
* Power operations module for Hypernum library
|
|
3
|
+
* Provides efficient implementations for exponentiation and related operations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
toBigInt,
|
|
8
|
+
ValidationError,
|
|
9
|
+
OverflowError,
|
|
10
|
+
validateNonNegative,
|
|
11
|
+
checkPowerOverflow
|
|
12
|
+
} from '../utils/validation';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
RoundingMode,
|
|
16
|
+
round,
|
|
17
|
+
} from '../utils/precision';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Options for power operations
|
|
21
|
+
*/
|
|
22
|
+
export interface PowerOptions {
|
|
23
|
+
/** Precision for decimal operations */
|
|
24
|
+
precision?: number;
|
|
25
|
+
/** Rounding mode for decimal operations */
|
|
26
|
+
roundingMode?: RoundingMode;
|
|
27
|
+
/** Whether to check for overflow */
|
|
28
|
+
checkOverflow?: boolean;
|
|
29
|
+
/** Maximum allowed computation steps */
|
|
30
|
+
maxSteps?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DEFAULT_OPTIONS: Required<PowerOptions> = {
|
|
34
|
+
precision: 0,
|
|
35
|
+
roundingMode: RoundingMode.HALF_EVEN,
|
|
36
|
+
checkOverflow: true,
|
|
37
|
+
maxSteps: 1000
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Raises a number to an integer power using binary exponentiation
|
|
42
|
+
*/
|
|
43
|
+
export function power(
|
|
44
|
+
baseValue: bigint | string | number,
|
|
45
|
+
exponentValue: bigint | string | number,
|
|
46
|
+
options: PowerOptions = {}
|
|
47
|
+
): bigint {
|
|
48
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
49
|
+
const bigBase = toBigInt(baseValue);
|
|
50
|
+
const bigExponent = toBigInt(exponentValue);
|
|
51
|
+
|
|
52
|
+
// Handle special cases
|
|
53
|
+
if (bigExponent === 0n) {
|
|
54
|
+
return 1n;
|
|
55
|
+
}
|
|
56
|
+
if (bigExponent === 1n) {
|
|
57
|
+
return bigBase;
|
|
58
|
+
}
|
|
59
|
+
if (bigBase === 0n && bigExponent < 0n) {
|
|
60
|
+
throw new ValidationError('Zero cannot be raised to a negative power');
|
|
61
|
+
}
|
|
62
|
+
if (bigBase === 0n) {
|
|
63
|
+
return 0n;
|
|
64
|
+
}
|
|
65
|
+
if (bigBase === 1n) {
|
|
66
|
+
return 1n;
|
|
67
|
+
}
|
|
68
|
+
if (bigBase === -1n) {
|
|
69
|
+
return bigExponent % 2n === 0n ? 1n : -1n;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Validate inputs
|
|
73
|
+
if (bigExponent < 0n) {
|
|
74
|
+
throw new ValidationError('Negative exponents not supported for integer power');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (opts.checkOverflow) {
|
|
78
|
+
checkPowerOverflow(bigBase, bigExponent);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Binary exponentiation algorithm
|
|
82
|
+
let result = 1n;
|
|
83
|
+
let base = bigBase;
|
|
84
|
+
let exponent = bigExponent;
|
|
85
|
+
let steps = 0;
|
|
86
|
+
|
|
87
|
+
while (exponent > 0n) {
|
|
88
|
+
if (steps++ > opts.maxSteps) {
|
|
89
|
+
throw new OverflowError('Power operation exceeded maximum computation steps');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (exponent & 1n) {
|
|
93
|
+
result *= base;
|
|
94
|
+
}
|
|
95
|
+
base *= base;
|
|
96
|
+
exponent >>= 1n;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (opts.precision > 0) {
|
|
100
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Calculates square root using Newton's method
|
|
108
|
+
*/
|
|
109
|
+
export function sqrt(
|
|
110
|
+
value: bigint | string | number,
|
|
111
|
+
options: PowerOptions = {}
|
|
112
|
+
): bigint {
|
|
113
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
114
|
+
const bigValue = toBigInt(value);
|
|
115
|
+
|
|
116
|
+
validateNonNegative(bigValue);
|
|
117
|
+
|
|
118
|
+
if (bigValue === 0n) {
|
|
119
|
+
return 0n;
|
|
120
|
+
}
|
|
121
|
+
if (bigValue === 1n) {
|
|
122
|
+
return 1n;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Newton's method for square root
|
|
126
|
+
let guess = bigValue >> 1n;
|
|
127
|
+
let lastGuess: bigint;
|
|
128
|
+
let steps = 0;
|
|
129
|
+
|
|
130
|
+
do {
|
|
131
|
+
if (steps++ > opts.maxSteps) {
|
|
132
|
+
throw new OverflowError('Square root operation exceeded maximum computation steps');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
lastGuess = guess;
|
|
136
|
+
guess = (guess + bigValue / guess) >> 1n;
|
|
137
|
+
} while (guess < lastGuess);
|
|
138
|
+
|
|
139
|
+
if (opts.precision > 0) {
|
|
140
|
+
return round(lastGuess, opts.precision, opts.roundingMode);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return lastGuess;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Calculates nth root using Newton's method
|
|
148
|
+
*/
|
|
149
|
+
export function nthRoot(
|
|
150
|
+
value: bigint | string | number,
|
|
151
|
+
n: bigint | string | number,
|
|
152
|
+
options: PowerOptions = {}
|
|
153
|
+
): bigint {
|
|
154
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
155
|
+
const bigValue = toBigInt(value);
|
|
156
|
+
const bigN = toBigInt(n);
|
|
157
|
+
|
|
158
|
+
validateNonNegative(bigValue);
|
|
159
|
+
if (bigN <= 0n) {
|
|
160
|
+
throw new ValidationError('Root index must be positive');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (bigValue === 0n) {
|
|
164
|
+
return 0n;
|
|
165
|
+
}
|
|
166
|
+
if (bigValue === 1n) {
|
|
167
|
+
return 1n;
|
|
168
|
+
}
|
|
169
|
+
if (bigN === 1n) {
|
|
170
|
+
return bigValue;
|
|
171
|
+
}
|
|
172
|
+
if (bigN === 2n) {
|
|
173
|
+
return sqrt(bigValue, opts);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Newton's method for nth root
|
|
177
|
+
let guess = bigValue >> 1n;
|
|
178
|
+
let lastGuess: bigint;
|
|
179
|
+
let steps = 0;
|
|
180
|
+
|
|
181
|
+
const nMinus1 = bigN - 1n;
|
|
182
|
+
|
|
183
|
+
do {
|
|
184
|
+
if (steps++ > opts.maxSteps) {
|
|
185
|
+
throw new OverflowError('Nth root operation exceeded maximum computation steps');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
lastGuess = guess;
|
|
189
|
+
const powered = power(guess, nMinus1, opts);
|
|
190
|
+
guess = ((nMinus1 * guess) + (bigValue / powered)) / bigN;
|
|
191
|
+
} while (guess < lastGuess);
|
|
192
|
+
|
|
193
|
+
if (opts.precision > 0) {
|
|
194
|
+
return round(lastGuess, opts.precision, opts.roundingMode);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return lastGuess;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Calculates tetration (repeated exponentiation)
|
|
202
|
+
* a↑↑n = a^(a^(a^...)) (n times)
|
|
203
|
+
*/
|
|
204
|
+
export function tetration(
|
|
205
|
+
base: bigint | string | number,
|
|
206
|
+
height: bigint | string | number,
|
|
207
|
+
options: PowerOptions = {}
|
|
208
|
+
): bigint {
|
|
209
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
210
|
+
const bigBase = toBigInt(base);
|
|
211
|
+
const bigHeight = toBigInt(height);
|
|
212
|
+
|
|
213
|
+
validateNonNegative(bigHeight);
|
|
214
|
+
|
|
215
|
+
if (bigHeight === 0n) {
|
|
216
|
+
return 1n;
|
|
217
|
+
}
|
|
218
|
+
if (bigHeight === 1n) {
|
|
219
|
+
return bigBase;
|
|
220
|
+
}
|
|
221
|
+
if (bigBase === 0n) {
|
|
222
|
+
return bigHeight % 2n === 0n ? 1n : 0n;
|
|
223
|
+
}
|
|
224
|
+
if (bigBase === 1n) {
|
|
225
|
+
return 1n;
|
|
226
|
+
}
|
|
227
|
+
if (bigBase === 2n && bigHeight > 4n) {
|
|
228
|
+
throw new OverflowError('Tetration would overflow for base 2 and height > 4');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let result = bigBase;
|
|
232
|
+
let steps = 0;
|
|
233
|
+
|
|
234
|
+
for (let i = 1n; i < bigHeight; i++) {
|
|
235
|
+
if (steps++ > opts.maxSteps) {
|
|
236
|
+
throw new OverflowError('Tetration operation exceeded maximum computation steps');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
result = power(bigBase, result, opts);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (opts.precision > 0) {
|
|
243
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Calculates super-root (inverse tetration)
|
|
251
|
+
* Finds x where x↑↑n = value
|
|
252
|
+
*/
|
|
253
|
+
export function superRoot(
|
|
254
|
+
value: bigint | string | number,
|
|
255
|
+
height: bigint | string | number,
|
|
256
|
+
options: PowerOptions = {}
|
|
257
|
+
): bigint {
|
|
258
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
259
|
+
const bigValue = toBigInt(value);
|
|
260
|
+
const bigHeight = toBigInt(height);
|
|
261
|
+
|
|
262
|
+
validateNonNegative(bigHeight);
|
|
263
|
+
if (bigHeight === 0n) {
|
|
264
|
+
throw new ValidationError('Height cannot be zero for super-root');
|
|
265
|
+
}
|
|
266
|
+
if (bigValue < 1n) {
|
|
267
|
+
throw new ValidationError('Value must be at least 1 for super-root');
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (bigValue === 1n) {
|
|
271
|
+
return 1n;
|
|
272
|
+
}
|
|
273
|
+
if (bigHeight === 1n) {
|
|
274
|
+
return bigValue;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Binary search for super-root
|
|
278
|
+
let left = 1n;
|
|
279
|
+
let right = bigValue;
|
|
280
|
+
let steps = 0;
|
|
281
|
+
|
|
282
|
+
while (left <= right) {
|
|
283
|
+
if (steps++ > opts.maxSteps) {
|
|
284
|
+
throw new OverflowError('Super-root operation exceeded maximum computation steps');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const mid = (left + right) >> 1n;
|
|
288
|
+
try {
|
|
289
|
+
const test = tetration(mid, bigHeight, opts);
|
|
290
|
+
if (test === bigValue) {
|
|
291
|
+
return mid;
|
|
292
|
+
}
|
|
293
|
+
if (test < bigValue) {
|
|
294
|
+
left = mid + 1n;
|
|
295
|
+
} else {
|
|
296
|
+
right = mid - 1n;
|
|
297
|
+
}
|
|
298
|
+
} catch (error) {
|
|
299
|
+
right = mid - 1n;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (opts.precision > 0) {
|
|
304
|
+
return round(right, opts.precision, opts.roundingMode);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return right;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export default {
|
|
311
|
+
power,
|
|
312
|
+
sqrt,
|
|
313
|
+
nthRoot,
|
|
314
|
+
tetration,
|
|
315
|
+
superRoot
|
|
316
316
|
};
|