gracio 1.0.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.
- package/LICENSE +21 -0
- package/README.md +130 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gglobensky
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# 📐 Gracio
|
|
2
|
+
|
|
3
|
+
A high-performance, arbitrary-precision rational arithmetic library for TypeScript and JavaScript. Built for developers who need mathematical certainty without sacrificing execution speed.
|
|
4
|
+
|
|
5
|
+
## 🚀 Why Gracio?
|
|
6
|
+
|
|
7
|
+
Standard JavaScript numbers are 64-bit floats (IEEE 754). While fast, they suffer from **precision drift**. For example:
|
|
8
|
+
`0.1 + 0.2 === 0.3` returns `false`.
|
|
9
|
+
|
|
10
|
+
In critical applications—like financial systems, scientific simulations, or game physics—these tiny errors accumulate into "catastrophic cancellation," leading to bugs like characters clipping through walls or missing cents in a transaction.
|
|
11
|
+
|
|
12
|
+
`Gracio` solves this by storing numbers as **Ratios (Fractions)** using native `BigInt`. Every operation is mathematically exact.
|
|
13
|
+
|
|
14
|
+
## ✨ Key Features
|
|
15
|
+
|
|
16
|
+
- **Absolute Precision**: No rounding errors, ever.
|
|
17
|
+
- **Binary GCD Optimization**: Uses Stein's Algorithm for fast fraction reduction.
|
|
18
|
+
- **Dynamic Simplification**: Only simplifies when numbers reach a critical size, maximizing CPU throughput.
|
|
19
|
+
- **Mutable API**: Designed for high-performance loops to minimize Garbage Collection (GC) pressure.
|
|
20
|
+
- **Zero Dependencies**: Lightweight and focused.
|
|
21
|
+
|
|
22
|
+
## 🛡️ Managing Computational Overhead
|
|
23
|
+
|
|
24
|
+
### The "Digit Explosion" Problem
|
|
25
|
+
In pure rational arithmetic, multiplying fractions can cause the number of digits in the numerator and denominator to grow exponentially. In a tight loop, you might quickly end up with numbers that have tens of thousands of digits, leading to severe performance degradation (the "BigInt Tax").
|
|
26
|
+
|
|
27
|
+
### The Precision Guard (`precisionLimit`)
|
|
28
|
+
To prevent this, `Gracio` introduces an optional `precisionLimit`. When the denominator exceeds this limit, the library automatically triggers a **Rational Approximation** using Continued Fraction convergents.
|
|
29
|
+
|
|
30
|
+
This finds the mathematically "best" simpler fraction that maintains the requested precision, capping digit growth without ever falling back to the imprecise IEEE 754 float trap.
|
|
31
|
+
|
|
32
|
+
## 📦 Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install gracio
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 🛠 Usage
|
|
39
|
+
|
|
40
|
+
### Basic Setup
|
|
41
|
+
```typescript
|
|
42
|
+
import { Gracio } from 'gracio';
|
|
43
|
+
|
|
44
|
+
// Create from integers
|
|
45
|
+
const a = Gracio.fromInt(10); // 10/1
|
|
46
|
+
const b = new Gracio(1n, 3n); // 1/3
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Evaluating Equation Strings
|
|
50
|
+
Gracio includes a built-in Lexer and Parser, making it trivial to build calculators or process equation strings (e.g., from Excel or user input).
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Lexer, Parser } from 'gracio';
|
|
54
|
+
|
|
55
|
+
const expr = "1/3 + 1/6";
|
|
56
|
+
const lexer = new Lexer();
|
|
57
|
+
const tokens = lexer.tokenize(expr);
|
|
58
|
+
const parser = new Parser(tokens);
|
|
59
|
+
const result = parser.parse();
|
|
60
|
+
|
|
61
|
+
console.log(result.toString()); // "1/2"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Arithmetic (Mutable)
|
|
65
|
+
Operations modify the object in place for maximum performance:
|
|
66
|
+
```typescript
|
|
67
|
+
const val = Gracio.fromInt(5);
|
|
68
|
+
val.add(new Gracio(1n, 2n)); // val is now 11/2
|
|
69
|
+
val.multiply(Gracio.fromInt(2)); // val is now 11/1
|
|
70
|
+
console.log(val.toString()); // "11/1"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Controlling Growth with precisionLimit
|
|
74
|
+
Prevent digit explosion in chaotic or multiplicative systems:
|
|
75
|
+
```typescript
|
|
76
|
+
const p = Gracio.fromFloat(0.7);
|
|
77
|
+
p.precisionLimit = 50; // Cap denominators at ~50 digits
|
|
78
|
+
// Now, any operation that would cause the denominator to explode
|
|
79
|
+
// will be approximated to the best possible ratio within 50 digits.
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Advanced Operations
|
|
83
|
+
`Gracio` supports powers and roots while maintaining symbolic precision:
|
|
84
|
+
```typescript
|
|
85
|
+
const base = new Gracio(2n, 1n);
|
|
86
|
+
base.pow(3n); // 8/1
|
|
87
|
+
|
|
88
|
+
// Calculate the n-th root of a value
|
|
89
|
+
const squareRootOfTwo = Gracio.root(2n, new Gracio(2n, 1n));
|
|
90
|
+
console.log(squareRootOfTwo.toString()); // High-precision rational approximation
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Conversion & Output
|
|
94
|
+
Convert back to standard JavaScript numbers when needed for display or external APIs:
|
|
95
|
+
```typescript
|
|
96
|
+
const ratio = new Gracio(1n, 3n);
|
|
97
|
+
console.log(ratio.toFloat()); // 0.3333333333333333
|
|
98
|
+
console.log(ratio.toString()); // "1/3"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Preserving Originals (Cloning)
|
|
102
|
+
If you need to keep the original value, use `.clone()`:
|
|
103
|
+
```typescript
|
|
104
|
+
const start = Gracio.fromInt(10);
|
|
105
|
+
const result = start.clone().add(new Gracio(1n, 2n));
|
|
106
|
+
|
|
107
|
+
console.log(start.toString()); // "10/1" (Unchanged)
|
|
108
|
+
console.log(result.toString()); // "21/2"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 📈 Performance Comparison
|
|
112
|
+
|
|
113
|
+
| Metric / Operation | Standard Float | Gracio (Mutable) | big.js / decimal.js | fraction.js |
|
|
114
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
115
|
+
| **Precision** | Drift $\approx 10^{-16}$ | **Absolute ($\infty$)** | Arbitrary Decimals | Absolute ($\infty$) |
|
|
116
|
+
| **Memory** | Stack/Register | Heap Allocated | Heap Allocated | Heap Allocated |
|
|
117
|
+
| **Additive Chain (1M)** | ~4ms | **~113ms** | 170ms - 430ms | ~256ms |
|
|
118
|
+
| **Multiplicative (1K)** | ~0.2ms | **~5.6ms** (Limit 50) | 4ms - 18ms | $\infty$ (Explosion) |
|
|
119
|
+
| **Chaotic Systems** | Fast / Imprecise | **Stable / Precise** | Variable | Crash/Hang |
|
|
120
|
+
|
|
121
|
+
### Optimization Secret: Common Denominator Fast-Path
|
|
122
|
+
`Gracio` detects when two ratios share the same denominator during addition or subtraction. In these cases, it skips expensive cross-multiplication entirely, making additive chains nearly as fast as floating-point arithmetic.
|
|
123
|
+
|
|
124
|
+
## 🎮 Use Cases
|
|
125
|
+
- **Game Physics**: Perfect coordinates for Euclidean spaces to prevent jittering and "ghost" collisions.
|
|
126
|
+
- **FinTech**: Exact currency calculations without rounding errors or the need for arbitrary decimal libraries.
|
|
127
|
+
- **Scientific Tools**: High-precision ratios for mathematical proofs and simulations.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gracio",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "A high-performance, arbitrary-precision rational arithmetic library for TypeScript and JavaScript.",
|
|
6
|
+
"author": "gglobensky",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/gglobensky/Gracio.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"math",
|
|
14
|
+
"precision",
|
|
15
|
+
"rational",
|
|
16
|
+
"fraction",
|
|
17
|
+
"bigint"
|
|
18
|
+
],
|
|
19
|
+
"main": "dist/src/library-entry.js",
|
|
20
|
+
"module": "dist/src/library-entry.js",
|
|
21
|
+
"types": "dist/src/library-entry.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"bundle": "npx esbuild src/library-entry.ts --bundle --minify --outfile=dist/gracio.bundle.js --format=iife --global-name=GracioLib",
|
|
30
|
+
"start": "tsx src/demo.ts",
|
|
31
|
+
"test": "npm run build && mocha dist/**/*.test.js"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/chai": "^5.2.3",
|
|
35
|
+
"@types/mocha": "^10.0.0",
|
|
36
|
+
"chai": "^4.3.7",
|
|
37
|
+
"mocha": "^12.0.1",
|
|
38
|
+
"ts-node": "^10.9.2",
|
|
39
|
+
"tsx": "^4.23.13",
|
|
40
|
+
"typescript": "^5.3.3",
|
|
41
|
+
"big.js": "^7.0.1",
|
|
42
|
+
"decimal.js": "^10.6.0",
|
|
43
|
+
"fraction.js": "^5.3.4"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {}
|
|
46
|
+
}
|