Files
Vincent Gao 52da37f4a7 Fix gamma-family overflow for large shape parameters
GAMMA.INV silently returned the first bisection midpoint for alpha in
~[143, 171.62] (e.g. GAMMA.INV(0.5, 143, 1) gave 358.0 instead of
142.667) and #NUM! above that, because the Newton-step pdf evaluates
Gamma(a), b**a and value**(a-1) in linear domain, all of which overflow
even though the density itself is a small representable number. The same
pattern breaks the GAMMA.DIST, CHISQ.DIST and F.DIST densities and
GAMMALN, which computed log(Gamma(x)) through Gamma(x).

Evaluate these in log domain via the existing logGamma, and scale the
incomplete-gamma series/continued-fraction iteration cap as O(sqrt(a)),
which both expansions need to converge near x ~ a once the shape is
large (GAMMA.INV drifted from the true quantile above alpha ~5000 and
returned alpha+1 by alpha=10000; same for CHISQ.INV at high df).
2026-08-07 01:14:45 +02:00

42 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class GammaLnTest extends AllSetupTeardown
{
#[\PHPUnit\Framework\Attributes\DataProvider('providerGAMMALN')]
public function testGAMMALN(mixed $expectedResult, mixed ...$args): void
{
$this->runTestCases('GAMMALN', $expectedResult, ...$args);
}
public static function providerGAMMALN(): array
{
return require 'tests/data/Calculation/Statistical/GAMMALN.php';
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerGammaLnArray')]
public function testGammaLnArray(array $expectedResult, string $values): void
{
$calculation = Calculation::getInstance();
$formula = "=GAMMALN({$values})";
$result = $calculation->calculateFormula($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public static function providerGammaLnArray(): array
{
return [
'matrix' => [
[['#NUM!', 1.5240638224307844], [0.20328095143129537, 2.8813232759012449]],
'{-1.5, 0.2; 0.75, 4.8}',
],
];
}
}