Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use FLOAT64_MAX_SAFE_NTH_FACTORIAL #6370

Merged
merged 1 commit into from
Mar 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/gammainc/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
var SQRT_TWO_PI = require( '@stdlib/constants/float64/sqrt-two-pi' );
var MAX_LN = require( '@stdlib/constants/float64/max-ln' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( '@stdlib/constants/float64/max-safe-nth-factorial' ); // eslint-disable-line id-length
var finiteGammaQ = require( './finite_gamma_q.js' );
var finiteHalfGammaQ = require( './finite_half_gamma_q.js' );
var fullIGammaPrefix = require( './full_igamma_prefix.js' );
Expand All @@ -58,19 +59,14 @@
var upperGammaFraction = require( './upper_gamma_fraction.js' );


// VARIABLES //

var MAX_FACTORIAL = 170; // TODO: consider extracting as a constant


// MAIN //

/**
* Computes the regularized incomplete gamma function. The upper tail is calculated via the modified Lentz's method for computing continued fractions, the lower tail using a power expansion.
*
* ## Notes
*
* - When `a >= MAX_FACTORIAL` and computing the non-normalized incomplete gamma, result is rather hard to compute unless we use logs. There are really two options a) if `x` is a long way from `a` in value then we can reliably use methods 2 and 4 below in logarithmic form and go straight to the result. Otherwise we let the regularized gamma take the strain (the result is unlikely to underflow in the central region anyway) and combine with `lgamma` in the hopes that we get a finite result.
* - When `a >= FLOAT64_MAX_SAFE_NTH_FACTORIAL` and computing the non-normalized incomplete gamma, result is rather hard to compute unless we use logs. There are really two options a) if `x` is a long way from `a` in value then we can reliably use methods 2 and 4 below in logarithmic form and go straight to the result. Otherwise we let the regularized gamma take the strain (the result is unlikely to underflow in the central region anyway) and combine with `lgamma` in the hopes that we get a finite result.
*
* @param {NonNegativeNumber} x - function parameter
* @param {PositiveNumber} a - function parameter
Expand Down Expand Up @@ -101,7 +97,7 @@
normalized = ( regularized === void 0 ) ? true : regularized;
invert = upper;
result = 0.0;
if ( a >= MAX_FACTORIAL && !normalized ) {
if ( a >= FLOAT64_MAX_SAFE_NTH_FACTORIAL && !normalized ) {
if ( invert && ( a * 4.0 < x ) ) {
// This is method 4 below, done in logs:
result = ( a * ln(x) ) - x;
Expand Down Expand Up @@ -180,7 +176,7 @@
if ( normalized && a > 20 ) {
sigma = abs( (x-a)/a );
if ( a > 200 ) {
// Limit chosen so that we use Temme's expansion only if the result would be larger than about 10^-6. Below that the regular series and continued fractions converge OK, and if we use Temme's method we get increasing errors from the dominant erfc term as it's (inexact) argument increases in magnitude.

Check warning on line 179 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "erfc"
if ( 20 / a > sigma * sigma ) {
useTemme = true;
}
Expand Down