exactcdf: double-precision statistical distributions, with the error declared before the code

There is a scene that repeats itself in every statistics classroom. The teacher writes a z value on the board, someone types it into a calculator or a spreadsheet, a p-value comes out with eight decimals, and everybody copies it down. Nobody asks where those eight decimals came from. The machine said so, and the machine doesn't make mistakes.

It does. Not always, not by much, but it does, and it almost never warns you. Behind every p-value there is a function somebody had to program, and programming a function like that well is watchmaker's work: large numbers, tiny numbers and the tails of the distribution each behave in their own way, and a formula that is perfect in the middle can return garbage at the edge. A computer is dumber than a rock. It does exactly what it was told, even when what it was told is wrong.

exactcdf is my answer to that problem: a small library that computes those functions and that, before computing anything, puts in writing how wrong it is allowed to be in each region. Then it checks.

What exactcdf is, in one paragraph

exactcdf is an open-source JavaScript (Node.js) library that computes cumulative distribution functions —normal, gamma, chi-squared and beta— and the special functions they rest on (the error function and the gamma function), in double precision. It is designed and maintained by Carlos Eduardo Ravello Joo (ORCID 0009-0007-5631-7436). It is published on npm (install it with npm install exactcdf) and its source is on GitHub, public since 5 November 2026, under the MPL-2.0 license. The current version is 1.0.2. It has no dependencies, never returns NaN, and every result is checked, row by row, against 31,307 reference values computed to 70 digits, under per-region tolerances that were committed to the repository before the code was.

Install and use

You need Node.js 18 or later. It is an ES module ("type": "module") with no build step.

npm install exactcdf
import { pnorm, pchisq, pbeta } from "exactcdf";

pnorm(1.96);          // 0.9750021048517796
pnorm(1.96, false);   // 0.024997895148220428
pchisq(7.81, 3);      // 0.949893943649994
pbeta(0.3, 2, 5);     // 0.579825

The second example is the one people use most without knowing it: the upper tail of the normal at z = 1.96, the famous 0.025 on each side that gives you 95% confidence. In pnorm, with lowerTail set to false, the library computes the upper tail directly instead of subtracting from 1. Subtracting from 1 looks harmless, but far out in the tails it eats your decimals. It is like weighing a cat by stepping on the scale holding it and subtracting your own weight: fine for a big cat, useless for a kitten.

The eight functions

FunctionSignatureReturns
erferf(x)error function
erfcerfc(x)complementary error function
pnormpnorm(z, lowerTail?)standard normal CDF, Φ(z)
lgammalgamma(a)log-gamma, ln Γ(a)
gammagamma(a)gamma function, Γ(a)
pgammapgamma(x, a, lowerTail?)regularized lower incomplete gamma, P(a, x)
pchisqpchisq(x, nu, lowerTail?)chi-squared CDF with nu degrees of freedom
pbetapbeta(x, a, b, lowerTail?)regularized incomplete beta, Iₓ(a, b)

lowerTail defaults to true. No function ever returns NaN. Pass it something out of domain or of the wrong type —gamma(-1), say— and the library throws a StatsError, which extends RangeError and carries structured fields: funcion, motivo (dominio, tipo, indeterminado or pendiente), argumento, valor, dominio and region. That way the caller can build its own error message, in any language.

Never returning NaN is a deliberate choice. A NaN is an error that makes no noise: it slips into a sum, poisons the average and shows up three screens later, when nobody remembers where it came from. I would rather the library complain on the spot and say exactly what bothered it.

How we know it is right

This is what I actually care about in exactcdf, and what separates it from a function copied off a forum. The question is not whether it returns numbers: anything returns numbers. The question is how far it strays from the truth, where, and who decided how far is too far.

The order was this. On 13 October 2026 the references went into the repository: thousands of values computed with mpmath 1.3.0 to 70 digits, far more than a double-precision number can hold. Those references were not produced by AI. Between 21 and 25 October the tolerances were written and frozen: how much error each function is allowed in each region. The code came afterwards, on 29 October. The git history shows it in that order, and anyone can check it.

Why does the order matter? Because if you set the tolerance after seeing the results, you always pass. It is drawing the bullseye around the arrow. Setting it first means accepting that you can fail.

