Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4033x 4033x 4033x 4033x 4033x 4033x 4033x 4033x 4033x 4033x 4033x 505x 4033x 3528x 3528x 4033x 4033x 2x 2x 2x 2x 2x 4031x 4033x 1x 1x 1x 1x 1x 4030x 4033x 24x 24x 24x 24x 24x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4033x 2003x 2003x 2003x 2003x 2003x 36267x 2003x 2003x 2003x 36267x 2003x 2003x 2003x 4006x 4006x 4033x 2003x 2003x 1557x 1557x 1557x 2003x 2003x 2003x 2003x 2003x 2003x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4006x 4033x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var PINF = require( '@stdlib/constants/float16/pinf' );
var NINF = require( '@stdlib/constants/float16/ninf' );
var BIAS = require( '@stdlib/constants/float16/exponent-bias' );
var abs = require( '@stdlib/math/base/special/abs' );
var floor = require( '@stdlib/math/base/special/floor' );
var rpad = require( '@stdlib/string/right-pad' );
var lpad = require( '@stdlib/string/left-pad' );
var repeat = require( '@stdlib/string/repeat' );
var div2 = require( './div2.js' );
var mult2 = require( './mult2.js' );
// VARIABLES //
// TODO: consider placing in external modules
var NUM_SIGNIFICAND_BITS = 10;
var NUM_EXPONENT_BITS = 5;
// MAIN //
/**
* Returns a string giving the literal bit representation of a half-precision floating-point number.
*
* @param {number} x - input value
* @returns {BinaryString} bit representation
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var str = toBinaryString( toFloat16( 4.0 ) );
* // returns '0100010000000000'
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var str = toBinaryString( toFloat16( 3.1415926 ) );
* // returns '0100001001001000'
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var str = toBinaryString( toFloat16( -1.0e3 ) );
* // returns '1110001111010000'
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var str = toBinaryString( toFloat16( -3.14e-6 ) );
* // returns '1000000000110101'
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var str = toBinaryString( toFloat16( 1.4e-7 ) );
* // returns '0000000000000010'
*
* @example
* var str = toBinaryString( 0.0 );
* // returns '0000000000000000'
*
* @example
* var str = toBinaryString( -0.0 );
* // returns '1000000000000000'
*
* @example
* var str = toBinaryString( NaN );
* // returns '0111111000000000'
*
* @example
* var PINF = require( '@stdlib/constants/float16/pinf' );
*
* var str = toBinaryString( PINF );
* // returns '0111110000000000'
*
* @example
* var NINF = require( '@stdlib/constants/float16/ninf' );
*
* var str = toBinaryString( NINF );
* // returns '1111110000000000'
*/
function toBinaryString( x ) {
var nbits;
var sign;
var str;
var exp;
var n;
var f;
var i;
// Check for a negative value or negative zero...
if ( x < 0.0 || 1.0/x === NINF ) {
sign = '1';
} else {
sign = '0';
}
// Special case: +-infinity
if ( x === PINF || x === NINF ) {
// Based on IEEE 754-2008...
exp = repeat( '1', NUM_EXPONENT_BITS ); // all 1s
str = repeat( '0', NUM_SIGNIFICAND_BITS ); // all 0s
return sign + exp + str;
}
// Special case: NaN
if ( x !== x ) {
// Based on IEEE 754-2008...
exp = repeat( '1', NUM_EXPONENT_BITS ); // all 1s
str = '1' + repeat( '0', NUM_SIGNIFICAND_BITS-1 ); // can't be all 0s
return sign + exp + str;
}
// Special case: +-0
if ( x === 0.0 ) {
// Based on IEEE 754-2008...
exp = repeat( '0', NUM_EXPONENT_BITS ); // all 0s
str = repeat( '0', NUM_SIGNIFICAND_BITS ); // all 0s
return sign + exp + str;
}
x = abs( x );
// Isolate the integer part (digits before the decimal):
n = floor( x );
// Isolate the fractional part (digits after the decimal):
f = x - n;
// Convert the integer and fractional parts to bit strings:
n = div2( n );
f = mult2( f );
// Determine the exponent needed to normalize the integer+fractional parts...
if ( n ) {
// Move the decimal `d` digits to the left:
exp = n.length - 1;
} else {
// Find the first '1' bit...
for ( i = 0; i < f.length; i++ ) {
if ( f[ i ] === '1' ) {
nbits = i + 1;
break;
}
}
// Move the decimal `d` digits to the right:
exp = -nbits;
}
// Normalize the combined integer+fractional string...
str = n + f;
if ( exp < 0 ) {
// Handle subnormals...
if ( exp <= -BIAS ) {
// Cap the number of bits removed:
nbits = BIAS - 1;
}
// Remove all leading zeros and the first '1' for normal values, and, for subnormals, remove at most BIAS-1 leading bits:
str = str.substring( nbits );
} else {
// Remove the leading '1' (implicit/hidden bit):
str = str.substring( 1 );
}
// Convert the exponent to a bit string:
exp = div2( exp + BIAS );
exp = lpad( exp, NUM_EXPONENT_BITS, '0' );
// Fill in any trailing zeros and ensure we have only 10 fraction bits:
str = rpad( str, NUM_SIGNIFICAND_BITS, '0' ).substring( 0, NUM_SIGNIFICAND_BITS );
// Return a bit representation:
return sign + exp + str;
}
// EXPORTS //
module.exports = toBinaryString;
|