@numero-uno-jpn/rstats 0.15.3 → 0.17.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.
Files changed (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -1,3 +1,7 @@
1
+ /** log(x/2), also for subnormal x (log(Number.MIN_VALUE / 2) was -Infinity) */
2
+ export declare function logHalf(x: number): number;
3
+ /** (x/2)^v, also for subnormal x */
4
+ export declare function powHalf(x: number, v: number): number;
1
5
  /**
2
6
  * (x/2)^v / Gamma(v + 1) * sum, the leading factor of the power series of I_v and J_v times the series sum.
3
7
  * For v >= 10 the factor is Loader's Poisson term dpois(v; x/2) e^(x/2), which is accurate where pow / gamma loses
@@ -6,3 +10,26 @@
6
10
  * factor (the log of a value near 1e-308 carries an absolute error of 700 ε, which a subnormal result can afford)
7
11
  */
8
12
  export declare function seriesTimesPrefix(v: number, x: number, sum: number): number;
13
+ /**
14
+ * Large orders. The recurrences in besselIK.ts and besselJY.ts take about v steps, which does not end for v = 1e12
15
+ * (besselK(1, 1e12)), while the result is far beyond the range of doubles. Above this order, bounds on the log of the
16
+ * result decide such cases first
17
+ */
18
+ export declare const LARGE_ORDER = 1000;
19
+ /** Bounds lo <= log y <= hi, with the sum of the sizes of the terms they were formed from (for the rounding margin) */
20
+ export interface LogBounds {
21
+ lo: number;
22
+ hi: number;
23
+ size: number;
24
+ }
25
+ /**
26
+ * Bounds on log I_v(x) (v >= 0) from its power series (x/2)^v / Gamma(v + 1) sum (x^2/4)^k / (k! (v+1)_k): the terms
27
+ * are positive, and the sum is at most exp(x^2 / (4 (v + 1))). The lower bound is also the upper bound of
28
+ * log abs(J_v(x)) (DLMF 10.14.4)
29
+ */
30
+ export declare function logISeriesBounds(v: number, x: number): LogBounds;
31
+ /**
32
+ * 1 if a value whose log lies in [lo, hi] certainly overflows, -1 if it certainly rounds to 0, 0 otherwise. The bounds
33
+ * must clear the limits by a margin above the rounding of their terms
34
+ */
35
+ export declare function certainRange(b: LogBounds): 1 | -1 | 0;
@@ -1,7 +1,18 @@
1
1
  // The power-series prefix shared by the Bessel functions I and J (besselIK.ts, besselJY.ts).
2
- import { gamma } from "./gamma.js";
2
+ import { gamma, lgamma } from "./gamma.js";
3
3
  import { dpoisRaw } from "./loader.js";
4
4
  const DBL_MIN = 2.2250738585072014e-308;
5
+ const EPS = Number.EPSILON;
6
+ /** Below this x, x / 2 is not exact (Number.MIN_VALUE / 2 is 0) */
7
+ const HALF_EXACT = 2 ** -1021;
8
+ /** log(x/2), also for subnormal x (log(Number.MIN_VALUE / 2) was -Infinity) */
9
+ export function logHalf(x) {
10
+ return x >= HALF_EXACT ? Math.log(x / 2) : Math.log(x) - Math.LN2;
11
+ }
12
+ /** (x/2)^v, also for subnormal x */
13
+ export function powHalf(x, v) {
14
+ return x >= HALF_EXACT ? Math.pow(x / 2, v) : Math.pow(x, v) * Math.pow(2, -v);
15
+ }
5
16
  /**
6
17
  * (x/2)^v / Gamma(v + 1) * sum, the leading factor of the power series of I_v and J_v times the series sum.
7
18
  * For v >= 10 the factor is Loader's Poisson term dpois(v; x/2) e^(x/2), which is accurate where pow / gamma loses
@@ -11,7 +22,7 @@ const DBL_MIN = 2.2250738585072014e-308;
11
22
  */
12
23
  export function seriesTimesPrefix(v, x, sum) {
13
24
  if (v < 10)
14
- return (Math.pow(x / 2, v) / gamma(v + 1)) * sum;
25
+ return (powHalf(x, v) / gamma(v + 1)) * sum;
15
26
  const lambda = x / 2;
16
27
  const d = dpoisRaw(v, lambda, false);
17
28
  if (d >= DBL_MIN && lambda < 700) {
@@ -22,3 +33,35 @@ export function seriesTimesPrefix(v, x, sum) {
22
33
  const ld = dpoisRaw(v, lambda, true) + lambda + Math.log(Math.abs(sum));
23
34
  return Math.sign(sum) * Math.exp(ld);
24
35
  }
36
+ /**
37
+ * Large orders. The recurrences in besselIK.ts and besselJY.ts take about v steps, which does not end for v = 1e12
38
+ * (besselK(1, 1e12)), while the result is far beyond the range of doubles. Above this order, bounds on the log of the
39
+ * result decide such cases first
40
+ */
41
+ export const LARGE_ORDER = 1000;
42
+ const LOG_MAX = Math.log(Number.MAX_VALUE);
43
+ /** Values below Number.MIN_VALUE / 2 round to 0 */
44
+ const LOG_MIN = Math.log(Number.MIN_VALUE) - Math.LN2;
45
+ /**
46
+ * Bounds on log I_v(x) (v >= 0) from its power series (x/2)^v / Gamma(v + 1) sum (x^2/4)^k / (k! (v+1)_k): the terms
47
+ * are positive, and the sum is at most exp(x^2 / (4 (v + 1))). The lower bound is also the upper bound of
48
+ * log abs(J_v(x)) (DLMF 10.14.4)
49
+ */
50
+ export function logISeriesBounds(v, x) {
51
+ const a = v * logHalf(x);
52
+ const g = lgamma(v + 1);
53
+ const t = (x * x) / (4 * (v + 1));
54
+ return { lo: a - g, hi: a - g + t, size: Math.abs(a) + Math.abs(g) + t };
55
+ }
56
+ /**
57
+ * 1 if a value whose log lies in [lo, hi] certainly overflows, -1 if it certainly rounds to 0, 0 otherwise. The bounds
58
+ * must clear the limits by a margin above the rounding of their terms
59
+ */
60
+ export function certainRange(b) {
61
+ const m = 1 + 64 * EPS * b.size;
62
+ if (b.lo - m > LOG_MAX)
63
+ return 1;
64
+ if (b.hi + m < LOG_MIN)
65
+ return -1;
66
+ return 0;
67
+ }
@@ -156,7 +156,10 @@ const HUGE = 2 ** 52;
156
156
  */
157
157
  function lbetaHuge(a, b) {
158
158
  const lead = ddMulNum(ddSub(ddLog([b, 0]), ddLog([a, 0])), b);
159
- const rest = -0.5 * Math.log(b) - (a + b - 0.5) * Math.log1p(b / a) + (stirlerr(a) + stirlerr(b) - stirlerr(a + b));
159
+ const l1p = Math.log1p(b / a);
160
+ // a + b can overflow (lbeta(1e308, 1e308) was -Infinity), while a L and b L stay finite
161
+ const sumTerm = Number.isFinite(a + b) ? (a + b - 0.5) * l1p : a * l1p + (b - 0.5) * l1p;
162
+ const rest = -0.5 * Math.log(b) - sumTerm + (stirlerr(a) + stirlerr(b) - stirlerr(a + b));
160
163
  return ddAdd(ddAdd(lead, LS2PI_DD), [rest, 0]);
161
164
  }
162
165
  /**
@@ -10,10 +10,16 @@
10
10
  // log B is taken from lbetaDD (double-double), since its value can be in the hundreds and exp would turn the
11
11
  // rounding of a double log into a relative error of 1e-13.
12
12
  import { nonInt, roundHalfEven } from "../internal/integer.js";
13
+ import { ldexp } from "../internal/summary.js";
13
14
  import { lbeta, lbetaDD } from "./beta.js";
14
15
  import { ddAdd, ddDiv, ddLog, ddMul, twoSum } from "./dd.js";
15
16
  import { gamma, lgamma, lgamma1p, sinpi } from "./gamma.js";
16
17
  import { digamma } from "./psigamma.js";
18
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
19
+ import { checkOptions } from "../internal/checkOptions.js";
20
+ const CHOOSE_KEYS = ["rCompatible"];
21
+ const LCHOOSE_KEYS = ["rCompatible"];
22
+ // Generated by `node scripts/option-keys.mjs --write` (end)
17
23
  /** Below this k, choose is the product n (n-1) ... (n-k+1) / k! */
18
24
  const K_SMALL = 30;
19
25
  function isInt(x) {
@@ -40,6 +46,16 @@ function fallingRatio(n, k) {
40
46
  return r;
41
47
  }
42
48
  /** log B(a, b) (a, b > 0): double-double where lbetaDD is accurate, else lbeta (whose value is then large) */
49
+ /** n (n - 1) ... (n - k + 1) / k! where the product overflows on the way, in the coordinates 2^-s with 2^s near abs(n) */
50
+ function fallingRatioScaled(n, k) {
51
+ const s = Math.floor(Math.log2(Math.abs(n)));
52
+ let r = [1, 0];
53
+ for (let j = 0; j < k; j++) {
54
+ const t = twoSum(n, -j);
55
+ r = ddDiv(ddMul(r, [ldexp(t[0], -s), ldexp(t[1], -s)]), [j + 1, 0]);
56
+ }
57
+ return ldexp(r[0] + r[1], s * k);
58
+ }
43
59
  function logBeta(a, b) {
44
60
  return Math.min(a, b) >= 1 && Math.max(a, b) < 2 ** 52 ? lbetaDD(a, b) : [lbeta(a, b), 0];
45
61
  }
@@ -101,6 +117,7 @@ function logChooseLarge(n, k) {
101
117
  * @category Special functions
102
118
  */
103
119
  export function choose(n, k, options) {
120
+ checkOptions(options, CHOOSE_KEYS, "choose"); // generated: option-keys.mjs
104
121
  if (Number.isNaN(n) || Number.isNaN(k))
105
122
  return NaN;
106
123
  const rc = options?.rCompatible ?? true;
@@ -123,11 +140,9 @@ export function choose(n, k, options) {
123
140
  const v = r[0] + r[1];
124
141
  if (Number.isFinite(v))
125
142
  return isInt(n) || snap ? Math.round(v) : v;
126
- // Overflow (the double-double steps then give NaN): the plain product overflows too
127
- let p = 1;
128
- for (let j = 0; j < k; j++)
129
- p = (p * (n - j)) / (j + 1);
130
- return p;
143
+ // The product overflows on the way (n (n - 1) at n = 1.5e154 while choose(n, 2) = 1.125e308; the double-double steps
144
+ // then give NaN): the factors n - j divided by 2^s (exact), and the result scaled back by 2^(s k)
145
+ return fallingRatioScaled(n, k);
131
146
  }
132
147
  if (rc) {
133
148
  n = rSnap(n, k);
@@ -163,6 +178,7 @@ export function choose(n, k, options) {
163
178
  * @category Special functions
164
179
  */
165
180
  export function lchoose(n, k, options) {
181
+ checkOptions(options, LCHOOSE_KEYS, "lchoose"); // generated: option-keys.mjs
166
182
  if (Number.isNaN(n) || Number.isNaN(k))
167
183
  return NaN;
168
184
  const rc = options?.rCompatible ?? true;
@@ -186,9 +202,9 @@ export function lchoose(n, k, options) {
186
202
  const l = ddLog(r[0] < 0 ? [-r[0], -r[1]] : r);
187
203
  return l[0] + l[1];
188
204
  }
189
- if (v === 0)
190
- return -Infinity;
191
- // Out of range: sum of logs (the result is large in magnitude, so its rounding is relatively small)
205
+ // Out of range: sum of logs (the result is large in magnitude, so its rounding is relatively small). v = 0 also comes
206
+ // here: a coefficient below the doubles has a finite log (lchoose(MIN, 2) was -Infinity for -745.13; issue #107),
207
+ // and an exact zero has a factor n - j = 0, whose log makes the sum -Infinity
192
208
  let s = -lgamma(k + 1);
193
209
  for (let j = 0; j < k; j++)
194
210
  s += Math.log(Math.abs(n - j));
@@ -21,22 +21,66 @@ export function twoSum(a, b) {
21
21
  if (!ddEnabled)
22
22
  return [a + b, 0];
23
23
  const s = a + b;
24
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
25
+ if (!Number.isFinite(s))
26
+ return [s, 0];
24
27
  const bb = s - a;
25
28
  const e = a - (s - bb) + (b - bb);
26
29
  return [s, e];
27
30
  }
31
+ /** x 2^k, in two steps so that 2^k itself cannot overflow or underflow */
32
+ function scale2(x, k) {
33
+ const h = Math.trunc(k / 2);
34
+ return x * 2 ** h * 2 ** (k - h);
35
+ }
36
+ /**
37
+ * The power of 2 that brings a divisor below 1 to [1, 2) when the numerator is below 2^-900, 0 otherwise (or when the
38
+ * numerator would overflow). The remainders of the division, about |a| eps and |a| eps^2, fell below the subnormals
39
+ * there and the low part of the quotient was lost (ddDiv([MIN], [3 MIN]) gave [1/3, 0], ddDiv([1e-300], [3e-300]) a
40
+ * low part of 8.4e-20 for 1.85e-17; issue #105); the scaling by 2^k is exact and leaves the quotient unchanged
41
+ */
42
+ function tinyDivisionShift(a, b) {
43
+ if (!(Math.abs(a) < 2 ** -900) || !(Math.abs(b) < 1) || b === 0)
44
+ return 0;
45
+ const k = -Math.floor(Math.log2(Math.abs(b)));
46
+ return Number.isFinite(scale2(a, k)) ? k : 0;
47
+ }
48
+ /**
49
+ * The rounding error a * b - fl(a * b) for operands so large that Veltkamp splitting overflows: each operand is
50
+ * scaled near 1 by a power of 2 (exact), and the error of the scaled product scaled back
51
+ */
52
+ function prodErrScaled(a, b) {
53
+ const ka = a === 0 ? 0 : Math.floor(Math.log2(Math.abs(a)));
54
+ const kb = b === 0 ? 0 : Math.floor(Math.log2(Math.abs(b)));
55
+ const x = scale2(a, -ka);
56
+ const y = scale2(b, -kb);
57
+ const p = x * y;
58
+ let t = SPLITTER * x;
59
+ const xh = t - (t - x);
60
+ const xl = x - xh;
61
+ t = SPLITTER * y;
62
+ const yh = t - (t - y);
63
+ const yl = y - yh;
64
+ // scaled back by 2^(ka + kb) at once: 2^ka first underflowed the error of 1e-320 * 1e308 to 0 before 2^kb (#62)
65
+ return scale2(xh * yh - p + xh * yl + xl * yh + xl * yl, ka + kb);
66
+ }
28
67
  /** [p, e] satisfying a * b = p + e exactly (Veltkamp splitting) */
29
68
  export function twoProd(a, b) {
30
69
  if (!ddEnabled)
31
70
  return [a * b, 0];
32
71
  const p = a * b;
72
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
73
+ if (!Number.isFinite(p))
74
+ return [p, 0];
33
75
  let t = SPLITTER * a;
34
76
  const ah = t - (t - a);
35
77
  const al = a - ah;
36
78
  t = SPLITTER * b;
37
79
  const bh = t - (t - b);
38
80
  const bl = b - bh;
39
- const e = ah * bh - p + ah * bl + al * bh + al * bl;
81
+ let e = ah * bh - p + ah * bl + al * bh + al * bl;
82
+ if (!Number.isFinite(e) && Number.isFinite(p))
83
+ e = prodErrScaled(a, b);
40
84
  return [p, e];
41
85
  }
42
86
  // The helpers below expand the form that calls twoSum, quickTwoSum and twoProd (Dekker / Knuth), keeping the same
@@ -48,6 +92,9 @@ export function ddAdd(a, b) {
48
92
  if (!ddEnabled)
49
93
  return [a[0] + b[0], 0];
50
94
  const s1 = a[0] + b[0];
95
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
96
+ if (!Number.isFinite(s1))
97
+ return [s1, 0];
51
98
  const bb2 = s1 - a[0];
52
99
  const e3 = a[0] - (s1 - bb2) + (b[0] - bb2);
53
100
  const s4 = a[1] + b[1];
@@ -70,6 +117,9 @@ export function ddSub(a, b) {
70
117
  const n1 = -b[0];
71
118
  const n2 = -b[1];
72
119
  const s3 = a[0] + n1;
120
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
121
+ if (!Number.isFinite(s3))
122
+ return [s3, 0];
73
123
  const bb4 = s3 - a[0];
74
124
  const e5 = a[0] - (s3 - bb4) + (n1 - bb4);
75
125
  const s6 = a[1] + n2;
@@ -87,13 +137,18 @@ export function ddMul(a, b) {
87
137
  if (!ddEnabled)
88
138
  return [a[0] * b[0], 0];
89
139
  const p1 = a[0] * b[0];
140
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
141
+ if (!Number.isFinite(p1))
142
+ return [p1, 0];
90
143
  const t2 = SPLITTER * a[0];
91
144
  const ah3 = t2 - (t2 - a[0]);
92
145
  const al4 = a[0] - ah3;
93
146
  const t5 = SPLITTER * b[0];
94
147
  const bh6 = t5 - (t5 - b[0]);
95
148
  const bl7 = b[0] - bh6;
96
- const e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
149
+ let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
150
+ if (!Number.isFinite(e8) && Number.isFinite(p1))
151
+ e8 = prodErrScaled(a[0], b[0]);
97
152
  const u9 = e8 + (a[0] * b[1] + a[1] * b[0]);
98
153
  const s10 = p1 + u9;
99
154
  const e11 = u9 - (s10 - p1);
@@ -103,13 +158,18 @@ export function ddMulNum(a, b) {
103
158
  if (!ddEnabled)
104
159
  return [a[0] * b, 0];
105
160
  const p1 = a[0] * b;
161
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
162
+ if (!Number.isFinite(p1))
163
+ return [p1, 0];
106
164
  const t2 = SPLITTER * a[0];
107
165
  const ah3 = t2 - (t2 - a[0]);
108
166
  const al4 = a[0] - ah3;
109
167
  const t5 = SPLITTER * b;
110
168
  const bh6 = t5 - (t5 - b);
111
169
  const bl7 = b - bh6;
112
- const e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
170
+ let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
171
+ if (!Number.isFinite(e8) && Number.isFinite(p1))
172
+ e8 = prodErrScaled(a[0], b);
113
173
  const u9 = e8 + a[1] * b;
114
174
  const s10 = p1 + u9;
115
175
  const e11 = u9 - (s10 - p1);
@@ -118,7 +178,14 @@ export function ddMulNum(a, b) {
118
178
  export function ddDiv(a, b) {
119
179
  if (!ddEnabled)
120
180
  return [a[0] / b[0], 0];
181
+ // (written by hand, as the prodErrScaled lines: a tiny numerator over a divisor below 1, both times 2^k first)
182
+ const k = tinyDivisionShift(a[0], b[0]);
183
+ if (k !== 0)
184
+ return ddDiv([scale2(a[0], k), scale2(a[1], k)], [scale2(b[0], k), scale2(b[1], k)]);
121
185
  const q1 = a[0] / b[0];
186
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
187
+ if (!Number.isFinite(q1))
188
+ return [q1, 0];
122
189
  const p2 = b[0] * q1;
123
190
  const t3 = SPLITTER * b[0];
124
191
  const ah4 = t3 - (t3 - b[0]);
@@ -126,7 +193,9 @@ export function ddDiv(a, b) {
126
193
  const t6 = SPLITTER * q1;
127
194
  const bh7 = t6 - (t6 - q1);
128
195
  const bl8 = q1 - bh7;
129
- const e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
196
+ let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
197
+ if (!Number.isFinite(e9) && Number.isFinite(p2))
198
+ e9 = prodErrScaled(b[0], q1);
130
199
  const u10 = e9 + b[1] * q1;
131
200
  const s11 = p2 + u10;
132
201
  const e12 = u10 - (s11 - p2);
@@ -152,7 +221,9 @@ export function ddDiv(a, b) {
152
221
  const t32 = SPLITTER * q27;
153
222
  const bh33 = t32 - (t32 - q27);
154
223
  const bl34 = q27 - bh33;
155
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
224
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
225
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
226
+ e35 = prodErrScaled(b[0], q27);
156
227
  const u36 = e35 + b[1] * q27;
157
228
  const s37 = p28 + u36;
158
229
  const e38 = u36 - (s37 - p28);
@@ -190,7 +261,14 @@ export function ddDiv(a, b) {
190
261
  export function ddDivNum(a, b) {
191
262
  if (!ddEnabled)
192
263
  return [a[0] / b, 0];
264
+ // (by hand, as in ddDiv)
265
+ const k = tinyDivisionShift(a[0], b);
266
+ if (k !== 0)
267
+ return ddDivNum([scale2(a[0], k), scale2(a[1], k)], scale2(b, k));
193
268
  const q1 = a[0] / b;
269
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
270
+ if (!Number.isFinite(q1))
271
+ return [q1, 0];
194
272
  const p2 = b * q1;
195
273
  const t3 = SPLITTER * b;
196
274
  const ah4 = t3 - (t3 - b);
@@ -198,7 +276,9 @@ export function ddDivNum(a, b) {
198
276
  const t6 = SPLITTER * q1;
199
277
  const bh7 = t6 - (t6 - q1);
200
278
  const bl8 = q1 - bh7;
201
- const e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
279
+ let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
280
+ if (!Number.isFinite(e9) && Number.isFinite(p2))
281
+ e9 = prodErrScaled(b, q1);
202
282
  const u10 = e9 + 0 * q1;
203
283
  const s11 = p2 + u10;
204
284
  const e12 = u10 - (s11 - p2);
@@ -224,7 +304,9 @@ export function ddDivNum(a, b) {
224
304
  const t32 = SPLITTER * q27;
225
305
  const bh33 = t32 - (t32 - q27);
226
306
  const bl34 = q27 - bh33;
227
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
307
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
308
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
309
+ e35 = prodErrScaled(b, q27);
228
310
  const u36 = e35 + 0 * q27;
229
311
  const s37 = p28 + u36;
230
312
  const e38 = u36 - (s37 - p28);
@@ -450,7 +532,9 @@ export function ddLog(x) {
450
532
  const t32 = SPLITTER * q27;
451
533
  const bh33 = t32 - (t32 - q27);
452
534
  const bl34 = q27 - bh33;
453
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
535
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
536
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
537
+ e35 = prodErrScaled(s25, q27);
454
538
  const u36 = e35 + e26 * q27;
455
539
  const s37 = p28 + u36;
456
540
  const e38 = u36 - (s37 - p28);
@@ -476,7 +560,9 @@ export function ddLog(x) {
476
560
  const t58 = SPLITTER * q53;
477
561
  const bh59 = t58 - (t58 - q53);
478
562
  const bl60 = q53 - bh59;
479
- const e61 = ah56 * bh59 - p54 + ah56 * bl60 + al57 * bh59 + al57 * bl60;
563
+ let e61 = ah56 * bh59 - p54 + ah56 * bl60 + al57 * bh59 + al57 * bl60;
564
+ if (!Number.isFinite(e61) && Number.isFinite(p54))
565
+ e61 = prodErrScaled(s25, q53);
480
566
  const u62 = e61 + e26 * q53;
481
567
  const s63 = p54 + u62;
482
568
  const e64 = u62 - (s63 - p54);
@@ -516,7 +602,9 @@ export function ddLog(x) {
516
602
  const t98 = SPLITTER * s92;
517
603
  const bh99 = t98 - (t98 - s92);
518
604
  const bl100 = s92 - bh99;
519
- const e101 = ah96 * bh99 - p94 + ah96 * bl100 + al97 * bh99 + al97 * bl100;
605
+ let e101 = ah96 * bh99 - p94 + ah96 * bl100 + al97 * bh99 + al97 * bl100;
606
+ if (!Number.isFinite(e101) && Number.isFinite(p94))
607
+ e101 = prodErrScaled(s92, s92);
520
608
  const u102 = e101 + (s92 * e93 + e93 * s92);
521
609
  const s103 = p94 + u102;
522
610
  const e104 = u102 - (s103 - p94);
@@ -527,7 +615,9 @@ export function ddLog(x) {
527
615
  const t109 = SPLITTER * s103;
528
616
  const bh110 = t109 - (t109 - s103);
529
617
  const bl111 = s103 - bh110;
530
- const e112 = ah107 * bh110 - p105 + ah107 * bl111 + al108 * bh110 + al108 * bl111;
618
+ let e112 = ah107 * bh110 - p105 + ah107 * bl111 + al108 * bh110 + al108 * bl111;
619
+ if (!Number.isFinite(e112) && Number.isFinite(p105))
620
+ e112 = prodErrScaled(RECIP[5][0], s103);
531
621
  const u113 = e112 + (RECIP[5][0] * e104 + RECIP[5][1] * s103);
532
622
  const s114 = p105 + u113;
533
623
  const e115 = u113 - (s114 - p105);
@@ -550,7 +640,9 @@ export function ddLog(x) {
550
640
  const t132 = SPLITTER * s103;
551
641
  const bh133 = t132 - (t132 - s103);
552
642
  const bl134 = s103 - bh133;
553
- const e135 = ah130 * bh133 - p128 + ah130 * bl134 + al131 * bh133 + al131 * bl134;
643
+ let e135 = ah130 * bh133 - p128 + ah130 * bl134 + al131 * bh133 + al131 * bl134;
644
+ if (!Number.isFinite(e135) && Number.isFinite(p128))
645
+ e135 = prodErrScaled(s126, s103);
554
646
  const u136 = e135 + (s126 * e104 + e127 * s103);
555
647
  const s137 = p128 + u136;
556
648
  const e138 = u136 - (s137 - p128);
@@ -573,7 +665,9 @@ export function ddLog(x) {
573
665
  const t155 = SPLITTER * s103;
574
666
  const bh156 = t155 - (t155 - s103);
575
667
  const bl157 = s103 - bh156;
576
- const e158 = ah153 * bh156 - p151 + ah153 * bl157 + al154 * bh156 + al154 * bl157;
668
+ let e158 = ah153 * bh156 - p151 + ah153 * bl157 + al154 * bh156 + al154 * bl157;
669
+ if (!Number.isFinite(e158) && Number.isFinite(p151))
670
+ e158 = prodErrScaled(s149, s103);
577
671
  const u159 = e158 + (s149 * e104 + e150 * s103);
578
672
  const s160 = p151 + u159;
579
673
  const e161 = u159 - (s160 - p151);
@@ -596,7 +690,9 @@ export function ddLog(x) {
596
690
  const t178 = SPLITTER * s103;
597
691
  const bh179 = t178 - (t178 - s103);
598
692
  const bl180 = s103 - bh179;
599
- const e181 = ah176 * bh179 - p174 + ah176 * bl180 + al177 * bh179 + al177 * bl180;
693
+ let e181 = ah176 * bh179 - p174 + ah176 * bl180 + al177 * bh179 + al177 * bl180;
694
+ if (!Number.isFinite(e181) && Number.isFinite(p174))
695
+ e181 = prodErrScaled(s172, s103);
600
696
  const u182 = e181 + (s172 * e104 + e173 * s103);
601
697
  const s183 = p174 + u182;
602
698
  const e184 = u182 - (s183 - p174);
@@ -619,7 +715,9 @@ export function ddLog(x) {
619
715
  const t201 = SPLITTER * s103;
620
716
  const bh202 = t201 - (t201 - s103);
621
717
  const bl203 = s103 - bh202;
622
- const e204 = ah199 * bh202 - p197 + ah199 * bl203 + al200 * bh202 + al200 * bl203;
718
+ let e204 = ah199 * bh202 - p197 + ah199 * bl203 + al200 * bh202 + al200 * bl203;
719
+ if (!Number.isFinite(e204) && Number.isFinite(p197))
720
+ e204 = prodErrScaled(s195, s103);
623
721
  const u205 = e204 + (s195 * e104 + e196 * s103);
624
722
  const s206 = p197 + u205;
625
723
  const e207 = u205 - (s206 - p197);
@@ -642,7 +740,9 @@ export function ddLog(x) {
642
740
  const t224 = SPLITTER * s218;
643
741
  const bh225 = t224 - (t224 - s218);
644
742
  const bl226 = s218 - bh225;
645
- const e227 = ah222 * bh225 - p220 + ah222 * bl226 + al223 * bh225 + al223 * bl226;
743
+ let e227 = ah222 * bh225 - p220 + ah222 * bl226 + al223 * bh225 + al223 * bl226;
744
+ if (!Number.isFinite(e227) && Number.isFinite(p220))
745
+ e227 = prodErrScaled(s92, s218);
646
746
  const u228 = e227 + (s92 * e219 + e93 * s218);
647
747
  const s229 = p220 + u228;
648
748
  const e230 = u228 - (s229 - p220);
@@ -653,7 +753,9 @@ export function ddLog(x) {
653
753
  const t235 = SPLITTER * 2;
654
754
  const bh236 = t235 - (t235 - 2);
655
755
  const bl237 = 2 - bh236;
656
- const e238 = ah233 * bh236 - p231 + ah233 * bl237 + al234 * bh236 + al234 * bl237;
756
+ let e238 = ah233 * bh236 - p231 + ah233 * bl237 + al234 * bh236 + al234 * bl237;
757
+ if (!Number.isFinite(e238) && Number.isFinite(p231))
758
+ e238 = prodErrScaled(s229, 2);
657
759
  const u239 = e238 + e230 * 2;
658
760
  const s240 = p231 + u239;
659
761
  const e241 = u239 - (s240 - p231);
@@ -676,7 +778,9 @@ export function ddLog(x) {
676
778
  const t258 = SPLITTER * k;
677
779
  const bh259 = t258 - (t258 - k);
678
780
  const bl260 = k - bh259;
679
- const e261 = ah256 * bh259 - p254 + ah256 * bl260 + al257 * bh259 + al257 * bl260;
781
+ let e261 = ah256 * bh259 - p254 + ah256 * bl260 + al257 * bh259 + al257 * bl260;
782
+ if (!Number.isFinite(e261) && Number.isFinite(p254))
783
+ e261 = prodErrScaled(LN2[0], k);
680
784
  const u262 = e261 + LN2[1] * k;
681
785
  const s263 = p254 + u262;
682
786
  const e264 = u262 - (s263 - p254);
@@ -38,6 +38,11 @@ export function gammainc(x, a) {
38
38
  const lg = lgamma(a);
39
39
  const g = gamma(a);
40
40
  const tail = (lower) => {
41
+ const logP = gammaInc(a, x, lower, true);
42
+ // a tail that is exactly 0 (the upper at x = Infinity, the lower at x = 0): 0, also where Gamma(a) overflows
43
+ // (Infinity * 0 made gammainc(Infinity, 1e308)$upper NaN)
44
+ if (logP === -Infinity)
45
+ return [0, -Infinity];
41
46
  const p = gammaInc(a, x, lower, false);
42
47
  const v = g * p;
43
48
  // the product where it is a normal number (then also its log: lgamma(a) + log P can cancel)
@@ -86,6 +91,9 @@ function smallTail(x, a, lower) {
86
91
  export function erfcinv(z) {
87
92
  if (Number.isNaN(z) || z < 0 || z > 2)
88
93
  return NaN;
94
+ // Below 2^-1021, z / 2 is not exact (Number.MIN_VALUE / 2 is 0): the log probability instead
95
+ if (z < 2 ** -1021)
96
+ return -qnorm(Math.log(z) - Math.LN2, { logP: true }) * Math.SQRT1_2;
89
97
  return -qnorm(z / 2) * Math.SQRT1_2;
90
98
  }
91
99
  /**
@@ -136,7 +144,11 @@ export function perm(n, k) {
136
144
  const intK = k === Math.floor(k);
137
145
  if (n === Math.floor(n) && intK && k > n)
138
146
  return 0;
139
- if (!Number.isFinite(n) || !Number.isFinite(k))
147
+ // the limit n -> Infinity of a product ~ n^k for a fixed finite k (lgamma would give Infinity - Infinity);
148
+ // both infinite is indeterminate
149
+ if (n === Infinity)
150
+ return k === Infinity ? NaN : k === 0 ? 1 : Infinity;
151
+ if (!Number.isFinite(k))
140
152
  return permLgamma(n, k);
141
153
  if (intK && k <= 1000)
142
154
  return fallingFactorial(n, k);
@@ -228,7 +240,7 @@ function lgammaDD(z) {
228
240
  const e = ddSub(ddMul(twoSum(z, -0.5), ddLog([z, 0])), [z, 0]);
229
241
  return ddAdd(ddAdd(e, LS2PI_DD), [stirlerr(z), 0]);
230
242
  }
231
- /** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (infinite n or k) */
243
+ /** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (finite n, infinite k) */
232
244
  function permLgamma(n, k) {
233
245
  const num = lgammaSign(n + 1);
234
246
  const den = lgammaSign(n - k + 1);
@@ -41,7 +41,8 @@ export function sinpi(x) {
41
41
  * Replaces xsf's Stirling approximation (polynomial STIR), which has a relative error of 7e-14 at x = 170.
42
42
  */
43
43
  function stirf(x) {
44
- if (x >= MAXGAM)
44
+ // MAXGAM rounds to the largest double whose Gamma is finite, so only beyond it is the result Infinity
45
+ if (x > MAXGAM)
45
46
  return Infinity;
46
47
  const n = x - 1;
47
48
  const e = ddAdd(ddMulNum(ddLog([n, 0]), n), [-n + stirlerrForGamma(n), 0]);
@@ -97,7 +98,15 @@ export function gamma(x) {
97
98
  z = q * sinpi(z);
98
99
  if (z === 0)
99
100
  return sgngam * Infinity;
100
- return (sgngam * Math.PI) / (Math.abs(z) * stirf(q));
101
+ const denom = Math.abs(z) * stirf(q);
102
+ if (Number.isFinite(denom))
103
+ return (sgngam * Math.PI) / denom;
104
+ // The denominator overflows (q beyond 171.6, or near it) while the result is a subnormal down to q = 177.5:
105
+ // Gamma(q) = Gamma(q - 10) (q - 1) ... (q - 10), with q - 10 exact, and the result rounded once at the end
106
+ let prod = 1;
107
+ for (let i = 1; i <= 10; i++)
108
+ prod *= q - i;
109
+ return (sgngam * Math.PI) / (Math.abs(z) * stirf(q - 10)) / prod;
101
110
  }
102
111
  return stirf(x);
103
112
  }
@@ -18,7 +18,7 @@ import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, isDoubleDouble, SPLITTER,
18
18
  import { lgamma1p } from "./gamma.js";
19
19
  import { lgammaDiffDD } from "./lgammaDiff.js";
20
20
  import { HUGE_SHAPE, HUGE_SHAPE_MAX_SMALL, lgammaRatioHuge, mulBig, prefactorParts, SMALL_SHAPE, smallShapeG, } from "./incbetPrefactor.js";
21
- import { betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
21
+ import { betaIncOverflowSum, betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
22
22
  import { betaIncLargeA, LARGE_A_MIN, SMALL_B_MAX } from "./incbetLargeA.js";
23
23
  const BIG = 4.503599627370496e15;
24
24
  const BIGINV = 2.22044604925031308085e-16;
@@ -749,9 +749,9 @@ function pseriesOk(a, b, x) {
749
749
  * { mu: 195.6 }) has 1 - x = 6.3e-286 against b/(a + b) = 8.5e-88, and the continued fraction above the mean was NaN)
750
750
  */
751
751
  function cfOk(a, b, x, xc) {
752
+ // (a/(a + b) as 1/(1 + b/a) and b/(a + b) as 1/(1 + a/b) where a + b overflows: the ratio was 0, below every x; #491)
752
753
  if (x[0] <= 0.5)
753
- return x[0] <= a / (a + b);
754
- // (b/(a + b) as 1/(1 + a/b) where a + b overflows)
754
+ return x[0] <= (a + b < Infinity ? a / (a + b) : 1 / (1 + b / a));
755
755
  return xc[0] >= (a + b < Infinity ? b / (a + b) : 1 / (1 + a / b));
756
756
  }
757
757
  /** Compute I_x(a, b) by continued fraction */
@@ -869,7 +869,9 @@ export function betaIncTinyX(a, b, x, lx, lower, wantLog) {
869
869
  * pbeta(0.615, 2, 2.7e-158, { lowerTail: false }) was 1.0000000000000002), so it is capped at 1 (log at 0)
870
870
  */
871
871
  function betaIncCore(a, b, xd, xcd, lower, wantLog) {
872
- const r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
872
+ let r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
873
+ if (Number.isNaN(r[1]) || (wantLog && Number.isNaN(r[0])))
874
+ r = betaIncOverflowSum(a, b, xd, xcd, lower, wantLog) ?? r;
873
875
  return r[1] > 1 || r[0] > 0 ? [Math.min(r[0], 0), Math.min(r[1], 1)] : r;
874
876
  }
875
877
  /**
@@ -889,11 +891,35 @@ function subShapeTail(t, s, small) {
889
891
  return [t[0] + Math.log(r), v];
890
892
  return [Math.log1p(-v), 1 - v];
891
893
  }
894
+ /**
895
+ * [log, linear] for both shapes below SUB_SHAPE: I_x(a, b) = b/(a + b) and 1 - I_x(a, b) = a/(a + b) to double precision
896
+ * for every x in (0, 1). From the series, I_x(a, b) = x^a b/(a + b) Γ(a+b+1)/(Γ(a+1) Γ(b+1)) (1 + a S) with abs(S) at most
897
+ * about -log(1 - x) (below 37 for a double x below 1) and abs(a log x) at most 745 a, so the relative error is below
898
+ * 1e-297; the upper tail likewise from I_{1-x}(b, a). The series overflowed in 1/a and the prefactor a b/(a + b) underflowed
899
+ * to 0 there (pbeta(0.5, 5e-324, 5e-324) was NaN, pbeta(0.5, 1e-322, 1e-315) -Infinity). R (toms708) takes the same limit
900
+ * below 1e-3 ε. The shapes are scaled exactly by 2^1000 into the normal range and a + b is kept in double-double (a = b
901
+ * gives 1/2 exactly)
902
+ */
903
+ function bothSubShape(a, b, lower, wantLog) {
904
+ const sa = a * 2 ** 1000;
905
+ const sb = b * 2 ** 1000;
906
+ const sum = twoSum(sa, sb);
907
+ const ratio = (u) => {
908
+ const r = ddDiv([u, 0], sum);
909
+ return r[0] + r[1];
910
+ };
911
+ const v = ratio(lower ? sb : sa);
912
+ if (!wantLog)
913
+ return [NaN, v];
914
+ return [v > 0.5 ? Math.log1p(-ratio(lower ? sa : sb)) : Math.log(v), v];
915
+ }
892
916
  function betaIncUncapped(a, b, xd, xcd, lower, wantLog) {
893
917
  if (xd[0] <= 0)
894
918
  return lower ? [-Infinity, 0] : [0, 1];
895
919
  if (xcd[0] <= 0)
896
920
  return lower ? [0, 1] : [-Infinity, 0];
921
+ if (a < SUB_SHAPE && b < SUB_SHAPE)
922
+ return bothSubShape(a, b, lower, wantLog);
897
923
  // The tail of the order of a shape below SUB_SHAPE (1 - I_x(a, b) for a tiny a, I_x(a, b) for a tiny b), from
898
924
  // that at SUB_SHAPE scaled (see there), and the other tail as 1 minus it
899
925
  if (a < SUB_SHAPE && b > SUB_OTHER)
@@ -17,3 +17,10 @@ export declare function halfErfc(y: number): number;
17
17
  * used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
18
18
  */
19
19
  export declare function betaIncTemme(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
20
+ /**
21
+ * Fallback where a + b overflows and the other methods gave NaN (the continued fraction and the prefactor form
22
+ * a + b; pbeta(0.1, 1e308, 1e308) was NaN; #491). The smaller tail is e^-E times a factor whose log is of the order of
23
+ * the logs of the shapes (below about 2000 in size), so where E >= 1e22 its log is -E to double precision and its
24
+ * linear value 0. E in the halved shapes, doubled, as in betaIncTemme. Undefined where a + b is finite or E < 1e22
25
+ */
26
+ export declare function betaIncOverflowSum(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;