time-to-botec

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

main.js (2146B)


      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 ln = require( './../../../../base/special/ln' );
     26 var round = require( './../../../../base/special/round' );
     27 var PHI = require( '@stdlib/constants/float64/phi' );
     28 var PINF = require( '@stdlib/constants/float64/pinf' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 var SQRT_5 = 2.23606797749979;
     34 var LN_PHI = ln( PHI );
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Computes the Fibonacci number index.
     41 *
     42 * ## Notes
     43 *
     44 * -   We use `round` instead of `floor` due to errors introduced by floating-point precision.
     45 *
     46 *
     47 * @param {NonNegativeInteger} F - Fibonacci number
     48 * @returns {NonNegativeInteger} Fibonacci number index
     49 *
     50 * @example
     51 * var n = fibonacciIndex( 0 );
     52 * // returns NaN
     53 *
     54 * @example
     55 * var n = fibonacciIndex( 1 );
     56 * // returns NaN
     57 *
     58 * @example
     59 * var n = fibonacciIndex( 2 );
     60 * // returns 3
     61 *
     62 * @example
     63 * var n = fibonacciIndex( 3 );
     64 * // returns 4
     65 *
     66 * @example
     67 * var n = fibonacciIndex( 5 );
     68 * // returns 5
     69 *
     70 * @example
     71 * var n = fibonacciIndex( 8 );
     72 * // returns 6
     73 *
     74 * @example
     75 * var n = fibonacciIndex( NaN );
     76 * // returns NaN
     77 *
     78 * @example
     79 * var n = fibonacciIndex( 3.14 );
     80 * // returns NaN
     81 *
     82 * @example
     83 * var n = fibonacciIndex( -1 );
     84 * // returns NaN
     85 */
     86 function fibonacciIndex( F ) {
     87 	var x;
     88 	if (
     89 		isnan( F ) ||
     90 		isInteger( F ) === false ||
     91 		F <= 1 ||
     92 		F === PINF
     93 	) {
     94 		return NaN;
     95 	}
     96 	x = ( F*SQRT_5 ) + 0.5;
     97 	return round( ln( x ) / LN_PHI );
     98 }
     99 
    100 
    101 // EXPORTS //
    102 
    103 module.exports = fibonacciIndex;