time-to-botec

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

main.js (2261B)


      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 cospi = require( './../../../../base/special/cospi' );
     25 var pow = require( './../../../../base/special/pow' );
     26 var PHI = require( '@stdlib/constants/float64/phi' );
     27 var PINF = require( '@stdlib/constants/float64/pinf' );
     28 var NINF = require( '@stdlib/constants/float64/ninf' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 var SQRT_5 = 2.23606797749979;
     34 
     35 
     36 // MAIN //
     37 
     38 /**
     39 * Evaluates Binet's formula extended to real numbers.
     40 *
     41 * ## Notes
     42 *
     43 * -   [Non integer Fibonacci numbers][1]
     44 * -   [Interpolated Fibonacci numbers - real or complex][2]
     45 *
     46 * [1]: https://math.stackexchange.com/questions/798190/non-integer-fibonacci-numbers
     47 * [2]: https://math.stackexchange.com/questions/589841/interpolated-fibonacci-numbers-real-or-complex
     48 *
     49 * @param {number} x - input value
     50 * @returns {number} real-valued result
     51 *
     52 * @example
     53 * var y = binet( 0.0 );
     54 * // returns 0.0
     55 *
     56 * @example
     57 * var y = binet( 1.0 );
     58 * // returns 1.0
     59 *
     60 * @example
     61 * var y = binet( 2.0 );
     62 * // returns 1.0
     63 *
     64 * @example
     65 * var y = binet( 3.0 );
     66 * // returns 2.0
     67 *
     68 * @example
     69 * var y = binet( 4.0 );
     70 * // returns 3.0
     71 *
     72 * @example
     73 * var y = binet( 5.0 );
     74 * // returns ~5.0
     75 *
     76 * @example
     77 * var y = binet( 6.0 );
     78 * // returns ~8.0
     79 *
     80 * @example
     81 * var y = binet( NaN );
     82 * // returns NaN
     83 *
     84 * @example
     85 * var y = binet( 3.14 );
     86 * // returns ~2.12
     87 *
     88 * @example
     89 * var y = binet( -1.0 );
     90 * // returns 1.0
     91 */
     92 function binet( x ) {
     93 	var a;
     94 	var b;
     95 	if (
     96 		isnan( x ) ||
     97 		x === PINF ||
     98 		x === NINF
     99 	) {
    100 		return NaN;
    101 	}
    102 	a = pow( PHI, x );
    103 	b = cospi( x ) / a;
    104 	return ( a - b ) / SQRT_5;
    105 }
    106 
    107 
    108 // EXPORTS //
    109 
    110 module.exports = binet;