main.js (2149B)
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 getWord = require( './../../../../float32/base/to-word' ); 24 var BIAS = require( '@stdlib/constants/float32/exponent-bias' ); 25 26 27 // VARIABLES // 28 29 // Exponent mask: 0 11111111 00000000000000000000000 30 var EXP_MASK = 0x7f800000; // TODO: consider making an external constant 31 32 33 // MAIN // 34 35 /** 36 * Returns an integer corresponding to the unbiased exponent of a single-precision floating-point number. 37 * 38 * @param {number} x - single-precision floating-point number 39 * @returns {integer8} unbiased exponent 40 * 41 * @example 42 * var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 43 * var exp = exponentf( toFloat32( 3.14e34 ) ); // => 2**114 ~ 2.08e34 44 * // returns 114 45 * 46 * @example 47 * var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 48 * var exp = exponentf( toFloat32( 3.14e-34 ) ); // => 2**-112 ~ 1.93e-34 49 * // returns -112 50 * 51 * @example 52 * var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 53 * var exp = exponentf( toFloat32( -3.14 ) ); 54 * // returns 1 55 * 56 * @example 57 * var exp = exponentf( 0.0 ); 58 * // returns -127 59 * 60 * @example 61 * var exp = exponentf( NaN ); 62 * // returns 128 63 */ 64 function exponentf( x ) { 65 // Convert `x` to an unsigned 32-bit integer corresponding to the IEEE 754 binary representation: 66 var w = getWord( x ); 67 68 // Apply a mask to isolate only the exponent bits and then shift off all bits which are part of the fraction: 69 w = ( w & EXP_MASK ) >>> 23; 70 71 // Remove the bias and return: 72 return w - BIAS; 73 } 74 75 76 // EXPORTS // 77 78 module.exports = exponentf;