@obinexusmk2/hypernum 0.1.0

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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +256 -0
  3. package/dist/index.cjs +3425 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +1449 -0
  6. package/dist/index.js +3284 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/index.umd.js +8 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/dist/types/config/config-loader.d.ts +56 -0
  11. package/dist/types/config/config-loader.d.ts.map +1 -0
  12. package/dist/types/config/config-parser.d.ts +28 -0
  13. package/dist/types/config/config-parser.d.ts.map +1 -0
  14. package/dist/types/config/config-resolver.d.ts +21 -0
  15. package/dist/types/config/config-resolver.d.ts.map +1 -0
  16. package/dist/types/config/config-source.d.ts +27 -0
  17. package/dist/types/config/config-source.d.ts.map +1 -0
  18. package/dist/types/config/index.d.ts +68 -0
  19. package/dist/types/config/index.d.ts.map +1 -0
  20. package/dist/types/core/common.d.ts +169 -0
  21. package/dist/types/core/common.d.ts.map +1 -0
  22. package/dist/types/core/config.d.ts +197 -0
  23. package/dist/types/core/config.d.ts.map +1 -0
  24. package/dist/types/core/constants.d.ts +88 -0
  25. package/dist/types/core/constants.d.ts.map +1 -0
  26. package/dist/types/core/errors.d.ts +97 -0
  27. package/dist/types/core/errors.d.ts.map +1 -0
  28. package/dist/types/core/hypernum.d.ts +60 -0
  29. package/dist/types/core/hypernum.d.ts.map +1 -0
  30. package/dist/types/core/index.d.ts +6 -0
  31. package/dist/types/core/index.d.ts.map +1 -0
  32. package/dist/types/index.d.ts +33 -0
  33. package/dist/types/index.d.ts.map +1 -0
  34. package/dist/types/operations/arithmetic.d.ts +72 -0
  35. package/dist/types/operations/arithmetic.d.ts.map +1 -0
  36. package/dist/types/operations/bitwise.d.ts +98 -0
  37. package/dist/types/operations/bitwise.d.ts.map +1 -0
  38. package/dist/types/operations/comparison.d.ts +94 -0
  39. package/dist/types/operations/comparison.d.ts.map +1 -0
  40. package/dist/types/operations/conversion.d.ts +79 -0
  41. package/dist/types/operations/conversion.d.ts.map +1 -0
  42. package/dist/types/operations/factorial.d.ts +58 -0
  43. package/dist/types/operations/factorial.d.ts.map +1 -0
  44. package/dist/types/operations/index.d.ts +6 -0
  45. package/dist/types/operations/index.d.ts.map +1 -0
  46. package/dist/types/operations/power.d.ts +49 -0
  47. package/dist/types/operations/power.d.ts.map +1 -0
  48. package/dist/types/storage/Heap.d.ts +95 -0
  49. package/dist/types/storage/Heap.d.ts.map +1 -0
  50. package/dist/types/storage/index.d.ts +2 -0
  51. package/dist/types/storage/index.d.ts.map +1 -0
  52. package/dist/types/structures/ackermann.d.ts +74 -0
  53. package/dist/types/structures/ackermann.d.ts.map +1 -0
  54. package/dist/types/structures/big-array.d.ts +102 -0
  55. package/dist/types/structures/big-array.d.ts.map +1 -0
  56. package/dist/types/structures/index.d.ts +5 -0
  57. package/dist/types/structures/index.d.ts.map +1 -0
  58. package/dist/types/structures/number-tree.d.ts +114 -0
  59. package/dist/types/structures/number-tree.d.ts.map +1 -0
  60. package/dist/types/structures/power-tower.d.ts +74 -0
  61. package/dist/types/structures/power-tower.d.ts.map +1 -0
  62. package/dist/types/utils/formatting.d.ts +45 -0
  63. package/dist/types/utils/formatting.d.ts.map +1 -0
  64. package/dist/types/utils/index.d.ts +5 -0
  65. package/dist/types/utils/index.d.ts.map +1 -0
  66. package/dist/types/utils/parser.d.ts +39 -0
  67. package/dist/types/utils/parser.d.ts.map +1 -0
  68. package/dist/types/utils/precision.d.ts +57 -0
  69. package/dist/types/utils/precision.d.ts.map +1 -0
  70. package/dist/types/utils/validation.d.ts +28 -0
  71. package/dist/types/utils/validation.d.ts.map +1 -0
  72. package/package.json +164 -0
  73. package/rollup.config.js +162 -0
  74. package/src/config/config-loader.ts +226 -0
  75. package/src/config/config-parser.ts +161 -0
  76. package/src/config/config-resolver.ts +52 -0
  77. package/src/config/config-source.ts +32 -0
  78. package/src/config/index.ts +159 -0
  79. package/src/core/common.ts +185 -0
  80. package/src/core/config.ts +393 -0
  81. package/src/core/constants.ts +102 -0
  82. package/src/core/errors.ts +203 -0
  83. package/src/core/hypernum.ts +241 -0
  84. package/src/core/index.ts +5 -0
  85. package/src/index.ts +183 -0
  86. package/src/operations/arithmetic.ts +333 -0
  87. package/src/operations/bitwise.ts +367 -0
  88. package/src/operations/comparison.ts +272 -0
  89. package/src/operations/conversion.ts +400 -0
  90. package/src/operations/factorial.ts +279 -0
  91. package/src/operations/index.ts +5 -0
  92. package/src/operations/power.ts +316 -0
  93. package/src/storage/Heap.ts +238 -0
  94. package/src/storage/index.ts +1 -0
  95. package/src/structures/ackermann.ts +233 -0
  96. package/src/structures/big-array.ts +306 -0
  97. package/src/structures/index.ts +4 -0
  98. package/src/structures/number-tree.ts +404 -0
  99. package/src/structures/power-tower.ts +278 -0
  100. package/src/types/common.d.ts +357 -0
  101. package/src/types/core.d.ts +161 -0
  102. package/src/types/index.d.ts +2 -0
  103. package/src/utils/formatting.ts +246 -0
  104. package/src/utils/index.ts +4 -0
  105. package/src/utils/parser.ts +245 -0
  106. package/src/utils/precision.ts +217 -0
  107. package/src/utils/validation.ts +183 -0
  108. package/tsconfig.json +84 -0
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Precision utilities for Hypernum library
3
+ * Provides functions for handling decimal precision and rounding operations
4
+ */
5
+
6
+ import { ValidationError } from './validation';
7
+
8
+ /**
9
+ * Rounding modes for decimal operations
10
+ */
11
+ export enum RoundingMode {
12
+ FLOOR = 'FLOOR', // Round towards negative infinity
13
+ CEIL = 'CEIL', // Round towards positive infinity
14
+ DOWN = 'DOWN', // Round towards zero
15
+ UP = 'UP', // Round away from zero
16
+ HALF_EVEN = 'HALF_EVEN', // Round to nearest even number when tied (Banker's rounding)
17
+ HALF_UP = 'HALF_UP', // Round up when tied
18
+ HALF_DOWN = 'HALF_DOWN', // Round down when tied
19
+ }
20
+
21
+ /**
22
+ * Scale a bigint by a power of 10
23
+ */
24
+ export const scaleByPowerOfTen = (value: bigint, power: number): bigint => {
25
+ if (power === 0) return value;
26
+ if (power > 0) {
27
+ return value * (BigInt(10) ** BigInt(power));
28
+ }
29
+ return value / (BigInt(10) ** BigInt(-power));
30
+ };
31
+
32
+ /**
33
+ * Round a number according to specified mode and precision
34
+ */
35
+ export const round = (
36
+ value: bigint,
37
+ precision: number = 0,
38
+ mode: RoundingMode = RoundingMode.HALF_EVEN
39
+ ): bigint => {
40
+ if (precision < 0) {
41
+ throw new ValidationError('Precision must be non-negative');
42
+ }
43
+
44
+ if (precision === 0) {
45
+ return value;
46
+ }
47
+
48
+ const scale = BigInt(10) ** BigInt(precision);
49
+ const scaled = value / scale;
50
+ const remainder = value % scale;
51
+
52
+ switch (mode) {
53
+ case RoundingMode.FLOOR:
54
+ return scaled * scale;
55
+
56
+ case RoundingMode.CEIL:
57
+ return remainder > 0n ? (scaled + 1n) * scale : scaled * scale;
58
+
59
+ case RoundingMode.DOWN:
60
+ return value >= 0n ? scaled * scale : (scaled - 1n) * scale;
61
+
62
+ case RoundingMode.UP:
63
+ return value >= 0n ? (scaled + 1n) * scale : scaled * scale;
64
+
65
+ case RoundingMode.HALF_UP:
66
+ return remainder >= scale / 2n ? (scaled + 1n) * scale : scaled * scale;
67
+
68
+ case RoundingMode.HALF_DOWN:
69
+ return remainder > scale / 2n ? (scaled + 1n) * scale : scaled * scale;
70
+
71
+ case RoundingMode.HALF_EVEN:
72
+ if (remainder === scale / 2n) {
73
+ return scaled % 2n === 0n ? scaled * scale : (scaled + 1n) * scale;
74
+ }
75
+ return remainder > scale / 2n ? (scaled + 1n) * scale : scaled * scale;
76
+
77
+ default:
78
+ throw new ValidationError('Invalid rounding mode');
79
+ }
80
+ };
81
+
82
+ /**
83
+ * Calculate precision required to represent a number without loss
84
+ */
85
+ export const calculateRequiredPrecision = (value: bigint): number => {
86
+ if (value === 0n) return 0;
87
+
88
+ const str = value.toString();
89
+ const nonZeroIndex = str.split('').reverse().findIndex(char => char !== '0');
90
+ return nonZeroIndex === -1 ? 0 : nonZeroIndex;
91
+ };
92
+
93
+ /**
94
+ * Normalize two numbers to the same precision
95
+ */
96
+ export const normalizePrecision = (
97
+ a: bigint,
98
+ b: bigint,
99
+ precisionA: number,
100
+ precisionB: number
101
+ ): [bigint, bigint] => {
102
+ const targetPrecision = Math.max(precisionA, precisionB);
103
+
104
+ const scaledA = scaleByPowerOfTen(a, targetPrecision - precisionA);
105
+ const scaledB = scaleByPowerOfTen(b, targetPrecision - precisionB);
106
+
107
+ return [scaledA, scaledB];
108
+ };
109
+
110
+ /**
111
+ * Scale a division operation to achieve desired precision
112
+ */
113
+ export const scaledDivision = (
114
+ numerator: bigint,
115
+ denominator: bigint,
116
+ precision: number,
117
+ roundingMode: RoundingMode = RoundingMode.HALF_EVEN
118
+ ): bigint => {
119
+ if (denominator === 0n) {
120
+ throw new ValidationError('Division by zero');
121
+ }
122
+
123
+ if (precision < 0) {
124
+ throw new ValidationError('Precision must be non-negative');
125
+ }
126
+
127
+ // Scale up numerator to handle desired precision
128
+ const scaledNumerator = scaleByPowerOfTen(numerator, precision);
129
+ const quotient = scaledNumerator / denominator;
130
+
131
+ return round(quotient, 0, roundingMode);
132
+ };
133
+
134
+ /**
135
+ * Calculate the number of significant digits
136
+ */
137
+ export const significantDigits = (value: bigint): number => {
138
+ const nonZeroPattern = /[1-9]/;
139
+ const str = value.toString();
140
+ const firstSignificant = str.search(nonZeroPattern);
141
+ if (firstSignificant === -1) return 0;
142
+
143
+ const lastSignificant = str.split('').reverse().findIndex(char => char !== '0');
144
+ return str.length - firstSignificant - (lastSignificant === -1 ? 0 : lastSignificant);
145
+ };
146
+
147
+ /**
148
+ * Truncate to specified number of significant digits
149
+ */
150
+ export const truncateToSignificantDigits = (
151
+ value: bigint,
152
+ digits: number,
153
+ roundingMode: RoundingMode = RoundingMode.HALF_EVEN
154
+ ): bigint => {
155
+ if (digits <= 0) {
156
+ throw new ValidationError('Number of significant digits must be positive');
157
+ }
158
+
159
+ const currentDigits = significantDigits(value);
160
+
161
+ if (currentDigits <= digits) {
162
+ return value;
163
+ }
164
+
165
+ const scale = currentDigits - digits;
166
+ return round(value, scale, roundingMode);
167
+ };
168
+
169
+ /**
170
+ * Check if two numbers are equal within a specified precision
171
+ */
172
+ export const equalWithinPrecision = (
173
+ a: bigint,
174
+ b: bigint,
175
+ precision: number
176
+ ): boolean => {
177
+ const diff = a - b;
178
+ const tolerance = BigInt(10) ** BigInt(precision);
179
+ return diff.toString().length <= tolerance.toString().length;
180
+ };
181
+
182
+ /**
183
+ * Get the fractional part of a number at a given precision
184
+ */
185
+ export const getFractionalPart = (
186
+ value: bigint,
187
+ precision: number
188
+ ): bigint => {
189
+ if (precision <= 0) return 0n;
190
+
191
+ const scale = BigInt(10) ** BigInt(precision);
192
+ return value % scale;
193
+ };
194
+
195
+ /**
196
+ * Format a number with exact precision (no rounding)
197
+ */
198
+ export const toExactPrecision = (value: bigint, precision: number): string => {
199
+ if (precision < 0) {
200
+ throw new ValidationError('Precision must be non-negative');
201
+ }
202
+
203
+ let str = value.toString();
204
+ const isNegative = str.startsWith('-');
205
+ if (isNegative) {
206
+ str = str.slice(1);
207
+ }
208
+
209
+ while (str.length <= precision) {
210
+ str = '0' + str;
211
+ }
212
+
213
+ const integerPart = str.slice(0, -precision) || '0';
214
+ const fractionalPart = str.slice(-precision);
215
+
216
+ return `${isNegative ? '-' : ''}${integerPart}.${fractionalPart}`;
217
+ };
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Validation utilities for Hypernum library
3
+ * Provides type checking and validation functions for large number operations
4
+ */
5
+
6
+ // Custom error types for validation
7
+ export class ValidationError extends Error {
8
+ constructor(message: string) {
9
+ super(message);
10
+ this.name = 'ValidationError';
11
+ }
12
+ }
13
+
14
+ export class OverflowError extends Error {
15
+ constructor(message: string) {
16
+ super(message);
17
+ this.name = 'OverflowError';
18
+ }
19
+ }
20
+
21
+ // Type guards
22
+ export const isBigInt = (value: unknown): value is bigint => {
23
+ return typeof value === 'bigint';
24
+ };
25
+
26
+ export const isValidNumberString = (value: string): boolean => {
27
+ return /^-?\d+$/.test(value);
28
+ };
29
+
30
+ export const isValidNumber = (value: unknown): value is number => {
31
+ return typeof value === 'number' && !isNaN(value) && isFinite(value);
32
+ };
33
+
34
+ // Type conversions with validation
35
+ export const toBigInt = (value: unknown): bigint => {
36
+ if (isBigInt(value)) {
37
+ return value;
38
+ }
39
+
40
+ if (typeof value === 'string') {
41
+ if (!isValidNumberString(value)) {
42
+ throw new ValidationError(`Invalid number string: ${value}`);
43
+ }
44
+ return BigInt(value);
45
+ }
46
+
47
+ if (isValidNumber(value)) {
48
+ if (!Number.isInteger(value)) {
49
+ throw new ValidationError('Cannot convert non-integer number to BigInt');
50
+ }
51
+ return BigInt(value);
52
+ }
53
+
54
+ throw new ValidationError(`Cannot convert ${typeof value} to BigInt`);
55
+ };
56
+
57
+ // Range validation
58
+ export const validateRange = (value: bigint, min?: bigint, max?: bigint): void => {
59
+ if (min !== undefined && value < min) {
60
+ throw new ValidationError(`Value ${value} is below minimum ${min}`);
61
+ }
62
+ if (max !== undefined && value > max) {
63
+ throw new ValidationError(`Value ${value} exceeds maximum ${max}`);
64
+ }
65
+ };
66
+
67
+ // Operation safety checks
68
+ export const checkAdditionOverflow = (a: bigint, b: bigint): void => {
69
+ // Check if addition would overflow
70
+ if (b > 0 && a > BigInt(Number.MAX_SAFE_INTEGER) - b) {
71
+ throw new OverflowError('Addition would overflow');
72
+ }
73
+ if (b < 0 && a < BigInt(Number.MIN_SAFE_INTEGER) - b) {
74
+ throw new OverflowError('Addition would underflow');
75
+ }
76
+ };
77
+
78
+ export const checkMultiplicationOverflow = (a: bigint, b: bigint): void => {
79
+ // Check if multiplication would overflow
80
+ if (a !== BigInt(0) && b !== BigInt(0)) {
81
+ const maxValue = BigInt(Number.MAX_SAFE_INTEGER);
82
+ const minValue = BigInt(Number.MIN_SAFE_INTEGER);
83
+
84
+ if (a > maxValue / b || a < minValue / b) {
85
+ throw new OverflowError('Multiplication would overflow');
86
+ }
87
+ }
88
+ };
89
+
90
+ export const checkPowerOverflow = (base: bigint, exponent: bigint): void => {
91
+ // Basic overflow checks for exponentiation
92
+ if (exponent < BigInt(0)) {
93
+ throw new ValidationError('Negative exponents not supported for integers');
94
+ }
95
+
96
+ if (base === BigInt(0) && exponent === BigInt(0)) {
97
+ throw new ValidationError('Zero raised to zero is undefined');
98
+ }
99
+
100
+ if (exponent > BigInt(1000)) {
101
+ throw new OverflowError('Exponent too large, computation would overflow');
102
+ }
103
+ };
104
+
105
+ // Array and data structure validation
106
+ export const validateArrayLength = (length: number): void => {
107
+ if (!Number.isInteger(length) || length < 0) {
108
+ throw new ValidationError('Array length must be a non-negative integer');
109
+ }
110
+ if (length > Number.MAX_SAFE_INTEGER) {
111
+ throw new ValidationError('Array length exceeds maximum safe integer');
112
+ }
113
+ };
114
+
115
+ export const validateArrayIndex = (index: number, length: number): void => {
116
+ if (!Number.isInteger(index)) {
117
+ throw new ValidationError('Array index must be an integer');
118
+ }
119
+ if (index < 0 || index >= length) {
120
+ throw new ValidationError('Array index out of bounds');
121
+ }
122
+ };
123
+
124
+ // Tree validation
125
+ export const validateTreeNode = (value: unknown): void => {
126
+ try {
127
+ toBigInt(value);
128
+ } catch (error) {
129
+ throw new ValidationError('Invalid tree node value');
130
+ }
131
+ };
132
+
133
+ // Heap validation
134
+ export const validateHeapProperty = <T>(
135
+ value: T,
136
+ parent: T | undefined,
137
+ comparator: (a: T, b: T) => -1 | 0 | 1,
138
+ isMinHeap: boolean
139
+ ): void => {
140
+ if (!parent) return;
141
+
142
+ const comparison = comparator(value, parent);
143
+ if (isMinHeap && comparison < 0) {
144
+ throw new ValidationError('Min heap property violated');
145
+ }
146
+ if (!isMinHeap && comparison > 0) {
147
+ throw new ValidationError('Max heap property violated');
148
+ }
149
+ };
150
+
151
+ // Ackermann function validation
152
+ export const validateAckermannInput = (m: number, n: number): void => {
153
+ if (!Number.isInteger(m) || !Number.isInteger(n)) {
154
+ throw new ValidationError('Ackermann inputs must be integers');
155
+ }
156
+ if (m < 0 || n < 0) {
157
+ throw new ValidationError('Ackermann inputs must be non-negative');
158
+ }
159
+ if (m > 4) {
160
+ throw new ValidationError('First Ackermann parameter too large for computation');
161
+ }
162
+ };
163
+
164
+ // General numeric validation utilities
165
+ export const isInRange = (value: bigint, min: bigint, max: bigint): boolean => {
166
+ return value >= min && value <= max;
167
+ };
168
+
169
+ export const isPowerOfTwo = (value: bigint): boolean => {
170
+ return value > BigInt(0) && (value & (value - BigInt(1))) === BigInt(0);
171
+ };
172
+
173
+ export const validatePositive = (value: bigint): void => {
174
+ if (value <= BigInt(0)) {
175
+ throw new ValidationError('Value must be positive');
176
+ }
177
+ };
178
+
179
+ export const validateNonNegative = (value: bigint): void => {
180
+ if (value < BigInt(0)) {
181
+ throw new ValidationError('Value must be non-negative');
182
+ }
183
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Target and Module Settings
4
+ "target": "es2020",
5
+ "module": "esnext",
6
+ "lib": ["es2020", "dom"],
7
+ "moduleResolution": "node",
8
+ "types": ["node"],
9
+ // Module Resolution
10
+ "baseUrl": ".",
11
+ "paths": {
12
+ "@/*": ["src/*"],
13
+ "@core/*": ["src/core/*"],
14
+ "@operations/*": ["src/operations/*"],
15
+ "@structures/*": ["src/structures/*"],
16
+ "@utils/*": ["src/utils/*"],
17
+ "@types/*": ["src/types/*"]
18
+ },
19
+
20
+ // Strict Type Checking
21
+ "strict": true,
22
+ "noImplicitAny": true,
23
+ "strictNullChecks": true,
24
+ "strictFunctionTypes": true,
25
+ "strictBindCallApply": true,
26
+ "strictPropertyInitialization": true,
27
+ "noImplicitThis": true,
28
+ "useUnknownInCatchVariables": true,
29
+ "alwaysStrict": true,
30
+
31
+ // Additional Checks
32
+ "noUnusedLocals": true,
33
+ "noUnusedParameters": true,
34
+ "noImplicitReturns": true,
35
+ "noFallthroughCasesInSwitch": true,
36
+ "noUncheckedIndexedAccess": true,
37
+ "noImplicitOverride": true,
38
+ "noPropertyAccessFromIndexSignature": true,
39
+
40
+ // Module Features
41
+ "allowSyntheticDefaultImports": true,
42
+ "esModuleInterop": true,
43
+ "isolatedModules": true,
44
+
45
+ // Emit Configuration
46
+ "declaration": true,
47
+ "declarationMap": true,
48
+ "sourceMap": true,
49
+ "outDir": "./dist",
50
+ "removeComments": false,
51
+ "importHelpers": true,
52
+
53
+ // Advanced Options
54
+ "forceConsistentCasingInFileNames": true,
55
+ "skipLibCheck": true,
56
+ "resolveJsonModule": true,
57
+ "preserveWatchOutput": true,
58
+
59
+ // Experimental Features
60
+ "experimentalDecorators": true,
61
+ "emitDecoratorMetadata": true,
62
+
63
+ // JSX (for future web components)
64
+ "jsx": "preserve"
65
+ },
66
+ "include": [
67
+ "src/**/*.ts",
68
+ "src/**/*.tsx",
69
+ "src/**/*.json"
70
+ ],
71
+ "exclude": [
72
+ "node_modules",
73
+ "dist",
74
+ "**/*.spec.ts",
75
+ "**/*.test.ts",
76
+ "coverage",
77
+ "jest.config.ts"
78
+ ],
79
+ "ts-node": {
80
+ "compilerOptions": {
81
+ "module": "CommonJS"
82
+ }
83
+ }
84
+ }