README.md (3275B)
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 # Binary String 22 23 > Return a string giving the literal bit representation of a [double-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toBinaryString = require( '@stdlib/number/float64/base/to-binary-string' ); 31 ``` 32 33 #### toBinaryString( x ) 34 35 Returns a `string` giving the literal bit representation of a [double-precision floating-point number][ieee754]. 36 37 ```javascript 38 var str = toBinaryString( 4.0 ); 39 // returns '0100000000010000000000000000000000000000000000000000000000000000' 40 41 str = toBinaryString( 3.141592653589793 ); 42 // returns '0100000000001001001000011111101101010100010001000010110100011000' 43 44 str = toBinaryString( -1.0e308 ); 45 // returns '1111111111100001110011001111001110000101111010111100100010100000' 46 ``` 47 48 The function handles [subnormals][subnormals]. 49 50 ```javascript 51 str = toBinaryString( -3.14e-320 ); 52 // returns '1000000000000000000000000000000000000000000000000001100011010011' 53 54 str = toBinaryString( 5.0e-324 ); 55 // returns '0000000000000000000000000000000000000000000000000000000000000001' 56 ``` 57 58 The function handles special values. 59 60 ```javascript 61 str = toBinaryString( 0.0 ); 62 // returns '0000000000000000000000000000000000000000000000000000000000000000' 63 64 str = toBinaryString( -0.0 ); 65 // returns '1000000000000000000000000000000000000000000000000000000000000000' 66 67 str = toBinaryString( NaN ); 68 // returns '0111111111111000000000000000000000000000000000000000000000000000' 69 70 str = toBinaryString( Infinity ); 71 // returns '0111111111110000000000000000000000000000000000000000000000000000' 72 73 str = toBinaryString( -Infinity ); 74 // returns '1111111111110000000000000000000000000000000000000000000000000000' 75 ``` 76 77 </section> 78 79 <!-- /.usage --> 80 81 <section class="examples"> 82 83 ## Examples 84 85 <!-- eslint no-undef: "error" --> 86 87 ```javascript 88 var randu = require( '@stdlib/random/base/randu' ); 89 var round = require( '@stdlib/math/base/special/round' ); 90 var pow = require( '@stdlib/math/base/special/pow' ); 91 var toBinaryString = require( '@stdlib/number/float64/base/to-binary-string' ); 92 93 var frac; 94 var sign; 95 var exp; 96 var b; 97 var x; 98 var i; 99 100 // Convert random numbers to literal bit representations... 101 for ( i = 0; i < 100; i++ ) { 102 if ( randu() < 0.5 ) { 103 sign = -1.0; 104 } else { 105 sign = 1.0; 106 } 107 frac = randu() * 10.0; 108 exp = round( randu()*100.0 ); 109 if ( randu() < 0.5 ) { 110 exp = -exp; 111 } 112 x = sign * frac * pow( 2.0, exp ); 113 b = toBinaryString( x ); 114 console.log( b ); 115 } 116 ``` 117 118 </section> 119 120 <!-- /.examples --> 121 122 <section class="links"> 123 124 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 125 126 [subnormals]: https://en.wikipedia.org/wiki/Denormal_number 127 128 </section> 129 130 <!-- /.links -->