y_is_huge.js (2448B)
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 * ## Notice 20 * 21 * The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/s_pow.c}. The implementation follows the original, but has been modified for JavaScript. 22 * 23 * ```text 24 * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. 25 * 26 * Developed at SunPro, a Sun Microsystems, Inc. business. 27 * Permission to use, copy, modify, and distribute this 28 * software is freely granted, provided that this notice 29 * is preserved. 30 * ``` 31 */ 32 33 'use strict'; 34 35 // MODULES // 36 37 var getHighWord = require( '@stdlib/number/float64/base/get-high-word' ); 38 39 40 // VARIABLES // 41 42 // 0x7fffffff = 2147483647 => 0 11111111111 11111111111111111111 43 var ABS_MASK = 0x7fffffff|0; // asm type annotation 44 45 // 0x3fefffff = 1072693247 => 0 01111111110 11111111111111111111 => biased exponent: 1022 = -1+1023 => 2^-1 46 var HIGH_MAX_NEAR_UNITY = 0x3fefffff|0; // asm type annotation 47 48 var HUGE = 1.0e300; 49 var TINY = 1.0e-300; 50 51 52 // MAIN // 53 54 /** 55 * Evaluates the exponential function when \\(|y| > 2^64\\). 56 * 57 * @private 58 * @param {number} x - base 59 * @param {number} y - exponent 60 * @returns {number} overflow or underflow result 61 * 62 * @example 63 * var v = pow( 9.0, 3.6893488147419103e19 ); 64 * // returns Infinity 65 * 66 * @example 67 * var v = pow( -3.14, -3.6893488147419103e19 ); 68 * // returns 0.0 69 */ 70 function pow( x, y ) { 71 var ahx; 72 var hx; 73 74 hx = getHighWord( x ); 75 ahx = (hx & ABS_MASK); 76 77 if ( ahx <= HIGH_MAX_NEAR_UNITY ) { 78 if ( y < 0 ) { 79 // signal overflow... 80 return HUGE * HUGE; 81 } 82 // signal underflow... 83 return TINY * TINY; 84 } 85 // `x` has a biased exponent greater than or equal to `0`... 86 87 if ( y > 0 ) { 88 // signal overflow... 89 return HUGE * HUGE; 90 } 91 // signal underflow... 92 return TINY * TINY; 93 } 94 95 96 // EXPORTS // 97 98 module.exports = pow;