gracio 1.0.0 → 1.0.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.
@@ -0,0 +1,63 @@
1
+ import { expect } from 'chai';
2
+ import { Gracio } from '../src/gracio.js';
3
+ import { calculateDrift } from '../src/audit-util.js';
4
+ describe('Advanced Arithmetic Operations', () => {
5
+ describe('Powers (.pow)', () => {
6
+ it('should calculate positive integer powers correctly', () => {
7
+ const a = Gracio.fromInt(2);
8
+ a.pow(10n);
9
+ expect(a.toString()).to.equal("1024/1");
10
+ expect(calculateDrift(a, Math.pow(2, 10))).to.be.below(1e-15);
11
+ });
12
+ it('should handle zero exponent (x^0 = 1)', () => {
13
+ const a = new Gracio(3n, 7n);
14
+ a.pow(0n);
15
+ expect(a.toString()).to.equal("1/1");
16
+ });
17
+ it('should handle negative exponents by inverting the ratio', () => {
18
+ const a = new Gracio(1n, 2n); // 1/2
19
+ a.pow(-2n); // (1/2)^-2 = 2^2 = 4
20
+ expect(a.toString()).to.equal("4/1");
21
+ expect(calculateDrift(a, Math.pow(0.5, -2))).to.be.below(1e-15);
22
+ });
23
+ it('should handle fractional ratios raised to powers', () => {
24
+ const a = new Gracio(3n, 4n);
25
+ a.pow(2n); // (3/4)^2 = 9/16
26
+ expect(a.toString()).to.equal("9/16");
27
+ expect(calculateDrift(a, Math.pow(0.75, 2))).to.be.below(1e-15);
28
+ });
29
+ });
30
+ describe('Roots (.root)', () => {
31
+ it('should calculate square roots of perfect squares', () => {
32
+ const a = Gracio.fromInt(4);
33
+ const res = Gracio.root(2n, a);
34
+ expect(res.toString()).to.contain("2");
35
+ });
36
+ it('should calculate cube roots of perfect cubes', () => {
37
+ const a = Gracio.fromInt(27);
38
+ const res = Gracio.root(3n, a);
39
+ expect(res.toString()).to.contain("3");
40
+ });
41
+ it('should handle approximate roots correctly', () => {
42
+ const a = Gracio.fromInt(2);
43
+ const res = Gracio.root(2n, a);
44
+ // sqrt(2) approx 1.414213...
45
+ expect(res.toFloat()).to.be.closeTo(1.414213, 0.0001);
46
+ });
47
+ });
48
+ describe('Common Denominator Stress Test', () => {
49
+ it('should maintain high performance during long additive chains with same denominator', () => {
50
+ const iterations = 100_000;
51
+ const start = performance.now();
52
+ let rSum = Gracio.fromInt(0);
53
+ const step = new Gracio(1n, 3n);
54
+ for (let i = 0; i < iterations; i++) {
55
+ rSum.add(step);
56
+ }
57
+ const end = performance.now();
58
+ console.log(`\n Additive chain (${iterations} ops) took: ${(end - start).toFixed(2)}ms`);
59
+ expect(rSum.toString()).to.equal(`${iterations}/3`);
60
+ expect(end - start).to.be.below(100); // Should be extremely fast now
61
+ });
62
+ });
63
+ });
@@ -0,0 +1,40 @@
1
+ // test/basic-arithmetic.ts
2
+ import { expect } from 'chai';
3
+ import { Gracio } from '../src/gracio.js';
4
+ import { calculateDrift } from '../src/audit-util.js';
5
+ describe('Basic Arithmetic Operations', () => {
6
+ describe('Addition', () => {
7
+ it('should add two large numbers accurately', async () => {
8
+ const a = Gracio.fromInt(999999999999999);
9
+ const b = Gracio.fromInt(1);
10
+ const result = a.add(b);
11
+ expect(result.toString()).to.equal("1000000000000000/1");
12
+ expect(calculateDrift(result, 999999999999999 + 1)).to.be.below(1e-15);
13
+ });
14
+ it('should add decimal fractions accurately', async () => {
15
+ const a = new Gracio(1n, 10n);
16
+ const b = new Gracio(2n, 10n);
17
+ const result = a.add(b);
18
+ expect(result.toString()).to.equal("3/10");
19
+ expect(calculateDrift(result, 0.1 + 0.2)).to.be.below(1e-15);
20
+ });
21
+ });
22
+ describe('Multiplication', () => {
23
+ it('should multiply large numbers accurately', async () => {
24
+ const a = Gracio.fromInt(10000000000000000);
25
+ const b = Gracio.fromInt(10000000000000000);
26
+ const result = a.multiply(b);
27
+ expect(result.toString()).to.equal("100000000000000000000000000000000/1");
28
+ expect(calculateDrift(result, 10000000000000000 * 10000000000000000)).to.be.below(1e-15);
29
+ });
30
+ });
31
+ describe('Division', () => {
32
+ it('should divide accurately', async () => {
33
+ const a = Gracio.fromInt(10);
34
+ const b = Gracio.fromInt(3);
35
+ const result = a.divide(b);
36
+ expect(result.toString()).to.equal("10/3");
37
+ expect(calculateDrift(result, 10 / 3)).to.be.below(1e-15);
38
+ });
39
+ });
40
+ });
@@ -0,0 +1,40 @@
1
+ import { expect } from 'chai';
2
+ import { Gracio } from '../src/gracio.js';
3
+ import { auditOperation } from '../src/audit-util.js';
4
+ describe('Precision & Performance Audit', () => {
5
+ it('should audit simple additive loops (The Common Denominator Case)', async () => {
6
+ const iterations = 10_000;
7
+ const stepValue = 0.1;
8
+ const exactResult = stepValue * iterations;
9
+ // Ratio setup: we want to measure the .add() loop specifically
10
+ let rSum = Gracio.fromInt(0);
11
+ const stepRatio = new Gracio(1n, 10n);
12
+ // Float setup
13
+ let fSum = 0;
14
+ const metrics = await auditOperation('Additive Loop (0.1 * N)', () => {
15
+ rSum.add(stepRatio);
16
+ return rSum; // Return the object so we can extract value at end
17
+ }, () => {
18
+ fSum += stepValue;
19
+ return fSum;
20
+ }, exactResult, (res) => res.toFloat(), iterations);
21
+ expect(metrics.performanceTax).to.be.below(10); // Should be very efficient now
22
+ });
23
+ it('should audit multiplicative chains', async () => {
24
+ const iterations = 1_000;
25
+ const stepValue = 1.1;
26
+ const exactResult = Math.pow(stepValue, iterations);
27
+ let rVal = Gracio.fromInt(1);
28
+ const stepRatio = new Gracio(11n, 10n);
29
+ let fVal = 1.0;
30
+ const metrics = await auditOperation('Multiplicative Chain (1.1 ^ N)', () => {
31
+ rVal.multiply(stepRatio);
32
+ return rVal;
33
+ }, () => {
34
+ fVal *= stepValue;
35
+ return fVal;
36
+ }, exactResult, (res) => res.toFloat(), iterations);
37
+ // Multiplicative chains grow numbers fast, so the tax will be higher than additive
38
+ expect(Number.isFinite(metrics.performanceTax)).to.be.true;
39
+ });
40
+ });
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gracio",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "A high-performance, arbitrary-precision rational arithmetic library for TypeScript and JavaScript.",
6
6
  "author": "gglobensky",
7
7
  "license": "MIT",