The tolerances, in relative error, are: up to 1e-15 for erf, erfc and pnorm; up to 1e-14 for lgamma and gamma; up to 1e-13 for pgamma, pchisq and pbeta, which are the error bounds stated by the source algorithms themselves. Exact values and subnormal results follow separate fixed rules.

And a confession, because without it the rest is worthless. In the hardest tails —pgamma and pchisq in the Temme and far-tail branches, pbeta in the continued-fraction and asymptotic branches— the tolerance is provisional: between 788 and 3,864 ulps, set at twice the 99th percentile of the error R 4.6.1 makes on those same rows. It is marked "A FIJAR" (to be fixed) in the tolerance file and will be fixed in a later release. It is no secret: it sits in the repository, in plain view. (An ulp is the gap between one double-precision number and the next: the smallest unit by which a number can be wrong.)

The current release passes all 31,307 reference rows across the nine checked functions —the eight public ones plus the internal gamma1pm1— with no failures. This is the worst error measured per function:

FunctionRowsWorst case (ulps)Region
erf1,5754erf_r2
erfc3,4486erfc_r3
pnorm4,8706pnorm_cola
lgamma1,54213lgamma_d
gamma1,0186gamma_fuera
pgamma7,902917pgamma_temme (74% of its tolerance)
pchisq2,170917pgamma_temme (74%)
pbeta8,266121pbeta_bfrac20

Read it like this: for the normal, the worst thing found in almost five thousand cases was 6 ulps, roughly fifteen correct significant digits. For the incomplete gamma, in its hardest branch, the worst case reached 917 ulps, still about twelve or thirteen correct digits and within the declared tolerance. I do not claim "accurate to a few ulps" across the board, because in the tails that is not true. I say exactly how much, and where.

All of it is reproducible. The same command the continuous integration runs on Node 18, 20 and 22 on every change:

npm test

Or one function at a time, with its per-region report:

node stats/harness/run.js --funcion pgamma

The references live in tests/fixtures/, the scripts that generate them in tests/gen/, and the development reports in stats/informes/. None of this asks for faith. It asks for a command.

Where the algorithms come from

I invented nothing on the mathematical side, and that is a good thing. The methods come from the primary literature, from people who spent years taming these functions:

  • the error function and its complement, from W. J. Cody's 1969 work on rational Chebyshev approximations;
  • the incomplete gamma, from A. R. DiDonato and A. H. Morris (1986);
  • the incomplete beta, from DiDonato and Morris's Algorithm 708 (1992).

Full references with DOIs are at the end of the page. What it took was reading those texts with a magnifying glass and deciding what to do at the points they leave open. Those decisions are written down in the repository, one by one.

What it is for

If you do statistics, you use these functions even if you have never called them by name. Some concrete examples:

  • p-values. A z-test's comes from pnorm; a chi-squared test's (independence, goodness of fit) comes from pchisq.
  • Psychometrics. Turning a z score into a percentile is pnorm. Calculators that score psychological instruments need these functions underneath, and need them to be trustworthy.
  • Proportions and counts. The incomplete beta gives the binomial cumulative probability directly: the exact p-value of a binomial test comes out of pbeta. The incomplete gamma does the same for the Poisson: the cumulative probability of a count comes out of pgamma.
  • Other fields. Reliability engineering, epidemiology, quality control, A/B testing in digital products: anywhere a decision hangs on a cumulative probability.
  • JavaScript tools. If you build a calculator or a dashboard on Node, you can use exactcdf as the engine and know, in writing, how wrong it can be.

In my case, exactcdf is the engine of the research calculators I am building, starting with one for scoring psychological instruments, in auditable code.

Still in the workshop

The inverse functions (qnorm, qgamma, qchisq, qt) and the Student's t CDF (pt) already have their references in the repository, but no implementation yet. They will arrive by the same road: tolerances first, code second.

This release runs on Node.js. The package reads its rules file with node:fs, so a browser build is separate work, coming later.

How it was made

In the same words as the repository and the Zenodo record, so there is only one version: the code in stats/src, the test harness (stats/harness) and the reference-generation scripts (tests/gen) were written by Anthropic AI models (Claude Opus and Sonnet families, via Claude Code and Cowork, October–November 2026) from a written specification derived from the primary literature (Cody 1969; DiDonato & Morris 1986, 1992) and approved by the author. The reference values were not produced by AI: they were computed with R 4.6.1 and mpmath 1.3.0 (70 digits). The author directed the project, made every design and scope decision, arbitrated the numerical ambiguities the texts leave open, and signed off each release. The tolerances were fixed and frozen before the code was written, and each piece was audited in AI sessions other than the one that produced it.

