time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

gamma1pm1.js (2515B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 *
     18 *
     19 * ## Notice
     20 *
     21 * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_64_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
     22 *
     23 * ```text
     24 * (C) Copyright John Maddock 2006-7, 2013-14.
     25 * (C) Copyright Paul A. Bristow 2007, 2013-14.
     26 * (C) Copyright Nikhar Agrawal 2013-14.
     27 * (C) Copyright Christopher Kormanyos 2013-14.
     28 *
     29 * Use, modification and distribution are subject to the
     30 * Boost Software License, Version 1.0. (See accompanying file
     31 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     32 * ```
     33 */
     34 
     35 'use strict';
     36 
     37 // MODULES //
     38 
     39 var gamma = require( './../../../../base/special/gamma' );
     40 var expm1 = require( './../../../../base/special/expm1' );
     41 var log1p = require( './../../../../base/special/log1p' );
     42 var isnan = require( './../../../../base/assert/is-nan' );
     43 var lgammaSmallImp = require( './lgamma_small_imp.js' );
     44 
     45 
     46 // MAIN //
     47 
     48 /**
     49 * Computes `gamma(x+1) - 1`.
     50 *
     51 * @param {number} x - input value
     52 * @returns {number} function value
     53 *
     54 * @example
     55 * var v = gammap1m1( 0.2 );
     56 * // returns ~-0.082
     57 *
     58 * @example
     59 * var v = gammap1m1( -9.2 );
     60 * // returns ~-1.0
     61 *
     62 * @example
     63 * var v = gammap1m1( 0.0 );
     64 * // returns 0.0
     65 *
     66 * @example
     67 * var v = gammap1m1( -3.0 );
     68 * // returns NaN
     69 *
     70 * @example
     71 * var v = gammap1m1( NaN );
     72 * // returns NaN
     73 */
     74 function gammap1m1( x ) {
     75 	if ( isnan( x ) ) {
     76 		return NaN;
     77 	}
     78 	if ( x < 0.0 ) {
     79 		if ( x < -0.5 ) {
     80 			// Best method is simply to subtract 1 from gamma:
     81 			return gamma( 1.0+x ) - 1.0;
     82 		}
     83 		// Use expm1 on the logarithm of gamma:
     84 		return expm1( -log1p( x ) + lgammaSmallImp( x+2.0, x+1.0, x ) );
     85 	}
     86 	if ( x < 2.0 ) {
     87 		// Use expm1 on the logarithm of gamma:
     88 		return expm1( lgammaSmallImp( x+1.0, x, x-1.0 ) );
     89 	}
     90 	// Best method is simply to subtract 1 from gamma:
     91 	return gamma( 1.0+x ) - 1.0;
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = gammap1m1;