main.js (1777B)
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 getHighWord = require( './../../../../float64/base/get-high-word' ); 24 var EXP_MASK = require( '@stdlib/constants/float64/high-word-exponent-mask' ); 25 var BIAS = require( '@stdlib/constants/float64/exponent-bias' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an integer corresponding to the unbiased exponent of a double-precision floating-point number. 32 * 33 * @param {number} x - input value 34 * @returns {integer32} unbiased exponent 35 * 36 * @example 37 * var exp = exponent( 3.14e-307 ); // => 2**-1019 ~ 1e-307 38 * // returns -1019 39 * 40 * @example 41 * var exp = exponent( -3.14 ); 42 * // returns 1 43 * 44 * @example 45 * var exp = exponent( 0.0 ); 46 * // returns -1023 47 * 48 * @example 49 * var exp = exponent( NaN ); 50 * // returns 1024 51 */ 52 function exponent( x ) { 53 // Extract from the input value a higher order word (unsigned 32-bit integer) which contains the exponent: 54 var high = getHighWord( x ); 55 56 // Apply a mask to isolate only the exponent bits and then shift off all bits which are part of the fraction: 57 high = ( high & EXP_MASK ) >>> 20; 58 59 // Remove the bias and return: 60 return (high - BIAS)|0; // asm type annotation 61 } 62 63 64 // EXPORTS // 65 66 module.exports = exponent;