negafibonacci.js (1947B)
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_FIBONACCI = require( '@stdlib/constants/float64/max-safe-nth-fibonacci' ); 27 var NEGAFIBONACCI = require( './negafibonacci.json' ); 28 29 30 // MAIN // 31 32 /** 33 * Computes the nth negaFibonacci number. 34 * 35 * @param {NonPositiveInteger} n - the negaFibonacci number to compute 36 * @returns {integer} negaFibonacci number 37 * 38 * @example 39 * var y = negafibonacci( 0 ); 40 * // returns 0 41 * 42 * @example 43 * var y = negafibonacci( -1 ); 44 * // returns 1 45 * 46 * @example 47 * var y = negafibonacci( -2 ); 48 * // returns -1 49 * 50 * @example 51 * var y = negafibonacci( -3 ); 52 * // returns 2 53 * 54 * @example 55 * var y = negafibonacci( -4 ); 56 * // returns -3 57 * 58 * @example 59 * var y = negafibonacci( -5 ); 60 * // returns 5 61 * 62 * @example 63 * var y = negafibonacci( -6 ); 64 * // returns -8 65 * 66 * @example 67 * var y = negafibonacci( NaN ); 68 * // returns NaN 69 * 70 * @example 71 * var y = negafibonacci( -3.14 ); 72 * // returns NaN 73 */ 74 function negafibonacci( 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_FIBONACCI ) { 85 return NaN; 86 } 87 return NEGAFIBONACCI[ an ]; 88 } 89 90 91 // EXPORTS // 92 93 module.exports = negafibonacci;