I tell it this way because that is what happened, and because a library that boasts about verification cannot hide how it was built. AI writes fast and with total confidence; that is exactly why the serious work lies in what it does not write: the references, the tolerances fixed in advance, and the decision about what passes. The rock does not decide on its own.

The dossier with a DOI

The code, references and audits as they stood at the close of development are deposited on Zenodo under DOI 10.5281/zenodo.23811083, dated 30 October 2026. The files in that deposit are restricted, but their SHA-256 fingerprint manifest is public: it lets anyone check, at any time, that a file is exactly the one that existed on that date.

License

exactcdf is released under the Mozilla Public License 2.0 (MPL-2.0). In practice: you may use it inside closed or commercial software; if you modify the library's own files, those changes must stay open under the same license.

How to cite it

If you use exactcdf in research, cite the software:

Ravello Joo, C. E. (2026). exactcdf (Version 1.0.2) [Computer software]. https://github.com/carlosravello-png/exactcdf

And, if the development dossier is useful to you:

Ravello Joo, C. E. (2026). stats.js directa y su expediente: funciones estadísticas de alta precisión en JavaScript, con fixtures y auditorías [Direct stats.js and its dossier: high-precision statistical functions in JavaScript, with fixtures and audits] (Version 1.0.0). Zenodo. https://doi.org/10.5281/zenodo.23811083

The repository includes a CITATION.cff file, so GitHub offers the citation ready-made under "Cite this repository".

Frequently asked questions

What is exactcdf?

A JavaScript library for Node.js that computes the normal, gamma, chi-squared and beta cumulative distribution functions, plus the error and gamma functions, in double precision, with no dependencies and every result checked against 70-digit references.

How do I install it?

With npm install exactcdf. It requires Node.js 18 or later and is imported as an ES module.

Does it work in the browser?

Not this release: it runs on Node.js, because it reads its rules file with node:fs. A browser build is planned as separate work.

How accurate is it?

It passes all 31,307 reference rows with no failures. The worst measured error ranges from 4 ulps (erf) to 917 ulps (pgamma and pchisq in the Temme branch), always within per-region tolerances fixed before the code. In the hardest tails those tolerances are provisional and marked as such in the repository.

Who wrote it?

It is designed and maintained by Carlos Eduardo Ravello Joo. The code, harness and generators were written by Anthropic AI models from a specification he approved; the references were computed with R and mpmath, not AI; he made the design decisions and signed off each release.

Can I use it in commercial or closed-source software?

Yes. The MPL-2.0 license allows it; it only requires that changes to the library's own files stay open.

How is it different from other JavaScript statistics libraries?

I have not measured speed or accuracy against other libraries, so I make no such claims. The difference I can show is one of method: tolerances declared per region before the code was written, a frozen public test harness, and a measured, published worst-case error for every function.

Which functions are missing?

The inverses (qnorm, qgamma, qchisq, qt) and the Student's t CDF (pt). Their references are already in the repository; the implementation will come in later releases.

How do I cite it?

Ravello Joo, C. E. (2026). exactcdf (Version 1.0.2) [Computer software]. https://github.com/carlosravello-png/exactcdf. The repository includes CITATION.cff.

References

  1. Cody, W. J. (1969). Rational Chebyshev approximations for the error function. Mathematics of Computation, 23(107), 631–637. https://doi.org/10.1090/S0025-5718-1969-0247736-4
  2. DiDonato, A. R., & Morris, A. H. (1986). Computation of the incomplete gamma function ratios and their inverse. ACM Transactions on Mathematical Software, 12(4), 377–393. https://doi.org/10.1145/22721.23109
  3. DiDonato, A. R., & Morris, A. H. (1992). Algorithm 708: Significant digit computation of the incomplete beta function ratios. ACM Transactions on Mathematical Software, 18(3), 360–373. https://doi.org/10.1145/131766.131776

Software: npm · GitHub · MPL-2.0 license
Dossier: DOI 10.5281/zenodo.23811083
Text of this page: CC BY 4.0

← All tools
ES