bernoulli.js (2110B)
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 isNonNegativeInteger = require( './../../../../base/assert/is-nonnegative-integer' ); 24 var isnan = require( './../../../../base/assert/is-nan' ); 25 var isOdd = require( './../../../../base/assert/is-odd' ); 26 var NINF = require( '@stdlib/constants/float64/ninf' ); 27 var PINF = require( '@stdlib/constants/float64/pinf' ); 28 var BERNOULLI = require( './bernoulli.json' ); 29 30 31 // VARIABLES // 32 33 var MAX_BERNOULLI = 258|0; // asm type annotation 34 35 36 // MAIN // 37 38 /** 39 * Computes the nth Bernoulli number. 40 * 41 * @param {NonNegativeInteger} n - the Bernoulli number to compute 42 * @returns {number} Bernoulli number 43 * 44 * @example 45 * var y = bernoulli( 0 ); 46 * // returns 1.0 47 * 48 * @example 49 * var y = bernoulli( 1 ); 50 * // returns 0.0 51 * 52 * @example 53 * var y = bernoulli( 2 ); 54 * // returns ~0.167 55 * 56 * @example 57 * var y = bernoulli( 3 ); 58 * // returns 0.0 59 * 60 * @example 61 * var y = bernoulli( 4 ); 62 * // returns ~-0.033 63 * 64 * @example 65 * var y = bernoulli( 5 ); 66 * // returns 0.0 67 * 68 * @example 69 * var y = bernoulli( 20 ); 70 * // returns ~-529.124 71 * 72 * @example 73 * var y = bernoulli( 260 ); 74 * // returns -Infinity 75 * 76 * @example 77 * var y = bernoulli( 262 ); 78 * // returns Infinity 79 * 80 * @example 81 * var y = bernoulli( NaN ); 82 * // returns NaN 83 */ 84 function bernoulli( n ) { 85 if ( isnan( n ) || !isNonNegativeInteger( n ) ) { 86 return NaN; 87 } 88 if ( isOdd( n ) ) { 89 return 0.0; 90 } 91 if ( n > MAX_BERNOULLI ) { 92 return ( (n/2)&1 ) ? PINF : NINF; 93 } 94 return BERNOULLI[ n/2 ]; 95 } 96 97 98 // EXPORTS // 99 100 module.exports = bernoulli;