Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 2055x 1001x 1001x 1054x 2055x 2x 1x 1x 1x 1x 1052x 2055x 7x 7x 1045x 2055x 7x 7x 1038x 2055x 495x 495x 495x 495x 495x 257x 495x 238x 238x 495x 495x 255x 255x 255x 495x 495x 495x 255x 255x 495x 495x 1038x 1038x 1038x 2055x 60x 2055x 978x 978x 1038x 2055x 15369x 15369x 15369x 1038x 2055x 89x 1x 1x 1x 88x 88x 88x 1038x 2055x 1x 1x 2055x 1037x 59x 59x 1037x 37x 1037x 1000x 1000x 1000x 1037x 2055x 495x 495x 1038x 2055x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The original C code, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
*
* Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library, a commercial product. In either event, it is copyrighted by the author. What you see here may be used freely but it comes with no support or guarantee.
*
* Stephen L. Moshier
* moshier@na-net.ornl.gov
* ```
*/
'use strict';
// MODULES //
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var sinf = require( '@stdlib/math/base/special/sinf' );
var PINF = require( '@stdlib/constants/float32/pinf' );
var NINF = require( '@stdlib/constants/float32/ninf' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var PI = require( '@stdlib/constants/float32/pi' );
var smallApprox = require( './small_approximation.js' );
var polyeval = require( './polyval_p.js' );
// var stirlingApprox = require( './stirling_approximation.js' );
// VARIABLES //
var ZERO = f32( 0.0 );
var HALF = f32( 0.5 );
var ONE = f32( 1.0 );
var TWO = f32( 2.0 );
var THREE = f32( 3.0 );
var MAX_ARG = f32( 34.84425627277176174 );
var MIN_ARG = f32( -34.1955451965332 );
var SMALL_X = f32( 1.0e-4 );
// var TEN = f32( 10 );
// MAIN //
/**
* Evaluates the gamma function of a single-precision floating-point number.
*
* ## Method
*
* 1. Arguments between 0 and 10 are reduced by recurrence and the function is approximated by a polynomial function covering the interval (2,3)
* 2. Large arguments are handled by Stirling's formula.
* 3. Negative arguments are made positive using a reflection formula.
*
* ## Notes
*
* - Relative error:
*
* | arithmetic | domain | # trials | peak | rms |
* |:----------:|:---------:|:--------:|:-------:|:-------:|
* | IEEE | 0,-33 | 100,000 | 5.7e-7 | 1.0e-7 |
* | IEEE | -33,0 | 100,000 | 6.1e-7 | 1.2e-7 |
*
* @param {number} x - input value
* @returns {number} function value
*
* @example
* var v = gammaf( 4.0 );
* // returns 6.0
*
*/
function gammaf( x ) {
var direction;
var negative;
var small;
var sign;
var nz;
var q;
var p;
var z;
x = f32( x );
if ( ( isIntegerf( x ) && x < 0 ) || isnanf( x ) || x === NINF ) {
return NaN;
}
if ( x === ZERO ) {
if ( isNegativeZerof( x ) ) {
return NINF;
}
return PINF;
}
if ( x > MAX_ARG ) {
return PINF;
}
if ( x < MIN_ARG ) {
return 0.0;
}
if ( x < ZERO ) {
negative = 1;
q = -x;
p = floorf( q );
if ( p === q ) {
return NaN;
}
if ( (p&1) === 0 ) {
sign = -ONE;
} else {
sign = ONE;
}
nz = f32( q - p );
if ( nz > HALF ) {
p += ONE;
nz = f32( q - p );
}
nz = f32( q * sinf( f32( PI * nz ) ) );
if ( nz === ZERO ) {
return NaN;
}
if ( nz < 0 ) {
nz = f32( -nz );
}
x = f32( q );
}
// if ( x >= TEN ) {
// z = stirlingApprox( x );
// }
if ( x < TWO ) {
direction = 1;
} else {
direction = 0;
}
z = ONE;
while ( x >= THREE ) {
x = f32( x - ONE );
z = f32( z * x );
}
small = 0;
while ( x < TWO ) {
if ( x < SMALL_X ) {
small = 1;
break;
}
z = f32( z * x );
x = f32( x + ONE );
}
if ( small ) {
if ( x === ZERO ) {
return PINF;
}
p = smallApprox( x, z );
} else {
if ( direction ) {
z = f32( ONE / z );
}
if ( x === TWO ) {
p = z;
} else {
x = f32( x - TWO );
p = f32( z * polyeval( x ) );
}
}
if ( negative ) {
p = f32( f32( sign * PI ) / f32( nz * p ) );
}
return p;
}
// EXPORTS //
module.exports = gammaf;
|