time-to-botec

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

README.md (3661B)


      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 [single-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float32/base/to-binary-string].
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var fromBinaryStringf = require( '@stdlib/number/float32/base/from-binary-string' );
     31 ```
     32 
     33 #### fromBinaryStringf( bstr )
     34 
     35 Creates a [single-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float32/base/to-binary-string].
     36 
     37 ```javascript
     38 var bstr = '01000000100000000000000000000000';
     39 var v = fromBinaryStringf( bstr );
     40 // returns 4.0
     41 
     42 bstr = '01000000010010010000111111011011';
     43 v = fromBinaryStringf( bstr );
     44 // returns ~3.14
     45 
     46 bstr = '11111111011011000011101000110011';
     47 v = fromBinaryStringf( bstr );
     48 // returns ~-3.14e+38
     49 ```
     50 
     51 The function handles [subnormals][subnormals].
     52 
     53 ```javascript
     54 var bstr = '10000000000000000000000000010110';
     55 var val = fromBinaryStringf( bstr );
     56 // returns ~-3.08e-44
     57 
     58 bstr = '00000000000000000000000000000001';
     59 val = fromBinaryStringf( bstr );
     60 // returns ~1.40e-45
     61 ```
     62 
     63 The function handles special values.
     64 
     65 ```javascript
     66 var bstr = '00000000000000000000000000000000';
     67 var val = fromBinaryStringf( bstr );
     68 // returns 0.0
     69 
     70 bstr = '10000000000000000000000000000000';
     71 val = fromBinaryStringf( bstr );
     72 // returns -0.0
     73 
     74 bstr = '01111111110000000000000000000000';
     75 val = fromBinaryStringf( bstr );
     76 // returns NaN
     77 
     78 bstr = '01111111100000000000000000000000';
     79 val = fromBinaryStringf( bstr );
     80 // returns Infinity
     81 
     82 bstr = '11111111100000000000000000000000';
     83 val = fromBinaryStringf( 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 toFloat32 = require( '@stdlib/number/float64/base/to-float32' );
    102 var toBinaryStringf = require( '@stdlib/number/float32/base/to-binary-string' );
    103 var fromBinaryStringf = require( '@stdlib/number/float32/base/from-binary-string' );
    104 
    105 var frac;
    106 var sign;
    107 var exp;
    108 var b;
    109 var x;
    110 var y;
    111 var i;
    112 
    113 // Convert random numbers to IEEE 754 literal bit representations and then convert them back...
    114 for ( i = 0; i < 100; i++ ) {
    115     if ( randu() < 0.5 ) {
    116         sign = -1.0;
    117     } else {
    118         sign = 1.0;
    119     }
    120     frac = randu() * 10.0;
    121     exp = round( randu()*100.0 );
    122     if ( randu() < 0.5 ) {
    123         exp = -exp;
    124     }
    125     x = sign * frac * pow( 2.0, exp );
    126     x = toFloat32( x );
    127 
    128     b = toBinaryStringf( x );
    129     y = fromBinaryStringf( b );
    130 
    131     console.log( '%d => %s => %d', x, b, y );
    132     console.log( x === y );
    133 }
    134 ```
    135 
    136 </section>
    137 
    138 <!-- /.examples -->
    139 
    140 <section class="links">
    141 
    142 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
    143 
    144 [subnormals]: https://en.wikipedia.org/wiki/Denormal_number
    145 
    146 [@stdlib/number/float32/base/to-binary-string]: https://www.npmjs.com/package/@stdlib/number/tree/main/float32/base/to-binary-string
    147 
    148 </section>
    149 
    150 <!-- /.links -->