@yipe/dice 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![License: MIT](https://img.shields.io/badge/License-MIT-007ec6.svg)](LICENSE)
3
3
  ![TypeScript](https://img.shields.io/badge/TypeScript-Ready-3178c6?logo=typescript)
4
4
  ![Size](https://img.shields.io/bundlephobia/minzip/@yipe/dice?logo=npm&logoColor=fff&label=Size)
5
- ![Dice](https://dprcalc.com/api/roll)
5
+ ![Dice](https://dprcalc.com/api/roll.svg)
6
6
 
7
7
  ![Last Commit](https://img.shields.io/github/last-commit/yipe/dice?logo=github)
8
8
  ![Dependencies](https://img.shields.io/librariesio/release/npm/@yipe/dice?logo=npm)
package/dist/index.cjs CHANGED
@@ -47,8 +47,7 @@ var LRUCache = class {
47
47
  };
48
48
 
49
49
  // src/types.ts
50
- var COMPUTATIONAL_EPS = 1e-40;
51
- var TEST_EPS = 1e-10;
50
+ var EPS = 1e-12;
52
51
 
53
52
  // src/query.ts
54
53
  var _DiceQuery = class _DiceQuery {
@@ -1229,7 +1228,7 @@ var _DiceQuery = class _DiceQuery {
1229
1228
  *
1230
1229
  * Returns tuple: [pFirstNonSubset, pFirstSubset, pAnySuccess, pNone]
1231
1230
  */
1232
- firstSuccessSplit(successOutcome, subsetOutcome, eps = COMPUTATIONAL_EPS) {
1231
+ firstSuccessSplit(successOutcome, subsetOutcome, eps = EPS) {
1233
1232
  const pmfs = this.singles;
1234
1233
  if (!pmfs.length) {
1235
1234
  throw new Error("firstSuccessSplitFromPMFs: pmfs must be non-empty");
@@ -1278,10 +1277,9 @@ __publicField(_DiceQuery, "DEFAULT_OUTCOMES", [
1278
1277
  "missNone"
1279
1278
  ]);
1280
1279
  var DiceQuery = _DiceQuery;
1281
- var EPSILON = COMPUTATIONAL_EPS;
1282
1280
  var pmfCache = new LRUCache(1e3);
1283
1281
  var _PMF = class _PMF {
1284
- constructor(map = /* @__PURE__ */ new Map(), epsilon = EPSILON, normalized = false, identifier = `anon#${_PMF.__anonIdCounter++}`, _preservedProvidence = true) {
1282
+ constructor(map = /* @__PURE__ */ new Map(), epsilon = EPS, normalized = false, identifier = `anon#${_PMF.__anonIdCounter++}`, _preservedProvidence = true) {
1285
1283
  this.map = map;
1286
1284
  this.epsilon = epsilon;
1287
1285
  this.normalized = normalized;
@@ -1296,11 +1294,11 @@ var _PMF = class _PMF {
1296
1294
  __publicField(this, "_variance");
1297
1295
  __publicField(this, "_stdev");
1298
1296
  }
1299
- static empty(epsilon = EPSILON, identifier = "empty") {
1297
+ static empty(epsilon = EPS, identifier = "empty") {
1300
1298
  return new _PMF(/* @__PURE__ */ new Map(), epsilon, false, identifier);
1301
1299
  }
1302
1300
  // This has a single bin at value 0, mass of 1
1303
- static zero(epsilon = EPSILON) {
1301
+ static zero(epsilon = EPS) {
1304
1302
  const m = /* @__PURE__ */ new Map();
1305
1303
  m.set(0, { p: 1, count: { miss: 1 }, attr: {} });
1306
1304
  return new _PMF(m, epsilon, false, "zero");
@@ -1472,6 +1470,12 @@ var _PMF = class _PMF {
1472
1470
  preservedProvidence() {
1473
1471
  return this._preservedProvidence;
1474
1472
  }
1473
+ getPowerCacheKey(n, eps) {
1474
+ const id = this.identifier;
1475
+ let key = `${id}`;
1476
+ for (let i = 1; i < n; i++) key += `+${id}`;
1477
+ return `${key}@${eps}`;
1478
+ }
1475
1479
  /**
1476
1480
  * Efficiently computes this PMF convolved with itself `n` times.
1477
1481
  * Uses exponentiation by squaring to reduce total convolutions.
@@ -1488,7 +1492,7 @@ var _PMF = class _PMF {
1488
1492
  }
1489
1493
  if (n === 1) return this;
1490
1494
  const epsilon = eps ?? this.epsilon;
1491
- const key = getPowerCacheKey(this, n, epsilon);
1495
+ const key = this.getPowerCacheKey(n, epsilon);
1492
1496
  {
1493
1497
  const cached = pmfCache?.get(key);
1494
1498
  if (cached) return cached;
@@ -1887,7 +1891,7 @@ var _PMF = class _PMF {
1887
1891
  * - Similar PMF lists share common prefixes (A+B, (A+B)+C, etc.)
1888
1892
  * - Order-independent cache keys work better with consistent build patterns
1889
1893
  */
1890
- static convolveMany(pmfList, eps = EPSILON) {
1894
+ static convolveMany(pmfList, eps = EPS) {
1891
1895
  if (pmfList.length === 0) return _PMF.empty(eps);
1892
1896
  if (pmfList.length === 1) return pmfList[0];
1893
1897
  return _PMF.reduceConvolveLeft(pmfList, eps);
@@ -1902,7 +1906,7 @@ var _PMF = class _PMF {
1902
1906
  static fromJSON(jsonData) {
1903
1907
  return new _PMF(
1904
1908
  new Map(jsonData.bins),
1905
- EPSILON,
1909
+ EPS,
1906
1910
  !!jsonData.normalized,
1907
1911
  jsonData.identifier || "fromJSON"
1908
1912
  );
@@ -2114,12 +2118,6 @@ var _PMF = class _PMF {
2114
2118
  // Unique ID generator for anonymous PMFs to avoid cache key collisions
2115
2119
  __publicField(_PMF, "__anonIdCounter", 1);
2116
2120
  var PMF = _PMF;
2117
- function getPowerCacheKey(base, n, eps) {
2118
- const id = base.identifier;
2119
- let key = `${id}`;
2120
- for (let i = 1; i < n; i++) key += `+${id}`;
2121
- return `${key}@${eps}`;
2122
- }
2123
2121
 
2124
2122
  // src/dice.ts
2125
2123
  var Dice = class _Dice {
@@ -2435,7 +2433,7 @@ var Dice = class _Dice {
2435
2433
  * Convert dice to PMF using OutcomeType labels directly from damage distribution.
2436
2434
  * This is much cleaner than the original complex distribution conversion.
2437
2435
  */
2438
- toPMF(numEpsilon = COMPUTATIONAL_EPS) {
2436
+ toPMF(numEpsilon = EPS) {
2439
2437
  const total = this.total();
2440
2438
  if (total === 0) return PMF.empty(numEpsilon);
2441
2439
  this.ensureHitDistribution();
@@ -2948,16 +2946,15 @@ function parseOperation(s) {
2948
2946
  return;
2949
2947
  }
2950
2948
 
2951
- exports.COMPUTATIONAL_EPS = COMPUTATIONAL_EPS;
2952
2949
  exports.Dice = Dice;
2953
2950
  exports.DiceQuery = DiceQuery;
2951
+ exports.EPS = EPS;
2954
2952
  exports.LRUCache = LRUCache;
2955
2953
  exports.PMF = PMF;
2956
- exports.TEST_EPS = TEST_EPS;
2957
2954
  exports.clearParserCache = clearParserCache;
2958
2955
  exports.getCachingEnabled = getCachingEnabled;
2959
- exports.getPowerCacheKey = getPowerCacheKey;
2960
2956
  exports.parse = parse;
2957
+ exports.pmfCache = pmfCache;
2961
2958
  exports.setCachingEnabled = setCachingEnabled;
2962
2959
  //# sourceMappingURL=index.cjs.map
2963
2960
  //# sourceMappingURL=index.cjs.map