time-to-botec

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

main.js (1869B)


      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 isnan = require( './../../../../base/assert/is-nan' );
     24 var isInteger = require( './../../../../base/assert/is-integer' );
     25 var abs = require( './../../../../base/special/abs' );
     26 var MAX_LUCAS = require( '@stdlib/constants/float64/max-safe-nth-lucas' );
     27 var NEGALUCAS = require( './negalucas.json' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Computes the nth negaLucas number.
     34 *
     35 * @param {NonPositiveInteger} n - the negaLucas number to compute
     36 * @returns {integer} negaLucas number
     37 *
     38 * @example
     39 * var y = negalucas( 0 );
     40 * // returns 2
     41 *
     42 * @example
     43 * var y = negalucas( -1 );
     44 * // returns -1
     45 *
     46 * @example
     47 * var y = negalucas( -2 );
     48 * // returns 3
     49 *
     50 * @example
     51 * var y = negalucas( -3 );
     52 * // returns -4
     53 *
     54 * @example
     55 * var y = negalucas( -4 );
     56 * // returns 7
     57 *
     58 * @example
     59 * var y = negalucas( -5 );
     60 * // returns -11
     61 *
     62 * @example
     63 * var y = negalucas( -6 );
     64 * // returns 18
     65 *
     66 * @example
     67 * var y = negalucas( NaN );
     68 * // returns NaN
     69 *
     70 * @example
     71 * var y = negalucas( -3.14 );
     72 * // returns NaN
     73 */
     74 function negalucas( n ) {
     75 	var an;
     76 	if (
     77 		isnan( n ) ||
     78 		isInteger( n ) === false ||
     79 		n > 0
     80 	) {
     81 		return NaN;
     82 	}
     83 	an = abs( n );
     84 	if ( an > MAX_LUCAS ) {
     85 		return NaN;
     86 	}
     87 	return NEGALUCAS[ an ];
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = negalucas;