README.md (3385B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # To Binary String 22 23 > Return a string giving the literal bit representation of a [single-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toBinaryStringf = require( '@stdlib/number/float32/base/to-binary-string' ); 31 ``` 32 33 #### toBinaryStringf( x ) 34 35 Returns a `string` giving the literal bit representation of a [single-precision floating-point number][ieee754]. 36 37 ```javascript 38 var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 39 40 var str = toBinaryStringf( toFloat32( 4.0 ) ); 41 // returns '01000000100000000000000000000000' 42 43 str = toBinaryStringf( toFloat32( 3.141592653589793 ) ); 44 // returns '01000000010010010000111111011011' 45 46 str = toBinaryStringf( toFloat32( -1.0e38 ) ); 47 // returns '11111110100101100111011010011001' 48 ``` 49 50 The function handles [subnormals][subnormals]. 51 52 ```javascript 53 var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 54 55 str = toBinaryStringf( toFloat32( -3.14e-39 ) ); 56 // returns '10000000001000100011000100001011' 57 58 str = toBinaryStringf( toFloat32( 1.4e-45 ) ); 59 // returns '00000000000000000000000000000001' 60 ``` 61 62 The function handles special values. 63 64 ```javascript 65 var PINF = require( '@stdlib/constants/float32/pinf' ); 66 var NINF = require( '@stdlib/constants/float32/ninf' ); 67 68 str = toBinaryStringf( 0.0 ); 69 // returns '00000000000000000000000000000000' 70 71 str = toBinaryStringf( -0.0 ); 72 // returns '10000000000000000000000000000000' 73 74 str = toBinaryStringf( NaN ); 75 // returns '01111111110000000000000000000000' 76 77 str = toBinaryStringf( PINF ); 78 // returns '01111111100000000000000000000000' 79 80 str = toBinaryStringf( NINF ); 81 // returns '11111111100000000000000000000000' 82 ``` 83 84 </section> 85 86 <!-- /.usage --> 87 88 <section class="examples"> 89 90 ## Examples 91 92 <!-- eslint no-undef: "error" --> 93 94 ```javascript 95 var randu = require( '@stdlib/random/base/randu' ); 96 var round = require( '@stdlib/math/base/special/round' ); 97 var pow = require( '@stdlib/math/base/special/pow' ); 98 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 99 var toBinaryStringf = require( '@stdlib/number/float32/base/to-binary-string' ); 100 101 var frac; 102 var sign; 103 var exp; 104 var b; 105 var x; 106 var i; 107 108 // Convert random numbers to literal bit representations... 109 for ( i = 0; i < 100; i++ ) { 110 if ( randu() < 0.5 ) { 111 sign = -1.0; 112 } else { 113 sign = 1.0; 114 } 115 frac = randu() * 10.0; 116 exp = round( randu()*100.0 ); 117 if ( randu() < 0.5 ) { 118 exp = -exp; 119 } 120 x = sign * frac * pow( 2.0, exp ); 121 x = float64ToFloat32( x ); 122 b = toBinaryStringf( x ); 123 console.log( b ); 124 } 125 ``` 126 127 </section> 128 129 <!-- /.examples --> 130 131 <section class="links"> 132 133 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-2008 134 135 [subnormals]: https://en.wikipedia.org/wiki/Denormal_number 136 137 </section> 138 139 <!-- /.links -->