# Two focused division improvements for decimal.js Two sequential proposals against **decimal.js 10.6.0**, each changing CommonJS and ES modules and adding tests to the existing division test module. 1. [Reuse the quotient to calculate the remainder](patches/0001-reuse-quotient-for-remainder.patch). One production line per module: replace `t % divisor` with `t - quotient * divisor`, using the quotient limb already calculated. 2. [Fill a proven repeating fraction](patches/0002-fill-repeating-fraction-limbs.patch). Twenty added production lines per module. After consuming the actual input, check whether a fractional step returns the same remainder. If so, fill the remaining output limbs with that step's digit and retain the remainder for rounding. Apply this after patch 1. The second patch retains early termination and the ordinary division loop for other fractions. It introduces no reciprocal-growth helper, cache or general cycle detector. Both patches preserve the public API. The arithmetic and loop invariants are explained in [REVIEW.md](REVIEW.md). ## Review the two effects separately On Apple M1 Max, Node 24.22.1 / arm64, at **10,000 significant digits**: | Workload | Patch 1 vs stock | Patch 2 vs patch 1 | |---|---:|---:| | `2 / 3` | 3.66× | 4.24–4.58× | | `3 / 7` | 3.26–3.32× | 1.00–1.01× | | Changing numerator/divisor pairs | 1.78–1.81× | 1.13–1.15× | | `2 / 3`, including conversion to text | 2.77–2.78× | 2.03× | Ranges contain the two runs' medians of paired-round ratios. A value above 1 means faster. The ratio for patch 2 compares it with patch 1, so it does not claim the first patch's gain again. These ratios are measured separately and should not be multiplied to infer an exact combined paired median. Some controls ran slower. The largest observed slowdown for patch 1 was 5.53% on a multi-limb-divisor case; for patch 2 it was 5.05% on a full-input case. Both maxima occurred in run 2 and were smaller in run 1. Identical-code A/A ratios ranged 0.9754–1.0221 for step 1 and 0.9759–1.0269 for step 2. See [RESULTS.md](RESULTS.md) for every case, both runs and the A/A control. The fixture mix targets this optimization; it is not an application workload distribution. These are local Node/V8 results, not browser-wide or application-wide speedup claims. The 24-extra-limb threshold is conservative and has not been tuned across engines or CPUs. ## Apply and test From a clean decimal.js 10.6.0 checkout: ```sh patch -p1 < /path/to/0001-reuse-quotient-for-remainder.patch node test/test.js patch -p1 < /path/to/0002-fill-repeating-fraction-limbs.patch node test/test.js ``` Both also applied successfully to the current upstream source files retrieved on 2026-11-23. The performance baseline and full validation use the pinned 10.6.0 release. The upstream test runner prints failures without reliably setting a failing exit code; the supplied validation wrapper checks totals and exits unsuccessfully on a failed assertion. For the complete offline reproduction, from this package: ```sh python3 reproduce.py --output /tmp/decimal-review ``` Requires **Node.js, Python 3.10+ and `patch`**. Use a new output directory. No npm installation or download is needed. The runner verifies and extracts the pinned source archive, builds both stages, applies the patches to a separate clean tree, and checks their exact output. It runs the independent tests and all upstream suites before timing candidate run 1, A/A, and candidate run 2 sequentially. Options: `--check-only`, `--node`, `--rounds`, `--batch-ms`, and `--seed`. A normal reference run took about 40 seconds on this machine. ## Validation and measurement The reference run passed **128,570 comparisons**: exact internal-state comparisons with stock, an independent BigInt rational rounding oracle, other operations using internal division, and CommonJS/ESM parity. All 60 upstream test modules passed for stock and each patched stage. Their assertion totals vary slightly because upstream includes randomized tests. Patch regressions cover all nine rounding modes, both signs, threshold edges, large limb products, finite tails, finite prefixes before repetition, canceled factors, longer-period controls, and multi-limb input. Tests are included in their corresponding patches and need no new upstream test registration. There are **48 benchmark cases**, 11 shuffled paired rounds per run and timed batches of at least 3 ms. Input construction and exact comparisons are outside the timer. Timings include the public division API, allocation and a checksum; `divide+string` also includes text conversion. Changing-input batches cycle through 64 pairs. Results are checked again after timing. No forced GC or CPU affinity is used. Raw JSON/CSV, source hashes, logs and a run manifest are in `results/reference/`. Upstream: [decimal.js v10.6.0](https://github.com/MikeMcl/decimal.js/tree/v10.6.0), copyright Michael Mclaughlin, MIT licensed. The verified source archive includes its licence. This independent contribution was developed by mikedotexe with Codex, following the [GMP study](https://gmp.mikedotexe.com/).