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 | 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2343x 2343x 2343x 2343x 2343x 2343x 2343x 2343x 1343x 2343x 1001x 1001x 2343x 2x 1x 1x 1x 1x 2343x 82x 82x 2343x 82x 82x 1176x 2343x 944x 541x 541x 403x 403x 403x 944x 200x 944x 203x 203x 403x 944x 201x 201x 201x 403x 403x 403x 232x 232x 2343x 1874x 1874x 1874x 2343x 1697x 1x 1x 1696x 1696x 1696x 2343x 205x 1x 1x 204x 204x 204x 2343x 36x 36x 194x 194x 2343x 1x 1x 1x 1x 1x | /** * @license Apache-2.0 * * Copyright (c) 2018 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); var isInteger = require( '@stdlib/math/base/assert/is-integer' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); var abs = require( '@stdlib/math/base/special/abs' ); var floor = require( '@stdlib/math/base/special/floor' ); var sin = require( '@stdlib/math/base/special/sin' ); var PINF = require( '@stdlib/constants/float64/pinf' ); var NINF = require( '@stdlib/constants/float64/ninf' ); var PI = require( '@stdlib/constants/float64/pi' ); var stirlingApprox = require( './stirling_approximation.js' ); var smallApprox = require( './small_approximation.js' ); var rateval = require( './rational_pq.js' ); // MAIN // /** * Evaluates the gamma function. * * ## Method * * 1. Arguments \\(|x| \leq 34\\) are reduced by recurrence and the function approximated by a rational function of degree \\(6/7\\) in the interval \\((2,3)\\). * 2. Large negative arguments are made positive using a reflection formula. * 3. Large arguments are handled by Stirling's formula. * * ## Notes * * - Relative error: * * | arithmetic | domain | # trials | peak | rms | * |:----------:|:---------:|:--------:|:-------:|:-------:| * | DEC | -34,34 | 10000 | 1.3e-16 | 2.5e-17 | * | IEEE | -170,-33 | 20000 | 2.3e-15 | 3.3e-16 | * | IEEE | -33, 33 | 20000 | 9.4e-16 | 2.2e-16 | * | IEEE | 33, 171.6 | 20000 | 2.3e-15 | 3.2e-16 | * * - Error for arguments outside the test range will be larger owing to error amplification by the exponential function. * * @param {number} x - input value * @returns {number} function value * * @example * var v = gamma( 4.0 ); * // returns 6.0 * * @example * var v = gamma( -1.5 ); * // returns ~2.363 * * @example * var v = gamma( -0.5 ); * // returns ~-3.545 * * @example * var v = gamma( 0.5 ); * // returns ~1.772 * * @example * var v = gamma( 0.0 ); * // returns Infinity * * @example * var v = gamma( -0.0 ); * // returns -Infinity * * @example * var v = gamma( NaN ); * // returns NaN */ function gamma( x ) { var sign; var q; var p; var z; if ( (isInteger( x ) && x < 0) || x === NINF || isnan( x ) ) { return NaN; } if ( x === 0.0 ) { if ( isNegativeZero( x ) ) { return NINF; } return PINF; } if ( x > 171.61447887182298 ) { return PINF; } if ( x < -170.5674972726612 ) { return 0.0; } q = abs( x ); if ( q > 33.0 ) { if ( x >= 0.0 ) { return stirlingApprox( x ); } p = floor( q ); // Check whether `x` is even... if ( (p&1) === 0 ) { sign = -1.0; } else { sign = 1.0; } z = q - p; if ( z > 0.5 ) { p += 1.0; z = q - p; } z = q * sin( PI * z ); return sign * PI / ( abs(z)*stirlingApprox(q) ); } // Reduce `x`... z = 1.0; while ( x >= 3.0 ) { x -= 1.0; z *= x; } while ( x < 0.0 ) { if ( x > -1.0e-9 ) { return smallApprox( x, z ); } z /= x; x += 1.0; } while ( x < 2.0 ) { if ( x < 1.0e-9 ) { return smallApprox( x, z ); } z /= x; x += 1.0; } if ( x === 2.0 ) { return z; } x -= 2.0; return z * rateval( x ); } // EXPORTS // module.exports = gamma; |