time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (3004B)


      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 PINF = require( '@stdlib/constants/float32/pinf' );
     24 var NINF = require( '@stdlib/constants/float32/ninf' );
     25 var BIAS = require( '@stdlib/constants/float32/exponent-bias' );
     26 var pow = require( '@stdlib/math/base/special/pow' );
     27 var toFloat32 = require( './../../../../float64/base/to-float32' );
     28 var toFrac = require( './tofrac.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Creates a single-precision floating-point number from an IEEE 754 literal bit representation.
     35 *
     36 * @param {BinaryString} bstr - string which is a literal bit representation
     37 * @throws {Error} must provide a string with a length equal to `32`
     38 * @returns {number} single-precision floating-point number
     39 *
     40 * @example
     41 * var bstr = '01000000100000000000000000000000';
     42 * var v = fromBinaryStringf( bstr );
     43 * // returns 4.0
     44 *
     45 * @example
     46 * var bstr = '01000000010010010000111111011011';
     47 * var v = fromBinaryStringf( bstr );
     48 * // returns ~3.14
     49 *
     50 * @example
     51 * var bstr = '11111111011011000011101000110011';
     52 * var v = fromBinaryStringf( bstr );
     53 * // returns ~-3.14e+38
     54 *
     55 * @example
     56 * var bstr = '00000000000000000000000000000000';
     57 * var v =  fromBinaryStringf( bstr );
     58 * // returns 0.0
     59 *
     60 * @example
     61 * var bstr = '10000000000000000000000000000000';
     62 * var v = fromBinaryStringf( bstr );
     63 * // returns -0.0
     64 */
     65 function fromBinaryStringf( bstr ) {
     66 	var sign;
     67 	var frac;
     68 	var exp;
     69 
     70 	if ( bstr.length !== 32 ) {
     71 		throw new Error( 'invalid argument. Input string must have a length equal to 32. Value: `'+bstr+'`.' );
     72 	}
     73 	// Sign bit:
     74 	sign = ( bstr[0] === '1' ) ? -1.0 : 1.0;
     75 
     76 	// Exponent bits:
     77 	exp = parseInt( bstr.substring(1, 9), 2 ) - BIAS;
     78 
     79 	// Fraction bits:
     80 	frac = toFrac( bstr.substring( 9 ) );
     81 
     82 	// Detect `0` (all 0s) and subnormals (exponent bits are all 0, but fraction bits are not all 0s)...
     83 	if ( exp === -BIAS ) {
     84 		if ( frac === 0.0 ) {
     85 			return ( sign === 1.0 ) ? 0.0 : -0.0;
     86 		}
     87 		exp = -(BIAS-1); // subnormals are special in that their exponent is constant
     88 	}
     89 	// Detect `+-inf` (exponent bits are all 1 and fraction is 0) and `NaN` (exponent bits are all 1 and fraction is not 0)...
     90 	else if ( exp === BIAS+1 ) {
     91 		if ( frac === 0.0 ) {
     92 			return ( sign === 1.0 ) ? PINF : NINF;
     93 		}
     94 		return NaN;
     95 	}
     96 	// Normal numbers...
     97 	else {
     98 		// Account for hidden/implicit bit (2^0):
     99 		frac += 1.0;
    100 	}
    101 	return toFloat32( sign*frac*pow(2, exp) );
    102 }
    103 
    104 
    105 // EXPORTS //
    106 
    107 module.exports = fromBinaryStringf;