time-to-botec

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

correction.js (1636B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var pow = require( '@stdlib/math/base/special/pow' );
     24 
     25 
     26 // VARIABLES //
     27 
     28 var ONE_12 = 1.0 / 12.0;
     29 var ONE_360 = 1.0 / 360.0;
     30 var ONE_1260 = 1.0 / 1260.0;
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns a correction for Stirling's approximation.
     37 *
     38 * @private
     39 * @param {NonNegativeInteger} k - input argument
     40 * @returns {number} correction term
     41 *
     42 * @example
     43 * var c = correction( 0 );
     44 * // returns 0.08106146679532726
     45 */
     46 function correction( k ) {
     47 	var v;
     48 	switch ( k ) {
     49 	case 0:
     50 		return 0.08106146679532726;
     51 	case 1:
     52 		return 0.04134069595540929;
     53 	case 2:
     54 		return 0.02767792568499834;
     55 	case 3:
     56 		return 0.02079067210376509;
     57 	case 4:
     58 		return 0.01664469118982119;
     59 	case 5:
     60 		return 0.01387612882307075;
     61 	case 6:
     62 		return 0.01189670994589177;
     63 	case 7:
     64 		return 0.01041126526197209;
     65 	case 8:
     66 		return 0.009255462182712733;
     67 	case 9:
     68 		return 0.008330563433362871;
     69 	default:
     70 		k += 1;
     71 		v = pow( k, 2 );
     72 		return (ONE_12 - ((ONE_360 - (ONE_1260/v)) / v)) / k;
     73 	}
     74 }
     75 
     76 
     77 // EXPORTS //
     78 
     79 module.exports = correction;