| 653 | if (k > 0 && term.abs().lt(tol)) break; |
| 654 | result = result.add(term); |
| 655 | pw = pw._mulToPrecision(u, p); |
| 656 | } |
| 657 | |
| 658 | return result.sub(logProduct); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Bignum Gamma function. Precision scales with BigDecimal.precision. |
| 663 | * |
| 664 | * Reflection for z < 0.5; general case delegates to exp(bigGammaln(z)). |
| 665 | * Note: integer fast paths are in bigGammaln (returning exact ln(n!)). |
| 666 | * bigGamma goes through exp(ln(...)) so the result is a full-precision float. |
| 667 | */ |
| 668 | export function bigGamma(ce: ComputeEngine, z: BigNum): BigNum { |
| 669 | // Exact: positive integer → (z-1)! (precision-independent, skip the |