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