y_is_infinite.js (2048B)
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 abs = require( './../../../../base/special/abs' ); 24 var PINF = require( '@stdlib/constants/float64/pinf' ); 25 26 27 // MAIN // 28 29 /** 30 * Evaluates the exponential function when \\( y = \pm \infty\\). 31 * 32 * @private 33 * @param {number} x - base 34 * @param {number} y - exponent 35 * @returns {number} function value 36 * 37 * @example 38 * var v = pow( -1.0, Infinity ); 39 * // returns NaN 40 * 41 * @example 42 * var v = pow( -1.0, -Infinity ); 43 * // returns NaN 44 * 45 * @example 46 * var v = pow( 1.0, Infinity ); 47 * // returns 1.0 48 * 49 * @example 50 * var v = pow( 1.0, -Infinity ); 51 * // returns 1.0 52 * 53 * @example 54 * var v = pow( 0.5, Infinity ); 55 * // returns 0.0 56 * 57 * @example 58 * var v = pow( 0.5, -Infinity ); 59 * // returns Infinity 60 * 61 * @example 62 * var v = pow( 1.5, -Infinity ); 63 * // returns 0.0 64 * 65 * @example 66 * var v = pow( 1.5, Infinity ); 67 * // returns Infinity 68 */ 69 function pow( x, y ) { 70 if ( x === -1.0 ) { 71 // Julia (0.4.2) and Python (2.7.9) return `1.0` (WTF???). JavaScript (`Math.pow`), R, and libm return `NaN`. We choose `NaN`, as the value is indeterminate; i.e., we cannot determine whether `y` is odd, even, or somewhere in between. 72 return (x-x)/(x-x); // signal NaN 73 } 74 if ( x === 1.0 ) { 75 return 1.0; 76 } 77 // (|x| > 1 && y === NINF) || (|x| < 1 && y === PINF) 78 if ( (abs(x) < 1.0) === (y === PINF) ) { 79 return 0.0; 80 } 81 // (|x| > 1 && y === PINF) || (|x| < 1 && y === NINF) 82 return PINF; 83 } 84 85 86 // EXPORTS // 87 88 module.exports = pow;